Skip to content

Commit 8c756c5

Browse files
committed
feat(routes): add hooks in create pages data
1 parent 537104c commit 8c756c5

File tree

2 files changed

+34
-22
lines changed

2 files changed

+34
-22
lines changed

packages/routes/src/PagesService.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
normalizeRoutePath,
1717
toArray,
1818
} from './utils';
19-
import { Page, ResolvedConfig, Route } from './types';
19+
import { Page, PageData, ResolvedConfig, Route } from './types';
2020
import {
2121
PAGE_EXTS,
2222
RESOLVED_PAGES_DATA_MODULE_ID,
@@ -399,19 +399,21 @@ export default routes;
399399
}
400400

401401
async generatePagesDataCode() {
402-
const pagesData = (await this.getPages()).reduce<Record<string, Page>>(
403-
(res, page) => {
404-
// skip layout file and 404 file
405-
if (page.isLayout || page.is404) {
406-
return res;
407-
}
402+
const pages = await this.getPages();
403+
let pagesData: Record<string, PageData> = {};
408404

409-
res[page.routePath] = page;
410-
return res;
411-
},
412-
{}
405+
await Promise.all(
406+
pages
407+
// skip layout file and 404 file
408+
.filter(page => !page.isLayout && !page.is404)
409+
.map(async page => {
410+
pagesData[page.routePath] =
411+
(await this.config.onCreatePageData?.(page)) || page;
412+
})
413413
);
414414

415+
pagesData = (await this.config.onCreatePagesData?.(pagesData)) || pagesData;
416+
415417
return `export const pagesData = ${JSON.stringify(pagesData, null, 2)};
416418
export default pagesData;
417419
`;

packages/routes/src/types.ts

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ export interface Route {
1414
meta?: Record<string, any>;
1515
}
1616

17+
export interface PageData {
18+
basePath: string;
19+
routePath: string;
20+
filePath: string;
21+
meta?: Record<string, any>;
22+
}
23+
1724
export interface PagesConfigItem {
1825
/**
1926
* base route path
@@ -59,21 +66,24 @@ export interface UserConfig {
5966
* Defines files/paths to be ignored.
6067
*/
6168
ignore?: string | string[];
62-
onCreatePage?: (
63-
page: Page
64-
) => Page | null | undefined | Promise<Page | null | undefined>;
65-
onCreatePages?: (
66-
pages: Page[]
67-
) => Page[] | null | undefined | Promise<Page[] | null | undefined>;
68-
onCreateRoute?: (
69-
route: Route
70-
) => Route | null | undefined | Promise<Route | null | undefined>;
69+
onCreatePage?: (page: Page) => Page | void | Promise<Page | void>;
70+
onCreatePages?: (pages: Page[]) => Page[] | void | Promise<Page[] | void>;
71+
onCreateRoute?: (route: Route) => Route | void | Promise<Route | void>;
7172
onCreateRoutes?: (
7273
routes: Route[]
73-
) => Route[] | null | undefined | Promise<Route[] | null | undefined>;
74+
) => Route[] | void | Promise<Route[] | void>;
7475
onGenerateRoutesCode?: (
7576
code: string
76-
) => string | null | undefined | Promise<string | null | undefined>;
77+
) => string | void | Promise<string | void>;
78+
onCreatePageData?: (
79+
pageData: PageData
80+
) => PageData | void | Promise<PageData | void>;
81+
onCreatePagesData?: (
82+
pagesData: Record<string, PageData>
83+
) =>
84+
| Record<string, PageData>
85+
| void
86+
| Promise<Record<string, PageData> | void>;
7787
}
7888

7989
export interface ResolvedConfig extends Omit<UserConfig, 'pages'> {

0 commit comments

Comments
 (0)