From 3b53c2aaec7a522c60f161fefe8adaa83de7beb2 Mon Sep 17 00:00:00 2001 From: markostanimirovic Date: Wed, 25 Jan 2023 12:47:23 +0100 Subject: [PATCH] fix(router): lazy load markdown routes --- packages/router/src/lib/routes.ts | 26 ++++++++------------------ 1 file changed, 8 insertions(+), 18 deletions(-) diff --git a/packages/router/src/lib/routes.ts b/packages/router/src/lib/routes.ts index 07fc3e84e..8802efcda 100644 --- a/packages/router/src/lib/routes.ts +++ b/packages/router/src/lib/routes.ts @@ -10,21 +10,9 @@ const FILES = import.meta.glob([ '/src/app/routes/**/*.ts', ]); -const CONTENT_FILES_GLOB = import.meta.glob( - ['/src/app/routes/**/*.md'], - { as: 'raw', eager: true } -); - -const CONTENT_FILES: Record Promise> = Object.keys( - CONTENT_FILES_GLOB -).reduce((curr, key) => { - curr = { - ...curr, - [key]: () => Promise.resolve(CONTENT_FILES_GLOB[key]), - }; - - return curr; -}, {}); +const CONTENT_FILES = import.meta.glob(['/src/app/routes/**/*.md'], { + as: 'raw', +}); /** * Function used to parse list of files and return @@ -33,12 +21,14 @@ const CONTENT_FILES: Record Promise> = Object.keys( * @param files * @returns Array of routes */ -export function getRoutes(files: Record Promise>) { +export function getRoutes( + files: Record Promise> +) { const ROUTES = Object.keys(files).sort((a, b) => a.length - b.length); const routeConfigs = ROUTES.reduce( (routes: Route[], key: string) => { - const module = key.endsWith('.md') + const module: () => Promise = key.endsWith('.md') ? () => import('@analogjs/content').then((m) => ({ default: m.MarkdownComponent, @@ -46,7 +36,7 @@ export function getRoutes(files: Record Promise>) { data: { _analogContent: files[key] }, }, })) - : files[key]; + : (files[key] as () => Promise); const segments = key .replace(/^\/(.*?)\/routes|\/app\/routes|\.(js|ts|md)$/g, '')