Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(router): lazy load markdown routes #233

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 8 additions & 18 deletions packages/router/src/lib/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,9 @@ const FILES = import.meta.glob<RouteExport>([
'/src/app/routes/**/*.ts',
]);

const CONTENT_FILES_GLOB = import.meta.glob<RouteExport>(
['/src/app/routes/**/*.md'],
{ as: 'raw', eager: true }
);

const CONTENT_FILES: Record<string, () => Promise<RouteExport>> = 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<string>(['/src/app/routes/**/*.md'], {
as: 'raw',
});

/**
* Function used to parse list of files and return
Expand All @@ -33,20 +21,22 @@ const CONTENT_FILES: Record<string, () => Promise<RouteExport>> = Object.keys(
* @param files
* @returns Array of routes
*/
export function getRoutes(files: Record<string, () => Promise<RouteExport>>) {
export function getRoutes(
files: Record<string, () => Promise<RouteExport | string>>
) {
const ROUTES = Object.keys(files).sort((a, b) => a.length - b.length);

const routeConfigs = ROUTES.reduce<Route[]>(
(routes: Route[], key: string) => {
const module = key.endsWith('.md')
const module: () => Promise<RouteExport> = key.endsWith('.md')
? () =>
import('@analogjs/content').then((m) => ({
default: m.MarkdownComponent,
routeMeta: {
data: { _analogContent: files[key] },
},
}))
: files[key];
: (files[key] as () => Promise<RouteExport>);

const segments = key
.replace(/^\/(.*?)\/routes|\/app\/routes|\.(js|ts|md)$/g, '')
Expand Down