@bleedingdev/modern-js-runtime 3.5.0-ultramodern.3 → 3.5.0-ultramodern.31
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/cli/ssr/index.js +1 -2
- package/dist/cjs/core/context/public.js +49 -0
- package/dist/cjs/core/context/runtime.js +6 -8
- package/dist/cjs/core/react/wrapper.js +57 -0
- package/dist/cjs/core/server/federatedCss.js +7 -3
- package/dist/cjs/core/server/requestHandler.js +15 -45
- package/dist/cjs/core/server/requestResponse.js +83 -0
- package/dist/cjs/core/server/routerCleanup.js +21 -0
- package/dist/cjs/core/server/scriptOrder.js +13 -7
- package/dist/cjs/core/server/string/loadable.js +3 -11
- package/dist/cjs/index.js +7 -6
- package/dist/cjs/router/cli/code/templates.js +3 -2
- package/dist/cjs/router/runtime/hooks.js +7 -6
- package/dist/cjs/router/runtime/provider.js +1 -1
- package/dist/esm/cli/ssr/index.mjs +1 -2
- package/dist/esm/core/context/public.mjs +5 -0
- package/dist/esm/core/context/runtime.mjs +4 -6
- package/dist/esm/core/react/wrapper.mjs +58 -1
- package/dist/esm/core/server/federatedCss.mjs +7 -3
- package/dist/esm/core/server/requestHandler.mjs +15 -45
- package/dist/esm/core/server/requestResponse.mjs +39 -0
- package/dist/esm/core/server/routerCleanup.mjs +16 -1
- package/dist/esm/core/server/scriptOrder.mjs +11 -8
- package/dist/esm/core/server/string/loadable.mjs +3 -8
- package/dist/esm/index.mjs +2 -1
- package/dist/esm/router/cli/code/templates.mjs +3 -2
- package/dist/esm/router/runtime/hooks.mjs +7 -6
- package/dist/esm/router/runtime/provider.mjs +1 -1
- package/dist/esm-node/cli/ssr/index.mjs +1 -2
- package/dist/esm-node/core/context/public.mjs +6 -0
- package/dist/esm-node/core/context/runtime.mjs +4 -6
- package/dist/esm-node/core/react/wrapper.mjs +58 -1
- package/dist/esm-node/core/server/federatedCss.mjs +7 -3
- package/dist/esm-node/core/server/requestHandler.mjs +15 -45
- package/dist/esm-node/core/server/requestResponse.mjs +40 -0
- package/dist/esm-node/core/server/routerCleanup.mjs +16 -1
- package/dist/esm-node/core/server/scriptOrder.mjs +11 -8
- package/dist/esm-node/core/server/string/loadable.mjs +3 -8
- package/dist/esm-node/index.mjs +2 -1
- package/dist/esm-node/router/cli/code/templates.mjs +3 -2
- package/dist/esm-node/router/runtime/hooks.mjs +7 -6
- package/dist/esm-node/router/runtime/provider.mjs +1 -1
- package/dist/types/boundary-debugger/index.d.ts +1 -1
- package/dist/types/common.d.ts +8 -1
- package/dist/types/core/context/index.d.ts +25 -9
- package/dist/types/core/context/monitors/default.d.ts +10 -2
- package/dist/types/core/context/monitors/index.d.ts +1 -1
- package/dist/types/core/context/monitors/index.server.d.ts +2 -2
- package/dist/types/core/context/public.d.ts +26 -0
- package/dist/types/core/context/runtime.d.ts +12 -22
- package/dist/types/core/react/index.d.ts +1 -1
- package/dist/types/core/server/requestResponse.d.ts +15 -0
- package/dist/types/core/server/routerCleanup.d.ts +2 -0
- package/dist/types/core/server/scriptOrder.d.ts +1 -0
- package/dist/types/core/server/stream/shared.d.ts +2 -2
- package/dist/types/core/server/string/loadable.d.ts +0 -2
- package/dist/types/index.d.ts +5 -5
- package/dist/types/internal.d.ts +0 -1
- package/dist/types/router/runtime/hooks.d.ts +10 -6
- package/dist/types/router/runtime/internal.d.ts +1 -1
- package/dist/types/router/runtime/plugin.d.ts +1 -1
- package/dist/types/router/runtime/plugin.node.d.ts +1 -1
- package/dist/types/router/runtime/provider.d.ts +1 -1
- package/dist/types/router/runtime/types.d.ts +27 -5
- package/package.json +8 -8
|
@@ -3,15 +3,13 @@ import { jsx } from "react/jsx-runtime";
|
|
|
3
3
|
import { storage } from "@modern-js/runtime-utils/node";
|
|
4
4
|
import { getPathname, parseCookie, parseHeaders, parseQuery } from "@modern-js/runtime-utils/universal/request";
|
|
5
5
|
import react, { Fragment } from "react";
|
|
6
|
-
import { getRouterServerSnapshot } from "../../router/runtime/lifecycle.mjs";
|
|
7
|
-
import { handleRSCRedirect } from "../../router/runtime/rsc-router.mjs";
|
|
8
6
|
import { getGlobalInternalRuntimeContext, getGlobalRSCRoot } from "../context/index.mjs";
|
|
9
7
|
import { getInitialContext } from "../context/runtime.mjs";
|
|
10
8
|
import { getServerPayload } from "../context/serverPayload/index.mjs";
|
|
11
9
|
import { createRoot } from "../react/index.mjs";
|
|
12
10
|
import { CHUNK_CSS_PLACEHOLDER } from "./constants.mjs";
|
|
13
|
-
import {
|
|
14
|
-
import {
|
|
11
|
+
import { applyRouterSnapshotResult, createLoaderRedirectResponse, finalizeRenderResponse } from "./requestResponse.mjs";
|
|
12
|
+
import { createRouterCleanup, finishWithRouterCleanup, runWithRouterCleanupOnError } from "./routerCleanup.mjs";
|
|
15
13
|
import { getSSRConfigByEntry, getSSRMode } from "./utils.mjs";
|
|
16
14
|
async function handleRSCRequest(request, Root, context, options, handleRequest) {
|
|
17
15
|
const serverPayload = getServerPayload();
|
|
@@ -31,29 +29,6 @@ async function handleRSCRequest(request, Root, context, options, handleRequest)
|
|
|
31
29
|
runtimeContext: context
|
|
32
30
|
});
|
|
33
31
|
}
|
|
34
|
-
const isRedirectStatus = (status)=>status >= 300 && status <= 399;
|
|
35
|
-
const processRedirect = (headers, status, ctx)=>{
|
|
36
|
-
if (ctx.enableRsc && ctx.isRSCNavigation) return handleRSCRedirect(headers, ctx.basename, status);
|
|
37
|
-
return new Response(null, {
|
|
38
|
-
status,
|
|
39
|
-
headers
|
|
40
|
-
});
|
|
41
|
-
};
|
|
42
|
-
const applyRouterSnapshotResult = (context, onError)=>{
|
|
43
|
-
const routerServerSnapshot = getRouterServerSnapshot(context);
|
|
44
|
-
const routerStatusCode = routerServerSnapshot?.statusCode ?? context.routerContext?.statusCode;
|
|
45
|
-
if (routerStatusCode && 200 !== routerStatusCode) context.ssrContext?.response.status(routerStatusCode);
|
|
46
|
-
const errors = Object.values(routerServerSnapshot?.errors || context.routerContext?.errors || {});
|
|
47
|
-
if (errors.length > 0) onError(errors[0], SSRErrors.LOADER_ERROR);
|
|
48
|
-
};
|
|
49
|
-
const createLoaderRedirectResponse = (beforeRenderResult, redirectCtx)=>{
|
|
50
|
-
if (!beforeRenderResult || !isRedirectStatus(beforeRenderResult.status)) return;
|
|
51
|
-
if (beforeRenderResult.headers.has('X-Modernjs-Redirect')) return beforeRenderResult;
|
|
52
|
-
const redirectUrl = beforeRenderResult.headers.get('Location') || '/';
|
|
53
|
-
return processRedirect(new Headers({
|
|
54
|
-
Location: redirectUrl
|
|
55
|
-
}), beforeRenderResult.status, redirectCtx);
|
|
56
|
-
};
|
|
57
32
|
const renderRequest = async (request, Root, context, options, handleRequest, enableRsc)=>{
|
|
58
33
|
if (enableRsc) return handleRSCRequest(request, Root, context, options, handleRequest);
|
|
59
34
|
return handleRequest(request, Root, {
|
|
@@ -61,17 +36,6 @@ const renderRequest = async (request, Root, context, options, handleRequest, ena
|
|
|
61
36
|
runtimeContext: context
|
|
62
37
|
});
|
|
63
38
|
};
|
|
64
|
-
const finalizeRenderResponse = (response, responseProxy, redirectCtx, routerCleanup)=>{
|
|
65
|
-
if (-1 !== responseProxy.status && isRedirectStatus(responseProxy.status) && responseProxy.headers.Location) return processRedirect(new Headers(responseProxy.headers), responseProxy.status, redirectCtx);
|
|
66
|
-
Object.entries(responseProxy.headers).forEach(([key, value])=>{
|
|
67
|
-
response.headers.set(key, value);
|
|
68
|
-
});
|
|
69
|
-
if (-1 !== responseProxy.status) return routerCleanup.deferUntilBodyDone(new Response(response.body, {
|
|
70
|
-
status: responseProxy.status,
|
|
71
|
-
headers: response.headers
|
|
72
|
-
}));
|
|
73
|
-
return routerCleanup.deferUntilBodyDone(response);
|
|
74
|
-
};
|
|
75
39
|
function createSSRContext(request, options) {
|
|
76
40
|
const { config, loaderContext, onError, onTiming, locals, resource, params, responseProxy, reporter } = options;
|
|
77
41
|
const { nonce, useJsonScript } = config;
|
|
@@ -167,20 +131,26 @@ const createRequestHandler = async (handleRequest, createRequestOptions)=>{
|
|
|
167
131
|
isRSCNavigation: 'true' === request.headers.get('x-rsc-tree'),
|
|
168
132
|
basename: ssrContext.baseUrl || '/'
|
|
169
133
|
};
|
|
170
|
-
|
|
171
|
-
|
|
134
|
+
const routerCleanup = createRouterCleanup(context, options.onError);
|
|
135
|
+
const beforeRenderResult = await runWithRouterCleanupOnError(routerCleanup, ()=>runBeforeRender(context));
|
|
136
|
+
await runWithRouterCleanupOnError(routerCleanup, ()=>{
|
|
172
137
|
applyRouterSnapshotResult(context, options.onError);
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
138
|
+
});
|
|
139
|
+
if ("u" > typeof Response) {
|
|
140
|
+
const redirectResponse = await runWithRouterCleanupOnError(routerCleanup, ()=>createLoaderRedirectResponse(beforeRenderResult, redirectCtx));
|
|
141
|
+
if (redirectResponse) {
|
|
142
|
+
await routerCleanup.run();
|
|
143
|
+
return redirectResponse;
|
|
176
144
|
}
|
|
145
|
+
}
|
|
146
|
+
await runWithRouterCleanupOnError(routerCleanup, ()=>{
|
|
177
147
|
if (!createRequestOptions?.enableRsc) {
|
|
178
148
|
const { htmlTemplate } = options.resource;
|
|
179
149
|
options.resource.htmlTemplate = htmlTemplate.replace('</head>', `${CHUNK_CSS_PLACEHOLDER}</head>`);
|
|
180
150
|
}
|
|
181
|
-
const response = await renderRequest(request, Root, context, options, handleRequest, !!createRequestOptions?.enableRsc);
|
|
182
|
-
return finalizeRenderResponse(response, responseProxy, redirectCtx, routerCleanup);
|
|
183
151
|
});
|
|
152
|
+
const response = await runWithRouterCleanupOnError(routerCleanup, ()=>renderRequest(request, Root, context, options, handleRequest, !!createRequestOptions?.enableRsc));
|
|
153
|
+
return finishWithRouterCleanup(routerCleanup, ()=>finalizeRenderResponse(response, responseProxy, redirectCtx, routerCleanup));
|
|
184
154
|
});
|
|
185
155
|
};
|
|
186
156
|
return requestHandler;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import "node:module";
|
|
2
|
+
import { getRouterServerSnapshot } from "../../router/runtime/lifecycle.mjs";
|
|
3
|
+
import { handleRSCRedirect } from "../../router/runtime/rsc-router.mjs";
|
|
4
|
+
import { SSRErrors } from "./tracer.mjs";
|
|
5
|
+
const isRedirectStatus = (status)=>status >= 300 && status <= 399;
|
|
6
|
+
const isNullBodyStatus = (status)=>204 === status || 205 === status || 304 === status;
|
|
7
|
+
const processRedirect = (headers, status, ctx)=>{
|
|
8
|
+
if (ctx.enableRsc && ctx.isRSCNavigation) return handleRSCRedirect(headers, ctx.basename, status);
|
|
9
|
+
return new Response(null, {
|
|
10
|
+
status,
|
|
11
|
+
headers
|
|
12
|
+
});
|
|
13
|
+
};
|
|
14
|
+
const applyRouterSnapshotResult = (context, onError)=>{
|
|
15
|
+
const routerServerSnapshot = getRouterServerSnapshot(context);
|
|
16
|
+
const routerStatusCode = routerServerSnapshot?.statusCode ?? context.routerContext?.statusCode;
|
|
17
|
+
if (routerStatusCode && 200 !== routerStatusCode) context.ssrContext?.response.status(routerStatusCode);
|
|
18
|
+
const errors = Object.values(routerServerSnapshot?.errors || context.routerContext?.errors || {});
|
|
19
|
+
if (errors.length > 0) onError(errors[0], SSRErrors.LOADER_ERROR);
|
|
20
|
+
};
|
|
21
|
+
const createLoaderRedirectResponse = (beforeRenderResult, redirectCtx)=>{
|
|
22
|
+
if (!beforeRenderResult || !isRedirectStatus(beforeRenderResult.status)) return;
|
|
23
|
+
if (beforeRenderResult.headers.has('X-Modernjs-Redirect')) return beforeRenderResult;
|
|
24
|
+
const redirectUrl = beforeRenderResult.headers.get('Location') || '/';
|
|
25
|
+
return processRedirect(new Headers({
|
|
26
|
+
Location: redirectUrl
|
|
27
|
+
}), beforeRenderResult.status, redirectCtx);
|
|
28
|
+
};
|
|
29
|
+
const finalizeRenderResponse = (response, responseProxy, redirectCtx, routerCleanup)=>{
|
|
30
|
+
if (-1 !== responseProxy.status && isRedirectStatus(responseProxy.status) && responseProxy.headers.Location) return processRedirect(new Headers(responseProxy.headers), responseProxy.status, redirectCtx);
|
|
31
|
+
Object.entries(responseProxy.headers).forEach(([key, value])=>{
|
|
32
|
+
response.headers.set(key, value);
|
|
33
|
+
});
|
|
34
|
+
if (-1 !== responseProxy.status) return routerCleanup.deferUntilBodyDone(new Response(isNullBodyStatus(responseProxy.status) ? null : response.body, {
|
|
35
|
+
status: responseProxy.status,
|
|
36
|
+
headers: response.headers
|
|
37
|
+
}));
|
|
38
|
+
return routerCleanup.deferUntilBodyDone(response);
|
|
39
|
+
};
|
|
40
|
+
export { applyRouterSnapshotResult, createLoaderRedirectResponse, finalizeRenderResponse };
|
|
@@ -9,6 +9,21 @@ async function withRouterCleanup(runtimeContext, onError, callback) {
|
|
|
9
9
|
if (!routerCleanup.deferred) await routerCleanup.run();
|
|
10
10
|
}
|
|
11
11
|
}
|
|
12
|
+
async function runWithRouterCleanupOnError(routerCleanup, callback) {
|
|
13
|
+
try {
|
|
14
|
+
return await callback();
|
|
15
|
+
} catch (error) {
|
|
16
|
+
await routerCleanup.run();
|
|
17
|
+
throw error;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
async function finishWithRouterCleanup(routerCleanup, callback) {
|
|
21
|
+
try {
|
|
22
|
+
return await callback();
|
|
23
|
+
} finally{
|
|
24
|
+
if (!routerCleanup.deferred) await routerCleanup.run();
|
|
25
|
+
}
|
|
26
|
+
}
|
|
12
27
|
function createRouterCleanup(runtimeContext, onError) {
|
|
13
28
|
let deferred = false;
|
|
14
29
|
let finished = false;
|
|
@@ -64,4 +79,4 @@ function createRouterCleanup(runtimeContext, onError) {
|
|
|
64
79
|
deferUntilBodyDone
|
|
65
80
|
};
|
|
66
81
|
}
|
|
67
|
-
export { ROUTER_CLEANUP_ERROR, createRouterCleanup, withRouterCleanup };
|
|
82
|
+
export { ROUTER_CLEANUP_ERROR, createRouterCleanup, finishWithRouterCleanup, runWithRouterCleanupOnError, withRouterCleanup };
|
|
@@ -43,6 +43,10 @@ function getMatchedRouteAssets(runtimeContext, routeManifest) {
|
|
|
43
43
|
const matchedRouteIds = getRouterMatchedRouteIds(runtimeContext) ?? [];
|
|
44
44
|
return matchedRouteIds.flatMap((routeId)=>routeAssets[routeId]?.assets ?? []);
|
|
45
45
|
}
|
|
46
|
+
function getMatchedRouteChunks(runtimeContext, routeManifest, routeAssetToChunk) {
|
|
47
|
+
if (!routeManifest) return [];
|
|
48
|
+
return getMatchedRouteAssets(runtimeContext, routeManifest).map(routeAssetToChunk);
|
|
49
|
+
}
|
|
46
50
|
const orderHydrationScriptChunks = ({ asyncEntryChunks, collectedChunks, matchedRouteChunks, entryName })=>{
|
|
47
51
|
const asyncEntryScriptChunks = [];
|
|
48
52
|
const asyncEntryDependencyChunks = [];
|
|
@@ -73,13 +77,12 @@ function createRouteHydrationScriptTags(runtimeContext, entryName, options = {})
|
|
|
73
77
|
const { nonce, template } = options;
|
|
74
78
|
const routeAssets = getRouteAssets(runtimeContext);
|
|
75
79
|
if (void 0 === routeAssets) return '';
|
|
76
|
-
const
|
|
77
|
-
const
|
|
78
|
-
...
|
|
79
|
-
routeAssets[`async-${entryName}`]
|
|
80
|
-
].filter((
|
|
81
|
-
const jsAssets = Array.from(new Set(assetEntries.flatMap((entry)=>(entry.assets ?? []).filter((asset)=>asset.endsWith('.js')))));
|
|
80
|
+
const existingScriptSrcs = void 0 === template ? new Set() : new Set(getScriptTags(template).map(({ src })=>src));
|
|
81
|
+
const jsAssets = Array.from(new Set([
|
|
82
|
+
...getMatchedRouteAssets(runtimeContext),
|
|
83
|
+
...routeAssets[`async-${entryName}`]?.assets ?? []
|
|
84
|
+
].filter((asset)=>asset.endsWith('.js'))));
|
|
82
85
|
const nonceAttr = void 0 === nonce || '' === nonce ? '' : ` nonce="${nonce}"`;
|
|
83
|
-
return jsAssets.filter((asset)
|
|
86
|
+
return jsAssets.filter((asset)=>!existingScriptSrcs.has(asset)).map((asset)=>`<script src=${asset}${nonceAttr}></script>`).join(' ');
|
|
84
87
|
}
|
|
85
|
-
export { createRouteHydrationScriptTags, getMatchedRouteAssets, injectBeforeHydrationEntryScript, orderHydrationScriptChunks, replaceChunkJsPlaceholder };
|
|
88
|
+
export { createRouteHydrationScriptTags, getMatchedRouteAssets, getMatchedRouteChunks, injectBeforeHydrationEntryScript, orderHydrationScriptChunks, replaceChunkJsPlaceholder };
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import "node:module";
|
|
2
2
|
import { ChunkExtractor } from "@loadable/server";
|
|
3
3
|
import { createFederatedCssLinks } from "../federatedCss.mjs";
|
|
4
|
-
import {
|
|
4
|
+
import { getMatchedRouteChunks, orderHydrationScriptChunks } from "../scriptOrder.mjs";
|
|
5
5
|
import { attributesToString, checkIsNode, hasStylesheetLink } from "../utils.mjs";
|
|
6
6
|
import { fileURLToPath as __rspack_fileURLToPath } from "node:url";
|
|
7
7
|
import { dirname as __rspack_dirname } from "node:path";
|
|
@@ -32,11 +32,6 @@ const readAsset = async (chunk)=>{
|
|
|
32
32
|
return fs.readFile(filepath, 'utf-8');
|
|
33
33
|
};
|
|
34
34
|
class LoadableCollector {
|
|
35
|
-
getMatchedRouteChunks() {
|
|
36
|
-
const { routeManifest, runtimeContext } = this.options;
|
|
37
|
-
if (!routeManifest) return [];
|
|
38
|
-
return getMatchedRouteAssets(runtimeContext, routeManifest).map(routeAssetToChunk);
|
|
39
|
-
}
|
|
40
35
|
collect(comopnent) {
|
|
41
36
|
const { stats, entryName } = this.options;
|
|
42
37
|
if (!stats) return comopnent;
|
|
@@ -58,7 +53,7 @@ class LoadableCollector {
|
|
|
58
53
|
]));
|
|
59
54
|
} catch (e) {}
|
|
60
55
|
const collectedChunks = extractor ? extractor.getChunkAssets(extractor.chunks) : [];
|
|
61
|
-
const matchedRouteChunks =
|
|
56
|
+
const matchedRouteChunks = getMatchedRouteChunks(options.runtimeContext, options.routeManifest, routeAssetToChunk);
|
|
62
57
|
const orderedScriptChunks = orderHydrationScriptChunks({
|
|
63
58
|
asyncEntryChunks: asyncChunks,
|
|
64
59
|
collectedChunks,
|
|
@@ -136,4 +131,4 @@ class LoadableCollector {
|
|
|
136
131
|
this.options = options;
|
|
137
132
|
}
|
|
138
133
|
}
|
|
139
|
-
export { LoadableCollector
|
|
134
|
+
export { LoadableCollector };
|
package/dist/esm-node/index.mjs
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import "node:module";
|
|
2
2
|
export { isBrowser } from "./common.mjs";
|
|
3
|
-
export {
|
|
3
|
+
export { defineRuntimeConfig } from "./core/config.mjs";
|
|
4
4
|
export { getMonitors } from "./core/context/monitors/index.mjs";
|
|
5
|
+
export { RuntimeContext, useRuntimeContext } from "./core/context/public.mjs";
|
|
5
6
|
export { getRequest } from "./core/context/request/index.mjs";
|
|
6
7
|
export { redirect, setHeaders, setStatus } from "./core/context/response/index.mjs";
|
|
@@ -184,7 +184,8 @@ const fileSystemRoutes = async ({ metaName, routes, ssrMode, nestedRoutesEntry,
|
|
|
184
184
|
component = `component_${components.length - 1}`;
|
|
185
185
|
}
|
|
186
186
|
const isClientComponent = 'nested' === route.type && Boolean(route._component) && Boolean(srcDirectory) && Boolean(internalSrcAlias) && await hasUseClientDirective(route._component, srcDirectory, internalSrcAlias);
|
|
187
|
-
const
|
|
187
|
+
const shouldHydrateRscRoute = isRscClientBundle && !isClientComponent && !route.loader && !route.data;
|
|
188
|
+
const shouldIncludeClientBundle = !isRscClientBundle || isClientComponent || shouldHydrateRscRoute;
|
|
188
189
|
const finalRoute = {
|
|
189
190
|
...route,
|
|
190
191
|
loading,
|
|
@@ -287,7 +288,7 @@ const fileSystemRoutes = async ({ metaName, routes, ssrMode, nestedRoutesEntry,
|
|
|
287
288
|
${importLazyCode}
|
|
288
289
|
${!isRscClientBundle ? importComponentsCode : ''}
|
|
289
290
|
${importRuntimeRouterCode}
|
|
290
|
-
${
|
|
291
|
+
${rootLayoutCode}
|
|
291
292
|
${importLoadingCode}
|
|
292
293
|
${importErrorComponentsCode}
|
|
293
294
|
${importLoadersCode}
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import "node:module";
|
|
2
2
|
import { createSyncHook } from "@modern-js/plugin";
|
|
3
|
-
const
|
|
4
|
-
const
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
const
|
|
8
|
-
const
|
|
3
|
+
const createRouterSyncHook = ()=>createSyncHook();
|
|
4
|
+
const modifyRoutes = createRouterSyncHook();
|
|
5
|
+
const onBeforeCreateRoutes = createRouterSyncHook();
|
|
6
|
+
const onBeforeCreateRouter = createRouterSyncHook();
|
|
7
|
+
const onAfterCreateRouter = createRouterSyncHook();
|
|
8
|
+
const onBeforeHydrateRouter = createRouterSyncHook();
|
|
9
|
+
const onAfterHydrateRouter = createRouterSyncHook();
|
|
9
10
|
export { modifyRoutes, onAfterCreateRouter, onAfterHydrateRouter, onBeforeCreateRouter, onBeforeCreateRoutes, onBeforeHydrateRouter };
|
|
@@ -41,7 +41,7 @@ function registerRouterProvider(name, factory, options = {}) {
|
|
|
41
41
|
}
|
|
42
42
|
function resolveRouterProvider(framework, options = {}) {
|
|
43
43
|
const registry = getRegistry();
|
|
44
|
-
const name = framework ||
|
|
44
|
+
const name = framework || options.localDefault?.name || registry.defaultProvider;
|
|
45
45
|
if (void 0 === name) throw new Error('[@modern-js/runtime] No default router provider is registered. This is a bug in the runtime setup.');
|
|
46
46
|
if (void 0 !== options.localDefault && name === options.localDefault.name) return options.localDefault.factory;
|
|
47
47
|
const factory = registry.providers.get(name);
|
package/dist/types/common.d.ts
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
|
-
import type { RuntimePlugin } from './core/plugin';
|
|
2
1
|
export declare const isBrowser: () => boolean;
|
|
2
|
+
export type RuntimePlugin<Extends = unknown> = {
|
|
3
|
+
name?: string;
|
|
4
|
+
registryHooks?: Extends extends {
|
|
5
|
+
extendHooks: infer Hooks;
|
|
6
|
+
} ? Hooks : Record<string, unknown>;
|
|
7
|
+
setup?: (...args: any[]) => unknown;
|
|
8
|
+
[key: string]: unknown;
|
|
9
|
+
};
|
|
3
10
|
export interface RuntimeConfig {
|
|
4
11
|
plugins?: RuntimePlugin[];
|
|
5
12
|
[key: string]: any;
|
|
@@ -1,13 +1,29 @@
|
|
|
1
|
-
import type { InternalRuntimeContext } from '@modern-js/plugin';
|
|
2
|
-
import type { NestedRoute, PageRoute } from '@modern-js/types';
|
|
3
1
|
import type React from 'react';
|
|
4
|
-
import type { RuntimeExtends } from '../plugin/types';
|
|
5
2
|
import type { ServerPayload } from './serverPayload/index';
|
|
3
|
+
export type RuntimeRoute = {
|
|
4
|
+
children?: RuntimeRoute[];
|
|
5
|
+
routes?: RuntimeRoute[];
|
|
6
|
+
[key: string]: any;
|
|
7
|
+
};
|
|
8
|
+
type RuntimeHookCaller = {
|
|
9
|
+
call: (...args: any[]) => any;
|
|
10
|
+
[key: string]: unknown;
|
|
11
|
+
};
|
|
12
|
+
type RuntimeHooks = Record<string, RuntimeHookCaller>;
|
|
13
|
+
type RuntimePluginAPI = {
|
|
14
|
+
updateRuntimeContext?: (context: unknown) => unknown;
|
|
15
|
+
[key: string]: any;
|
|
16
|
+
};
|
|
17
|
+
type InternalRuntimeContextLike = {
|
|
18
|
+
hooks: RuntimeHooks;
|
|
19
|
+
pluginAPI?: RuntimePluginAPI;
|
|
20
|
+
[key: string]: any;
|
|
21
|
+
};
|
|
6
22
|
export { DefaultNotFound } from '../../router/runtime/DefaultNotFound';
|
|
7
23
|
export { modifyRoutes, onAfterCreateRouter, onAfterHydrateRouter, onBeforeCreateRouter, onBeforeCreateRoutes, onBeforeHydrateRouter, type RouterExtendsHooks, } from '../../router/runtime/hooks';
|
|
8
24
|
export { applyRouterRuntimeState, applyRouterServerPrepareResult, cleanupRouterRuntimeState, createRouterRuntimeState, createRouterServerSnapshot, getRouterHydrationScripts, getRouterMatchedRouteIds, getRouterRuntimeState, getRouterServerSnapshot, type RouterLifecycleContext, type RouterLifecyclePhase, } from '../../router/runtime/lifecycle';
|
|
9
25
|
export { type RouterProviderFactory, type RouterProviderPlugin, registerRouterProvider, resolveRouterProvider, routerProviderRegistryHooks, } from '../../router/runtime/provider';
|
|
10
|
-
export type { BuiltInRouterFramework, InternalRouterRuntimeState, InternalRouterServerSnapshot, RouterFramework, RouterRouteMatchSnapshot, RouterServerPrepareResult, } from '../../router/runtime/types';
|
|
26
|
+
export type { BuiltInRouterFramework, InternalRouterRuntimeState, InternalRouterServerSnapshot, LoaderFunction, LoaderFunctionArgs, ModernRoute, RouterFramework, RouterRouteMatchSnapshot, RouterServerPrepareResult, } from '../../router/runtime/types';
|
|
11
27
|
export { createRuntimeContextExtension, type RuntimeContextExtension, } from './extensions';
|
|
12
28
|
export { getInitialContext, InternalRuntimeContext, RuntimeContext, type TInternalRuntimeContext, type TRuntimeContext, } from './runtime';
|
|
13
29
|
export type { PayloadRoute, ServerPayload } from './serverPayload/index';
|
|
@@ -20,7 +36,7 @@ interface GlobalContext {
|
|
|
20
36
|
/**
|
|
21
37
|
* nest router and page router config
|
|
22
38
|
*/
|
|
23
|
-
routes?:
|
|
39
|
+
routes?: RuntimeRoute[];
|
|
24
40
|
/**
|
|
25
41
|
* nest router init function
|
|
26
42
|
*/
|
|
@@ -33,7 +49,7 @@ interface GlobalContext {
|
|
|
33
49
|
* Entry basename for routing
|
|
34
50
|
*/
|
|
35
51
|
basename?: string;
|
|
36
|
-
internalRuntimeContext?:
|
|
52
|
+
internalRuntimeContext?: InternalRuntimeContextLike;
|
|
37
53
|
/**
|
|
38
54
|
* RSCRoot
|
|
39
55
|
*/
|
|
@@ -48,9 +64,9 @@ export declare function getGlobalEnableRsc(): boolean | undefined;
|
|
|
48
64
|
export declare function setGlobalContext(context: Omit<GlobalContext, 'internalRuntimeContext'>): void;
|
|
49
65
|
export declare function getCurrentEntryName(): string | undefined;
|
|
50
66
|
export declare function getGlobalRSCRoot(): React.ComponentType<{}> | undefined;
|
|
51
|
-
export declare function setGlobalInternalRuntimeContext(context:
|
|
52
|
-
export declare function getGlobalInternalRuntimeContext():
|
|
67
|
+
export declare function setGlobalInternalRuntimeContext(context: InternalRuntimeContextLike): void;
|
|
68
|
+
export declare function getGlobalInternalRuntimeContext(): InternalRuntimeContextLike;
|
|
53
69
|
export declare function getGlobalApp(): React.ComponentType<{}> | undefined;
|
|
54
|
-
export declare function getGlobalRoutes(): undefined |
|
|
70
|
+
export declare function getGlobalRoutes(): undefined | RuntimeRoute[];
|
|
55
71
|
export declare function getGlobalLayoutApp(): React.ComponentType<{}> | undefined;
|
|
56
72
|
export declare function getGlobalBasename(): string | undefined;
|
|
@@ -1,2 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
export type RuntimeMonitors = {
|
|
2
|
+
counter: (name: string, ...args: any[]) => void;
|
|
3
|
+
info: (...args: any[]) => void;
|
|
4
|
+
debug: (...args: any[]) => void;
|
|
5
|
+
trace: (...args: any[]) => void;
|
|
6
|
+
warn: (...args: any[]) => void;
|
|
7
|
+
error: (...args: any[]) => void;
|
|
8
|
+
timing: (name: string, dur: number, ...args: any[]) => void;
|
|
9
|
+
};
|
|
10
|
+
export declare const defaultMonitors: RuntimeMonitors;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const getMonitors: () =>
|
|
1
|
+
export declare const getMonitors: () => import("./default").RuntimeMonitors;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import type
|
|
2
|
-
export declare const getMonitors: () =>
|
|
1
|
+
import { type RuntimeMonitors } from './default';
|
|
2
|
+
export declare const getMonitors: () => RuntimeMonitors;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export type RuntimeRequest = Record<string, unknown>;
|
|
2
|
+
export type RuntimeResponse = Record<string, unknown>;
|
|
3
|
+
export type RequestContext = {
|
|
4
|
+
request: RuntimeRequest;
|
|
5
|
+
response: RuntimeResponse;
|
|
6
|
+
};
|
|
7
|
+
export interface TRuntimeContext {
|
|
8
|
+
initialData?: Record<string, unknown>;
|
|
9
|
+
isBrowser: boolean;
|
|
10
|
+
routes?: unknown[];
|
|
11
|
+
requestContext: RequestContext;
|
|
12
|
+
/**
|
|
13
|
+
* @deprecated Use `requestContext` instead
|
|
14
|
+
*/
|
|
15
|
+
context: RequestContext;
|
|
16
|
+
[key: string]: unknown;
|
|
17
|
+
}
|
|
18
|
+
export declare const RuntimeContext: import("react").Context<TRuntimeContext>;
|
|
19
|
+
/**
|
|
20
|
+
* deprecated, use RuntimeContext instead
|
|
21
|
+
*/
|
|
22
|
+
export declare const ReactRuntimeContext: import("react").Context<TRuntimeContext>;
|
|
23
|
+
/**
|
|
24
|
+
* @deprecated use use(RuntimeContext) instead
|
|
25
|
+
*/
|
|
26
|
+
export declare const useRuntimeContext: () => TRuntimeContext;
|
|
@@ -1,36 +1,26 @@
|
|
|
1
1
|
import type { RouteObject, StaticHandlerContext } from '@modern-js/runtime-utils/router';
|
|
2
|
+
import type { BaseSSRServerContext } from '@modern-js/types';
|
|
2
3
|
import type { RouteManifest } from '../../router/runtime/types';
|
|
3
|
-
import
|
|
4
|
-
export
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
context: RequestContext;
|
|
13
|
-
[key: string]: unknown;
|
|
14
|
-
}
|
|
4
|
+
import { type TRuntimeContext } from './public';
|
|
5
|
+
export type InternalSSRContext = {
|
|
6
|
+
request: BaseSSRServerContext['request'] & {
|
|
7
|
+
raw?: Request;
|
|
8
|
+
};
|
|
9
|
+
response: BaseSSRServerContext['response'];
|
|
10
|
+
[key: string]: any;
|
|
11
|
+
};
|
|
12
|
+
export { ReactRuntimeContext, RuntimeContext, type TRuntimeContext, useRuntimeContext, } from './public';
|
|
15
13
|
/**
|
|
16
14
|
* InternalRuntimeContext used internally and by plugins
|
|
17
15
|
*/
|
|
18
16
|
export interface TInternalRuntimeContext extends TRuntimeContext {
|
|
19
17
|
routeManifest?: RouteManifest;
|
|
18
|
+
routes?: RouteObject[];
|
|
20
19
|
routerContext?: StaticHandlerContext;
|
|
21
20
|
unstable_getBlockNavState?: () => boolean;
|
|
22
|
-
ssrContext?:
|
|
21
|
+
ssrContext?: InternalSSRContext;
|
|
23
22
|
_internalContext?: any;
|
|
24
23
|
_internalRouterBaseName?: any;
|
|
25
24
|
}
|
|
26
25
|
export declare const InternalRuntimeContext: import("react").Context<TInternalRuntimeContext>;
|
|
27
|
-
export declare const RuntimeContext: import("react").Context<TRuntimeContext>;
|
|
28
|
-
/**
|
|
29
|
-
* deprecated, use RuntimeContext instead
|
|
30
|
-
*/
|
|
31
|
-
export declare const ReactRuntimeContext: import("react").Context<TRuntimeContext>;
|
|
32
26
|
export declare const getInitialContext: (isBrowser?: boolean, routeManifest?: RouteManifest) => TInternalRuntimeContext;
|
|
33
|
-
/**
|
|
34
|
-
* @deprecated use use(RuntimeContext) instead
|
|
35
|
-
*/
|
|
36
|
-
export declare const useRuntimeContext: () => TRuntimeContext;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import type React from 'react';
|
|
2
|
-
export declare function createRoot(UserApp?: React.ComponentType | null):
|
|
2
|
+
export declare function createRoot(UserApp?: React.ComponentType | null): any;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { OnError } from '@modern-js/app-tools';
|
|
2
|
+
import type { TInternalRuntimeContext } from '../context';
|
|
3
|
+
import type { RouterCleanup } from './routerCleanup';
|
|
4
|
+
export type ResponseProxy = {
|
|
5
|
+
headers: Record<string, string>;
|
|
6
|
+
status: number;
|
|
7
|
+
};
|
|
8
|
+
export type RedirectContext = {
|
|
9
|
+
enableRsc: boolean;
|
|
10
|
+
isRSCNavigation: boolean;
|
|
11
|
+
basename: string;
|
|
12
|
+
};
|
|
13
|
+
export declare const applyRouterSnapshotResult: (context: TInternalRuntimeContext, onError: OnError) => void;
|
|
14
|
+
export declare const createLoaderRedirectResponse: (beforeRenderResult: Response | undefined, redirectCtx: RedirectContext) => Response | undefined;
|
|
15
|
+
export declare const finalizeRenderResponse: (response: Response, responseProxy: ResponseProxy, redirectCtx: RedirectContext, routerCleanup: RouterCleanup) => Response;
|
|
@@ -25,4 +25,6 @@ export type RouterCleanup = {
|
|
|
25
25
|
deferUntilBodyDone: (response: Response) => Response;
|
|
26
26
|
};
|
|
27
27
|
export declare function withRouterCleanup<T>(runtimeContext: TInternalRuntimeContext, onError: OnError, callback: (routerCleanup: RouterCleanup) => Promise<T>): Promise<T>;
|
|
28
|
+
export declare function runWithRouterCleanupOnError<T>(routerCleanup: RouterCleanup, callback: () => Promise<T> | T): Promise<T>;
|
|
29
|
+
export declare function finishWithRouterCleanup<T>(routerCleanup: RouterCleanup, callback: () => Promise<T> | T): Promise<T>;
|
|
28
30
|
export declare function createRouterCleanup(runtimeContext: TInternalRuntimeContext, onError: OnError): RouterCleanup;
|
|
@@ -10,6 +10,7 @@ type ScriptChunkLike = {
|
|
|
10
10
|
url?: string;
|
|
11
11
|
};
|
|
12
12
|
export declare function getMatchedRouteAssets(runtimeContext: TInternalRuntimeContext, routeManifest?: RouteManifestLike): string[];
|
|
13
|
+
export declare function getMatchedRouteChunks<T>(runtimeContext: TInternalRuntimeContext, routeManifest: RouteManifestLike | undefined, routeAssetToChunk: (asset: string) => T): T[];
|
|
13
14
|
export declare const orderHydrationScriptChunks: <T extends ScriptChunkLike>({ asyncEntryChunks, collectedChunks, matchedRouteChunks, entryName, }: {
|
|
14
15
|
asyncEntryChunks: T[];
|
|
15
16
|
collectedChunks: T[];
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { OnError } from '@modern-js/app-tools';
|
|
2
2
|
import type { ClientManifest as RscClientManifest, ServerManifest as RscServerManifest, SSRManifest as RscSSRManifest } from '@modern-js/types/server';
|
|
3
3
|
import type React from 'react';
|
|
4
|
-
import type {
|
|
4
|
+
import type { TInternalRuntimeContext } from '../../context';
|
|
5
5
|
import type { HandleRequestConfig } from '../requestHandler';
|
|
6
6
|
import type { RenderStreaming, SSRConfig } from '../shared';
|
|
7
7
|
export type RscManifest = {
|
|
@@ -11,7 +11,7 @@ export type RscManifest = {
|
|
|
11
11
|
entryCssFiles?: Record<string, string[]>;
|
|
12
12
|
};
|
|
13
13
|
export type CreateReadableStreamFromElementOptions = {
|
|
14
|
-
runtimeContext:
|
|
14
|
+
runtimeContext: TInternalRuntimeContext;
|
|
15
15
|
config: HandleRequestConfig;
|
|
16
16
|
ssrConfig: SSRConfig;
|
|
17
17
|
htmlTemplate: string;
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import type { ReactElement } from 'react';
|
|
2
2
|
import type { TInternalRuntimeContext } from '../../context';
|
|
3
3
|
import type { ChunkSet, Collector } from './types';
|
|
4
|
-
export { orderHydrationScriptChunks } from '../scriptOrder';
|
|
5
4
|
declare module '@loadable/server' {
|
|
6
5
|
interface ChunkExtractor {
|
|
7
6
|
chunks: string[];
|
|
@@ -30,7 +29,6 @@ export declare class LoadableCollector implements Collector {
|
|
|
30
29
|
private options;
|
|
31
30
|
private extractor?;
|
|
32
31
|
constructor(options: LoadableCollectorOptions);
|
|
33
|
-
private getMatchedRouteChunks;
|
|
34
32
|
collect(comopnent: ReactElement): ReactElement;
|
|
35
33
|
effect(): Promise<void>;
|
|
36
34
|
private emitLoadableScripts;
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import type { RouterConfig } from './router/
|
|
2
|
-
export type { RuntimeConfig } from './common';
|
|
1
|
+
import type { RouterConfig } from './router/runtime/types';
|
|
2
|
+
export type { RuntimeConfig, RuntimePlugin } from './common';
|
|
3
3
|
export { isBrowser } from './common';
|
|
4
|
-
export
|
|
5
|
-
export { defineRuntimeConfig, RuntimeContext, useRuntimeContext, } from './core';
|
|
4
|
+
export { defineRuntimeConfig } from './core/config';
|
|
6
5
|
export { getMonitors } from './core/context/monitors';
|
|
6
|
+
export type { TRuntimeContext } from './core/context/public';
|
|
7
|
+
export { RuntimeContext, useRuntimeContext, } from './core/context/public';
|
|
7
8
|
export { getRequest } from './core/context/request';
|
|
8
9
|
export { redirect, setHeaders, setStatus } from './core/context/response';
|
|
9
|
-
export type { TRuntimeContext } from './core/context/runtime';
|
|
10
10
|
export type { RouterConfig };
|
package/dist/types/internal.d.ts
CHANGED
|
@@ -1,12 +1,16 @@
|
|
|
1
1
|
import type { RouteObject } from '@modern-js/runtime-utils/router';
|
|
2
2
|
import type { TRuntimeContext } from '../../core/context/runtime';
|
|
3
3
|
import type { RouterLifecycleContext } from './lifecycle';
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
declare const
|
|
9
|
-
declare const
|
|
4
|
+
export type RouterSyncHook<Handler extends (...args: any[]) => any> = {
|
|
5
|
+
call: (...args: Parameters<Handler>) => ReturnType<Handler>;
|
|
6
|
+
[key: string]: unknown;
|
|
7
|
+
};
|
|
8
|
+
declare const modifyRoutes: RouterSyncHook<(routes: RouteObject[]) => RouteObject[]>;
|
|
9
|
+
declare const onBeforeCreateRoutes: RouterSyncHook<(context: TRuntimeContext) => void>;
|
|
10
|
+
declare const onBeforeCreateRouter: RouterSyncHook<(context: RouterLifecycleContext) => void>;
|
|
11
|
+
declare const onAfterCreateRouter: RouterSyncHook<(context: RouterLifecycleContext) => void>;
|
|
12
|
+
declare const onBeforeHydrateRouter: RouterSyncHook<(context: RouterLifecycleContext) => void>;
|
|
13
|
+
declare const onAfterHydrateRouter: RouterSyncHook<(context: RouterLifecycleContext) => void>;
|
|
10
14
|
export { modifyRoutes, onAfterCreateRouter, onAfterHydrateRouter, onBeforeCreateRouter, onBeforeCreateRoutes, onBeforeHydrateRouter, };
|
|
11
15
|
export type RouterExtendsHooks = {
|
|
12
16
|
modifyRoutes: typeof modifyRoutes;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { RuntimePlugin } from '../../
|
|
1
|
+
import type { RuntimePlugin } from '../../common';
|
|
2
2
|
import type { RouterExtendsHooks } from './hooks';
|
|
3
3
|
import type { RouterConfig, SingleRouteConfig } from './types';
|
|
4
4
|
export declare const routerPlugin: (userConfig?: Partial<RouterConfig>) => RuntimePlugin<{
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { RuntimePlugin } from '../../common';
|
|
2
2
|
import type { RouterExtendsHooks } from './hooks';
|
|
3
3
|
import type { RouterConfig, Routes } from './types';
|
|
4
4
|
export declare let finalRouteConfig: RouterConfig['routesConfig'];
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { RuntimePlugin } from '../../
|
|
1
|
+
import type { RuntimePlugin } from '../../common';
|
|
2
2
|
import type { RouterExtendsHooks } from './hooks';
|
|
3
3
|
import type { RouterConfig } from './types';
|
|
4
4
|
export declare const routerPlugin: (userConfig?: Partial<RouterConfig>) => RuntimePlugin<{
|