diff --git a/.changeset/twelve-monkeys-turn.md b/.changeset/twelve-monkeys-turn.md new file mode 100644 index 000000000000..eaea6c3f8cc3 --- /dev/null +++ b/.changeset/twelve-monkeys-turn.md @@ -0,0 +1,7 @@ +--- +'@modern-js/runtime': patch +--- + +feat: router plugin add beforeCreateRoutes hook + +feat: router 插件添加 beforeCreateRoutes 钩子 diff --git a/packages/runtime/plugin-runtime/src/router/runtime/hooks.ts b/packages/runtime/plugin-runtime/src/router/runtime/hooks.ts index 7a548ca0ac23..5ab2914df85d 100644 --- a/packages/runtime/plugin-runtime/src/router/runtime/hooks.ts +++ b/packages/runtime/plugin-runtime/src/router/runtime/hooks.ts @@ -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(); +const beforeCreateRoutes = createAsyncInterruptWorkflow(); -export { modifyRoutes }; +export { modifyRoutes, beforeCreateRoutes }; diff --git a/packages/runtime/plugin-runtime/src/router/runtime/plugin.node.tsx b/packages/runtime/plugin-runtime/src/router/runtime/plugin.node.tsx index d651efcd04f1..12fddcede5f0 100644 --- a/packages/runtime/plugin-runtime/src/router/runtime/plugin.node.tsx +++ b/packages/runtime/plugin-runtime/src/router/runtime/plugin.node.tsx @@ -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'; @@ -41,6 +44,7 @@ export const routerPlugin = ( name: '@modern-js/plugin-router', registerHook: { modifyRoutes: modifyRoutesHook, + beforeCreateRoutes: beforeCreateRoutesHook, }, setup: api => { let finalRouteConfig: any = {}; @@ -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() @@ -93,7 +100,6 @@ export const routerPlugin = ( }), ); - const runner = (api as any).useHookRunners(); routes = runner.modifyRoutes(routes); const { query } = createStaticHandler(routes, {