Skip to content

Commit

Permalink
refactor: useRouteMeta fully use getRouteMetaById (#1889)
Browse files Browse the repository at this point in the history
* fix: useRouteMeta fully use getRouteMetaById

* refactor: runtime get frontmatter directly

* refactor: use sync frontmatter
  • Loading branch information
MadCcc committed Sep 13, 2023
1 parent baec225 commit ff343de
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 25 deletions.
29 changes: 13 additions & 16 deletions src/client/theme-api/useRouteMeta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import {
useLocation,
useRouteData,
} from 'dumi';
import { useCallback, useState } from 'react';
import { useMemo } from 'react';
import use from './context/use';
import type { IRouteMeta } from './types';
import { useIsomorphicLayoutEffect } from './utils';

const cache = new Map<string, ReturnType<getRouteMetaById>>();

Expand All @@ -28,28 +27,26 @@ export const useRouteMeta = () => {
const { pathname } = useLocation();
const { clientRoutes } = useAppData();

const routeMeta = useAsyncRouteMeta(route.id);

const getter = useCallback((): IRouteMeta => {
let ret: IRouteMeta;

const curRoute = useMemo(() => {
if (route.path === pathname && !('isLayout' in route)) {
// use `useRouteData` result if matched, for performance
ret = routeMeta;
return route;
} else {
// match manually for dynamic route & layout component
const matched = matchRoutes(clientRoutes, pathname)?.pop();

ret = (matched?.route as any)?.meta;
return matched?.route;
}

return ret || { frontmatter: {}, toc: [], texts: [] };
}, [clientRoutes.length, pathname]);
const [meta, setMeta] = useState<IRouteMeta>(getter);

useIsomorphicLayoutEffect(() => {
setMeta(getter);
}, [clientRoutes.length, pathname]);
const meta: IRouteMeta = useAsyncRouteMeta((curRoute as any)?.id) || {
frontmatter: {},
toc: [],
texts: [],
};

if (curRoute && 'meta' in curRoute && (curRoute.meta as any)._atom_route) {
meta._atom_route = true;
}

return meta;
};
2 changes: 1 addition & 1 deletion src/features/compile/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ export default (api: IApi) => {
.rule('dumi-page')
.type('javascript/auto')
.test(/\.(j|t)sx?$/)
.resourceQuery(/meta$/)
.resourceQuery(/(meta|frontmatter)$/)
.use('page-meta-loader')
.loader(require.resolve('../../loaders/page'));

Expand Down
9 changes: 9 additions & 0 deletions src/features/meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ export default (api: IApi) => {
},
});

api.writeTmpFile({
noPluginDir: true,
path: 'dumi/meta/frontmatter.ts',
tplPath: winPath(join(TEMPLATES_DIR, 'meta-frontmatter.ts.tpl')),
context: {
metaFiles: parsedMetaFiles,
},
});

// generate meta lazy entry
const mdFiles = parsedMetaFiles.filter((metaFile) =>
metaFile.file.endsWith('.md'),
Expand Down
11 changes: 11 additions & 0 deletions src/templates/meta-frontmatter.ts.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{{#metaFiles}}
import { frontmatter as fm{{{index}}} } from '{{{file}}}?type=frontmatter';
{{/metaFiles}}

export const filesFrontmatter = {
{{#metaFiles}}
'{{{id}}}': {
frontmatter: fm{{{index}}},
},
{{/metaFiles}}
}
4 changes: 2 additions & 2 deletions src/templates/meta-route.ts.tpl
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { tabs } from './tabs';
import { filesFrontmatter } from './frontmatter';

const files = {
{{#metaFiles}}
'{{{id}}}': {
frontmatterGetter: () => import('{{{file}}}?type=frontmatter'),
textGetter: () => import('{{{file}}}?type=text'),
{{#tabs}}
tabs: {{{tabs}}},
Expand All @@ -20,7 +20,7 @@ export const getRouteMetaById = async (id: string) => {
}

const text = await file.textGetter();
const frontmatter = await file.frontmatterGetter();
const frontmatter = filesFrontmatter[id];

const tabsMeta = file.tabs && await Promise.all(file.tabs.map(async (tab) => {
const meta = await getRouteMetaById(tab) ?? {
Expand Down
13 changes: 7 additions & 6 deletions src/templates/meta-runtime.ts.tpl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { warning } from 'rc-util';
import { filesMeta, tabs } from '.';
import { tabs } from './tabs';
import { filesFrontmatter } from './frontmatter';
import deepmerge from '{{{deepmerge}}}';

// Proxy do not warning since `Object.keys` will get nothing to loop
Expand All @@ -14,20 +15,20 @@ function wrapEmpty(meta, fieldName, defaultValue) {

export const patchRoutes = ({ routes }) => {
Object.values(routes).forEach((route) => {
if (filesMeta[route.id]) {
if (process.env.NODE_ENV === 'production' && (route.meta?.frontmatter?.debug || filesMeta[route.id].frontmatter.debug)) {
if (filesFrontmatter[route.id]) {
if (process.env.NODE_ENV === 'production' && (route.meta?.frontmatter?.debug || filesFrontmatter[route.id].frontmatter.debug)) {
// hide route in production which set hide frontmatter
delete routes[route.id];
} else {
// merge meta to route object
route.meta = deepmerge(route.meta, filesMeta[route.id]);
route.meta = deepmerge(route.meta, filesFrontmatter[route.id]);
wrapEmpty(route.meta, 'demos', {});
wrapEmpty(route.meta, 'texts', []);

// apply real tab data from id
route.meta.tabs = route.meta.tabs?.map((id) => {
const meta = filesMeta[id] || {
const meta = filesFrontmatter[id] || {
frontmatter: { title: tabs[id].title },
toc: [],
texts: [],
Expand All @@ -44,4 +45,4 @@ export const patchRoutes = ({ routes }) => {
}
}
});
}
}

0 comments on commit ff343de

Please sign in to comment.