From e0a2fb0256fc85931aee3a464c4d3ed0093b0e38 Mon Sep 17 00:00:00 2001 From: outfoxxed Date: Thu, 24 Oct 2024 19:40:53 -0700 Subject: [PATCH] only generate type data once --- src/components/Header.astro | 4 ++-- src/components/navigation/sidebars/nav/RootNav.astro | 4 ++-- src/config/io/generateTypeData.ts | 12 +++++++++++- src/pages/docs/types/[type]/[name].astro | 4 ++-- src/pages/docs/types/[type]/index.astro | 4 ++-- src/pages/docs/types/index.astro | 4 ++-- 6 files changed, 21 insertions(+), 11 deletions(-) diff --git a/src/components/Header.astro b/src/components/Header.astro index cda71ab..24942c4 100644 --- a/src/components/Header.astro +++ b/src/components/Header.astro @@ -1,12 +1,12 @@ --- import { ThemeSelect } from "@components/hooks/ThemeSwitch"; -import { generateTypeData } from "@config/io/generateTypeData"; +import { getTypeData } from "@config/io/generateTypeData"; import Nav from "@components/navigation/sidebars/Nav.astro"; import TOC from "@components/navigation/sidebars/TOC.astro"; import type { TypeTOC } from "./navigation/sidebars/types"; import Search from "./navigation/Search.astro"; -const routes = await generateTypeData(); +const routes = await getTypeData(); const url = Astro.url.pathname.split("/"); const currentClass = url[4]; diff --git a/src/components/navigation/sidebars/nav/RootNav.astro b/src/components/navigation/sidebars/nav/RootNav.astro index 2ef3771..f70dd15 100644 --- a/src/components/navigation/sidebars/nav/RootNav.astro +++ b/src/components/navigation/sidebars/nav/RootNav.astro @@ -7,11 +7,11 @@ export interface Props { const { currentRoute, currentModule, currentClass } = Astro.props; -import { generateTypeData } from "@config/io/generateTypeData"; +import { getTypeData } from "@config/io/generateTypeData"; import { groupRoutes } from "@config/io/helpers"; import Tree from "./Tree.astro"; -const routes = await generateTypeData(); +const routes = await getTypeData(); const groupedRoutes = groupRoutes(routes); const configuration = { diff --git a/src/config/io/generateTypeData.ts b/src/config/io/generateTypeData.ts index e789a5f..240150e 100644 --- a/src/config/io/generateTypeData.ts +++ b/src/config/io/generateTypeData.ts @@ -30,7 +30,7 @@ async function readSubdir(subdir: string): Promise { return data; } -export async function generateTypeData(): Promise { +async function generateTypeData(): Promise { const mainDir = import.meta.env.SECRET_MODULES_PATH; if (!mainDir || mainDir == "") { @@ -56,3 +56,13 @@ export async function generateTypeData(): Promise { } return routes; } + +let globalTypeData: Promise; + +export function getTypeData(): Promise { + if (!globalTypeData) { + globalTypeData = generateTypeData(); + } + + return globalTypeData; +} diff --git a/src/pages/docs/types/[type]/[name].astro b/src/pages/docs/types/[type]/[name].astro index 821c224..76d3822 100644 --- a/src/pages/docs/types/[type]/[name].astro +++ b/src/pages/docs/types/[type]/[name].astro @@ -1,7 +1,7 @@ --- import { getQMLTypeLink } from "@config/io/helpers"; import { processMarkdown } from "@config/io/markdown"; -import { generateTypeData } from "@config/io/generateTypeData"; +import { getTypeData } from "@config/io/generateTypeData"; import DocsLayout from "@layouts/DocsLayout.astro"; import TOC from "@components/navigation/sidebars/TOC.astro"; import Properties from "@components/type/Properties.astro"; @@ -11,7 +11,7 @@ import Variants from "@components/type/Variants.astro"; import Badge from "@components/Badge.astro"; export async function getStaticPaths() { - const routes = await generateTypeData(); + const routes = await getTypeData(); return routes .filter(route => route.name !== "index") diff --git a/src/pages/docs/types/[type]/index.astro b/src/pages/docs/types/[type]/index.astro index 8879ed7..cdf191c 100644 --- a/src/pages/docs/types/[type]/index.astro +++ b/src/pages/docs/types/[type]/index.astro @@ -1,10 +1,10 @@ --- import DocsLayout from "@layouts/DocsLayout.astro"; -import { generateTypeData } from "@config/io/generateTypeData"; +import { getTypeData } from "@config/io/generateTypeData"; import { processMarkdown } from "@src/config/io/markdown"; export async function getStaticPaths() { - const routes = await generateTypeData(); + const routes = await getTypeData(); return routes .filter(route => route.name === "index") diff --git a/src/pages/docs/types/index.astro b/src/pages/docs/types/index.astro index 89c0bf6..cda2394 100644 --- a/src/pages/docs/types/index.astro +++ b/src/pages/docs/types/index.astro @@ -1,8 +1,8 @@ --- import DocsLayout from "@layouts/DocsLayout.astro"; -import { generateTypeData } from "@config/io/generateTypeData"; +import { getTypeData } from "@config/io/generateTypeData"; -const routes = await generateTypeData(); +const routes = await getTypeData(); const modules = [...new Set(routes.map(route => route.type))]; ---