Skip to content

Commit

Permalink
feat: add hook modifyRoutes (#3459)
Browse files Browse the repository at this point in the history
* feat: add hook `modifyRoutes`

* chore: changeset
  • Loading branch information
xuchaobei committed Apr 17, 2023
1 parent fefd1c5 commit afe5d35
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 2 deletions.
6 changes: 6 additions & 0 deletions .changeset/lovely-beans-begin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@modern-js/runtime': patch
---

feat: add hook `modifyRoutes`
feat: 增加 hook `modifyRoutes`
6 changes: 6 additions & 0 deletions packages/runtime/plugin-runtime/src/router/runtime/hooks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { createWaterfall } from '@modern-js/plugin';
import { RouteObject } from 'react-router-dom';

const modifyRoutes = createWaterfall<RouteObject[]>();

export { modifyRoutes };
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { SSRServerContext } from '../../ssr/serverRender/types';
import type { RouteManifest, RouterConfig } from './types';
import { renderRoutes, urlJoin } from './utils';
import { installGlobals } from './fetch';
import { modifyRoutes as modifyRoutesHook } from './hooks';

// Polyfill Web Fetch API
installGlobals();
Expand Down Expand Up @@ -69,7 +70,10 @@ export const routerPlugin = ({
}: RouterConfig): Plugin => {
return {
name: '@modern-js/plugin-router',
setup: () => {
registerHook: {
modifyRoutes: modifyRoutesHook,
},
setup: api => {
return {
async init({ context }, next) {
// can not get routes config, skip wrapping React Router.
Expand Down Expand Up @@ -111,6 +115,10 @@ export const routerPlugin = ({
// set routeManifest in context to be consistent with csr context
context.routeManifest = context.ssrContext!
.routeManifest as RouteManifest;

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

return next({ context });
},
hoc: ({ App }, next) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
} from 'react-router-dom';
import hoistNonReactStatics from 'hoist-non-react-statics';
import { Plugin } from '../../core';
import { modifyRoutes as modifyRoutesHook } from './hooks';
import { deserializeErrors, renderRoutes, urlJoin } from './utils';
import type { RouterConfig, Routes } from './types';

Expand Down Expand Up @@ -45,7 +46,10 @@ export const routerPlugin = ({
finalRouteConfig = routesConfig;
return {
name: '@modern-js/plugin-router',
setup: () => {
registerHook: {
modifyRoutes: modifyRoutesHook,
},
setup: api => {
return {
init({ context }, next) {
context.router = {
Expand Down Expand Up @@ -80,6 +84,9 @@ export const routerPlugin = ({
}),
);

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

const baseUrl =
window._SERVER_DATA?.router.baseUrl ||
select(location.pathname);
Expand Down
47 changes: 47 additions & 0 deletions packages/runtime/plugin-runtime/tests/router/router.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -216,5 +216,52 @@ describe('@modern-js/plugin-router', () => {
const { container } = render(<DefaultNotFound />);
expect(container.firstChild?.textContent).toEqual('404');
});

it('modifyRoutes hook', async () => {
const AppWrapper = createApp({
plugins: [
createPlugin(
() =>
({
modifyRoutes: (routes: any) => {
routes[0].element = <App2>{routes[0].element}</App2>;
return routes;
},
} as any),
),
createRouterPlugin({
routesConfig: {
routes: [
{
path: '/',
component: App1 as any,
type: 'nested',
_component: '',
},
],
},
}),
],
})();

function App1() {
return <div>App1</div>;
}

function App2(props: any) {
const { children } = props;
return (
<div>
<div> App2 </div>
<div>{children}</div>
</div>
);
}

const { container } = render(<AppWrapper />);
await waitFor(() => {
expect(container.innerHTML).toMatch('App2');
});
});
});
/* eslint-enable @typescript-eslint/ban-ts-comment */

0 comments on commit afe5d35

Please sign in to comment.