Skip to content
File

Blob: src/worker/sites/prepare-page-render.ts

typescript63 lines
1import type { Db } from "@/worker/db/d1/client";
2import {
3 resolvePublishedMentions,
4 type ResolvedMention,
5 type ResolvedPublishedPage,
6} from "@/worker/lib/published-pages";
7import { collectMentionPageIds } from "@/sites/json-prewalk";
8import type { SiteDocumentAssets } from "@/sites/types";
9import type { LoadedPmJson } from "@/worker/sites/load-page-pm-json";
10import { resolveSitesDocumentAssets } from "@/worker/sites/manifest";
11 
12type SiteTiming = <T>(name: string, operation: () => Promise<T>) => Promise<T>;
13 
14export interface PreparedSitePageRender {
15 pmJson: LoadedPmJson;
16 mentionIds: string[];
17 mentions: Map<string, ResolvedMention>;
18 assets: SiteDocumentAssets;
19 canonicalUrl: string;
20 canonicalPath: string;
21 currentIsHome: boolean;
22}
23 
24export interface PrepareSitePageRenderArgs {
25 env: Pick<Env, "ASSETS">;
26 db: Db;
27 timings: SiteTiming;
28 page: ResolvedPublishedPage;
29 pmJson: LoadedPmJson;
30 currentIsHome: boolean;
31 canonicalPath: string;
32 canonicalUrl: string;
33}
34 
35export async function prepareSitePageRender({
36 env,
37 db,
38 timings,
39 page,
40 pmJson,
41 currentIsHome,
42 canonicalPath,
43 canonicalUrl,
44}: PrepareSitePageRenderArgs): Promise<PreparedSitePageRender | null> {
45 const assets = await timings("asset_manifest", () => resolveSitesDocumentAssets(env));
46 if (!assets) return null;
47 
48 const mentionIds = collectMentionPageIds(pmJson.content);
49 const mentions = await timings("mention_resolution", () =>
50 resolvePublishedMentions(db, page.workspace_id, mentionIds),
51 );
52 
53 return {
54 pmJson,
55 mentionIds,
56 mentions,
57 assets,
58 canonicalUrl,
59 canonicalPath,
60 currentIsHome,
61 };
62}