From bcc29480c7e0309157165d6a3e4614f0db71a5f0 Mon Sep 17 00:00:00 2001 From: vcoppe Date: Mon, 23 Sep 2024 15:21:01 +0200 Subject: [PATCH] preload all guide titles --- website/src/hooks.server.js | 2 +- .../help/{+layout.ts => +layout.server.ts} | 8 ++--- .../routes/[[language]]/help/+layout.svelte | 10 ++----- .../src/routes/[[language]]/help/+page.svelte | 10 ++----- .../help/[...guide]/+page.server.ts | 29 +++++++++++++++++++ .../[[language]]/help/[...guide]/+page.svelte | 12 +++----- .../[[language]]/help/[...guide]/+page.ts | 14 +++------ 7 files changed, 48 insertions(+), 37 deletions(-) rename website/src/routes/[[language]]/help/{+layout.ts => +layout.server.ts} (70%) create mode 100644 website/src/routes/[[language]]/help/[...guide]/+page.server.ts diff --git a/website/src/hooks.server.js b/website/src/hooks.server.js index 3b41f793..b537f353 100644 --- a/website/src/hooks.server.js +++ b/website/src/hooks.server.js @@ -48,7 +48,7 @@ export async function handle({ event, resolve }) { const stringsHTML = page === 'app' ? stringsToHTML(strings) : ''; const response = await resolve(event, { - transformPageChunk: ({ html }) => html.replace('', htmlTag).replace('', headTag).replace('', ``) + transformPageChunk: ({ html }) => html.replace('', htmlTag).replace('', headTag).replace('', `
${stringsHTML}
`) }); return response; diff --git a/website/src/routes/[[language]]/help/+layout.ts b/website/src/routes/[[language]]/help/+layout.server.ts similarity index 70% rename from website/src/routes/[[language]]/help/+layout.ts rename to website/src/routes/[[language]]/help/+layout.server.ts index f1bf5e82..c3bd752b 100644 --- a/website/src/routes/[[language]]/help/+layout.ts +++ b/website/src/routes/[[language]]/help/+layout.server.ts @@ -14,15 +14,15 @@ function getModule(language: string | undefined, guide: string) { export async function load({ params }) { const { language } = params; - const guideModules: Record = {}; + const guideTitles: Record = {}; for (let guide of Object.keys(guides)) { - guideModules[guide] = getModule(language, guide); + guideTitles[guide] = (await getModule(language, guide)).metadata.title; for (let subguide of guides[guide]) { - guideModules[`${guide}/${subguide}`] = getModule(language, `${guide}/${subguide}`); + guideTitles[`${guide}/${subguide}`] = (await getModule(language, `${guide}/${subguide}`)).metadata.title; } } return { - guideModules + guideTitles }; } \ No newline at end of file diff --git a/website/src/routes/[[language]]/help/+layout.svelte b/website/src/routes/[[language]]/help/+layout.svelte index 8fc2746c..2655ca40 100644 --- a/website/src/routes/[[language]]/help/+layout.svelte +++ b/website/src/routes/[[language]]/help/+layout.svelte @@ -6,7 +6,7 @@ import { guides } from '$lib/components/docs/docs'; export let data: { - guideModules: Record; + guideTitles: Record; }; @@ -21,9 +21,7 @@ ? 'font-semibold text-foreground' : ''}" > - {#await data.guideModules[guide] then guideModule} - {guideModule.metadata.title} - {/await} + {data.guideTitles[guide]} {#each guides[guide] as subGuide} {/each} {/each} diff --git a/website/src/routes/[[language]]/help/+page.svelte b/website/src/routes/[[language]]/help/+page.svelte index 47a06966..c785ce7c 100644 --- a/website/src/routes/[[language]]/help/+page.svelte +++ b/website/src/routes/[[language]]/help/+page.svelte @@ -5,7 +5,7 @@ import { guides, guideIcons } from '$lib/components/docs/docs'; export let data: { - guideModules: Record; + guideTitles: Record; }; @@ -21,9 +21,7 @@ {guideIcons[guide]}
- {#await data.guideModules[guide] then guideModule} - {guideModule.metadata.title} - {/await} + {data.guideTitles[guide]}
{#each guides[guide] as subGuide} @@ -33,9 +31,7 @@ class="min-h-8 h-fit min-w-24 px-0 py-1 text-muted-foreground text-base text-center whitespace-normal" > - {#await data.guideModules[`${guide}/${subGuide}`] then guideModule} - {guideModule.metadata.title} - {/await} + {data.guideTitles[`${guide}/${subGuide}`]} {/each}
diff --git a/website/src/routes/[[language]]/help/[...guide]/+page.server.ts b/website/src/routes/[[language]]/help/[...guide]/+page.server.ts new file mode 100644 index 00000000..ff7c5538 --- /dev/null +++ b/website/src/routes/[[language]]/help/[...guide]/+page.server.ts @@ -0,0 +1,29 @@ +import { getNextGuide, getPreviousGuide } from "$lib/components/docs/docs"; + +function getModule(language: string | undefined, guide: string) { + language = language ?? 'en'; + let subguide = undefined; + if (guide.includes('/')) { + [guide, subguide] = guide.split('/'); + } + return subguide + ? import(`./../../../../lib/docs/${language}/${guide}/${subguide}.mdx`) + : import(`./../../../../lib/docs/${language}/${guide}.mdx`); +} + +export async function load({ params }) { + const { guide, language } = params; + + const previousGuide = getPreviousGuide(guide); + const nextGuide = getNextGuide(guide); + + const previousGuideTitle = previousGuide ? (await getModule(language, previousGuide)).metadata.title : undefined; + const nextGuideTitle = nextGuide ? (await getModule(language, nextGuide)).metadata.title : undefined; + + return { + previousGuide, + previousGuideTitle, + nextGuide, + nextGuideTitle + }; +} \ No newline at end of file diff --git a/website/src/routes/[[language]]/help/[...guide]/+page.svelte b/website/src/routes/[[language]]/help/[...guide]/+page.svelte index e43917d0..80f14a88 100644 --- a/website/src/routes/[[language]]/help/[...guide]/+page.svelte +++ b/website/src/routes/[[language]]/help/[...guide]/+page.svelte @@ -9,9 +9,9 @@ export let data: { guideModule: any; previousGuide: string | undefined; - previousGuideModule: any; + previousGuideTitle: string | undefined; nextGuide: string | undefined; - nextGuideModule: any; + nextGuideTitle: string | undefined; }; @@ -27,9 +27,7 @@ href={getURLForLanguage($locale, `/help/${data.previousGuide}`)} > - {#await data.previousGuideModule then guideModule} - {guideModule.metadata.title} - {/await} + {data.previousGuideTitle} {/if} {#if data.nextGuide} @@ -38,9 +36,7 @@ class="ml-auto" href={getURLForLanguage($locale, `/help/${data.nextGuide}`)} > - {#await data.nextGuideModule then guideModule} - {guideModule.metadata.title} - {/await} + {data.nextGuideTitle} {/if} diff --git a/website/src/routes/[[language]]/help/[...guide]/+page.ts b/website/src/routes/[[language]]/help/[...guide]/+page.ts index 1d32699a..f2ddf5ae 100644 --- a/website/src/routes/[[language]]/help/[...guide]/+page.ts +++ b/website/src/routes/[[language]]/help/[...guide]/+page.ts @@ -1,5 +1,3 @@ -import { getNextGuide, getPreviousGuide } from "$lib/components/docs/docs"; - function getModule(language: string | undefined, guide: string) { language = language ?? 'en'; let subguide = undefined; @@ -11,17 +9,13 @@ function getModule(language: string | undefined, guide: string) { : import(`./../../../../lib/docs/${language}/${guide}.mdx`); } -export async function load({ params }) { +export async function load({ data, params }) { const { guide, language } = params; - const previousGuide = getPreviousGuide(guide); - const nextGuide = getNextGuide(guide); + const guideModule = await getModule(language, guide); return { - guideModule: await getModule(language, guide), - previousGuide, - previousGuideModule: previousGuide ? getModule(language, previousGuide) : undefined, - nextGuide, - nextGuideModule: nextGuide ? getModule(language, nextGuide) : undefined, + guideModule, + ...data, }; } \ No newline at end of file