Skip to content

Commit

Permalink
feat: router plugin add beforeCreateRoutes hook (#6417)
Browse files Browse the repository at this point in the history
  • Loading branch information
caohuilin authored Oct 22, 2024
1 parent 679bdd9 commit 03625a8
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
7 changes: 7 additions & 0 deletions .changeset/twelve-monkeys-turn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@modern-js/runtime': patch
---

feat: router plugin add beforeCreateRoutes hook

feat: router 插件添加 beforeCreateRoutes 钩子
9 changes: 7 additions & 2 deletions packages/runtime/plugin-runtime/src/router/runtime/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { createWaterfall } from '@modern-js/plugin';
import {
createAsyncInterruptWorkflow,
createWaterfall,
} from '@modern-js/plugin';
import type { RouteObject } from '@modern-js/runtime-utils/router';
import type { RuntimeContext } from '../../core';

const modifyRoutes = createWaterfall<RouteObject[]>();
const beforeCreateRoutes = createAsyncInterruptWorkflow<RuntimeContext, void>();

export { modifyRoutes };
export { modifyRoutes, beforeCreateRoutes };
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ import { RuntimeReactContext } from '../../core';
import type { Plugin } from '../../core';
import { getGlobalLayoutApp, getGlobalRoutes } from '../../core/context';
import DeferredDataScripts from './DeferredDataScripts.node';
import { modifyRoutes as modifyRoutesHook } from './hooks';
import {
beforeCreateRoutes as beforeCreateRoutesHook,
modifyRoutes as modifyRoutesHook,
} from './hooks';
import type { RouterConfig } from './types';
import { renderRoutes, urlJoin } from './utils';

Expand All @@ -41,6 +44,7 @@ export const routerPlugin = (
name: '@modern-js/plugin-router',
registerHook: {
modifyRoutes: modifyRoutesHook,
beforeCreateRoutes: beforeCreateRoutesHook,
},
setup: api => {
let finalRouteConfig: any = {};
Expand Down Expand Up @@ -79,6 +83,9 @@ export const routerPlugin = (
context.ssrContext?.loaderContext,
);
requestContext.set(reporterCtx, reporter);
const runner = (api as any).useHookRunners();

await runner.beforeCreateRoutes(context);

let routes = createRoutes
? createRoutes()
Expand All @@ -93,7 +100,6 @@ export const routerPlugin = (
}),
);

const runner = (api as any).useHookRunners();
routes = runner.modifyRoutes(routes);

const { query } = createStaticHandler(routes, {
Expand Down

0 comments on commit 03625a8

Please sign in to comment.