Skip to content

Commit 342b4d4

Browse files
authored
feat: add routes into useRuntimeContext (#7130)
1 parent 17d8aee commit 342b4d4

File tree

4 files changed

+21
-4
lines changed

4 files changed

+21
-4
lines changed

.changeset/smooth-lions-pay.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@modern-js/runtime': patch
3+
---
4+
5+
feat: when use convention routes, useRuntimeContext will return routes field, which is the routes of the current app
6+
feat: 当前应用为约定式路由时,useRuntimeContext 支持返回 routes 字段,表示当前应用的完整路由表

packages/runtime/plugin-runtime/src/core/context/runtime.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { StaticHandlerContext } from '@modern-js/runtime-utils/remix-router';
2+
import type { RouteObject } from '@modern-js/runtime-utils/router';
23
import { ROUTE_MANIFEST } from '@modern-js/utils/universal/constants';
34
import { createContext } from 'react';
45
import type { RouteManifest } from '../../router/runtime/types';
@@ -30,14 +31,15 @@ export const RuntimeReactContext = createContext<RuntimeContext>({} as any);
3031
export const ServerRouterContext = createContext({} as any);
3132

3233
// TODO: We should export this context to user as RuntimeContext, use in `init` function
33-
export interface TRuntimeContext extends Partial<BaseRuntimeContext> {
34+
export interface TRuntimeContext {
3435
initialData?: Record<string, unknown>;
3536
isBrowser: boolean;
3637
context: TSSRContext;
3738
/** @deprecated use context.request field instead */
3839
request?: SSRServerContext['request'];
3940
/** @deprecated use context.response field instead */
4041
response?: SSRServerContext['response'];
42+
routes?: RouteObject[];
4143
[key: string]: any;
4244
}
4345

packages/runtime/plugin-runtime/src/router/runtime/plugin.node.tsx

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ import {
88
createStaticRouter,
99
} from '@modern-js/runtime-utils/node/router';
1010
import { createStaticHandler } from '@modern-js/runtime-utils/remix-router';
11-
import { createRoutesFromElements } from '@modern-js/runtime-utils/router';
11+
import {
12+
type RouteObject,
13+
createRoutesFromElements,
14+
} from '@modern-js/runtime-utils/router';
1215
import { time } from '@modern-js/runtime-utils/time';
1316
import { LOADER_REPORTER_NAME } from '@modern-js/utils/universal/constants';
1417
import type React from 'react';
@@ -89,7 +92,7 @@ export const routerPlugin = (
8992

9093
await hooks.onBeforeCreateRoutes.call(context);
9194

92-
let routes = createRoutes
95+
let routes: RouteObject[] = createRoutes
9396
? createRoutes()
9497
: createRoutesFromElements(
9598
renderRoutes({
@@ -153,7 +156,12 @@ export const routerPlugin = (
153156
context.remixRouter = router;
154157

155158
// private api, pass to React Component in `wrapRoot`
156-
context.routes = routes;
159+
Object.defineProperty(context, 'routes', {
160+
get() {
161+
return routes;
162+
},
163+
enumerable: true,
164+
});
157165
});
158166

159167
api.wrapRoot(App => {

packages/runtime/plugin-runtime/src/router/runtime/plugin.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ export const routerPlugin = (
8686
get() {
8787
return routes;
8888
},
89+
enumerable: true,
8990
});
9091
});
9192
api.wrapRoot(App => {

0 commit comments

Comments
 (0)