@modern-js/runtime 2.65.2 → 2.65.4
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/code.js +1 -0
- package/dist/cjs/cli/template.js +7 -3
- package/dist/cjs/core/browser/index.js +1 -3
- package/dist/cjs/core/compat/hooks.js +2 -2
- package/dist/cjs/core/compat/requestContext.js +48 -0
- package/dist/cjs/core/compatible.js +2 -14
- package/dist/cjs/core/context/index.js +6 -0
- package/dist/cjs/core/context/monitors/default.js +5 -10
- package/dist/cjs/core/context/response/index.js +43 -0
- package/dist/cjs/core/context/response/index.server.js +62 -0
- package/dist/cjs/core/plugin/index.js +2 -0
- package/dist/cjs/core/server/requestHandler.js +10 -8
- package/dist/cjs/index.js +7 -0
- package/dist/cjs/react-server.js +12 -2
- package/dist/cjs/router/cli/code/templates.js +3 -1
- package/dist/cjs/router/cli/handler.js +1 -0
- package/dist/esm/cli/code.js +1 -0
- package/dist/esm/cli/template.js +4 -4
- package/dist/esm/core/browser/index.js +3 -4
- package/dist/esm/core/compat/hooks.js +2 -2
- package/dist/esm/core/compat/requestContext.js +25 -0
- package/dist/esm/core/compatible.js +5 -16
- package/dist/esm/core/context/index.js +5 -0
- package/dist/esm/core/context/monitors/default.js +5 -25
- package/dist/esm/core/context/response/index.js +16 -0
- package/dist/esm/core/context/response/index.server.js +41 -0
- package/dist/esm/core/plugin/index.js +3 -1
- package/dist/esm/core/server/requestHandler.js +12 -10
- package/dist/esm/index.js +4 -0
- package/dist/esm/react-server.js +7 -1
- package/dist/esm/router/cli/code/templates.js +3 -3
- package/dist/esm/router/cli/handler.js +1 -0
- package/dist/esm-node/cli/code.js +1 -0
- package/dist/esm-node/cli/template.js +7 -3
- package/dist/esm-node/core/browser/index.js +1 -3
- package/dist/esm-node/core/compat/hooks.js +2 -2
- package/dist/esm-node/core/compat/requestContext.js +23 -0
- package/dist/esm-node/core/compatible.js +2 -14
- package/dist/esm-node/core/context/index.js +5 -0
- package/dist/esm-node/core/context/monitors/default.js +5 -10
- package/dist/esm-node/core/context/response/index.js +16 -0
- package/dist/esm-node/core/context/response/index.server.js +35 -0
- package/dist/esm-node/core/plugin/index.js +2 -0
- package/dist/esm-node/core/server/requestHandler.js +10 -8
- package/dist/esm-node/index.js +4 -0
- package/dist/esm-node/react-server.js +7 -1
- package/dist/esm-node/router/cli/code/templates.js +3 -1
- package/dist/esm-node/router/cli/handler.js +1 -0
- package/dist/types/cli/template.d.ts +2 -1
- package/dist/types/core/compat/requestContext.d.ts +27 -0
- package/dist/types/core/config.d.ts +1 -1
- package/dist/types/core/context/index.d.ts +2 -0
- package/dist/types/core/context/response/index.d.ts +4 -0
- package/dist/types/core/context/response/index.server.d.ts +7 -0
- package/dist/types/core/context/runtime.d.ts +1 -0
- package/dist/types/core/plugin/base.d.ts +1 -1
- package/dist/types/core/plugin/index.d.ts +1 -1
- package/dist/types/core/plugin/types.d.ts +2 -1
- package/dist/types/core/types.d.ts +4 -17
- package/dist/types/index.d.ts +1 -0
- package/dist/types/react-server.d.ts +2 -0
- package/dist/types/router/cli/code/templates.d.ts +2 -1
- package/package.json +12 -12
|
@@ -4,31 +4,11 @@ var defaultMonitors = {
|
|
|
4
4
|
args[_key - 1] = arguments[_key];
|
|
5
5
|
}
|
|
6
6
|
},
|
|
7
|
-
info:
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
debug: function debug(message) {
|
|
13
|
-
for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
|
14
|
-
args[_key - 1] = arguments[_key];
|
|
15
|
-
}
|
|
16
|
-
},
|
|
17
|
-
trace: function trace(message) {
|
|
18
|
-
for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
|
19
|
-
args[_key - 1] = arguments[_key];
|
|
20
|
-
}
|
|
21
|
-
},
|
|
22
|
-
warn: function warn(message) {
|
|
23
|
-
for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
|
24
|
-
args[_key - 1] = arguments[_key];
|
|
25
|
-
}
|
|
26
|
-
},
|
|
27
|
-
error: function error(message) {
|
|
28
|
-
for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
|
29
|
-
args[_key - 1] = arguments[_key];
|
|
30
|
-
}
|
|
31
|
-
},
|
|
7
|
+
info: console.info,
|
|
8
|
+
debug: console.debug,
|
|
9
|
+
trace: console.trace,
|
|
10
|
+
warn: console.warn,
|
|
11
|
+
error: console.error,
|
|
32
12
|
timing: function timing(name, dur) {
|
|
33
13
|
for (var _len = arguments.length, args = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
|
|
34
14
|
args[_key - 2] = arguments[_key];
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
var getResponseProxy = function() {
|
|
2
|
+
return null;
|
|
3
|
+
};
|
|
4
|
+
var setHeaders = function(headers) {
|
|
5
|
+
};
|
|
6
|
+
var setStatus = function(status) {
|
|
7
|
+
};
|
|
8
|
+
var redirect = function(url, init) {
|
|
9
|
+
console.warn("You should not use this API in the browser, please use the router's redirect or useNavigate method.");
|
|
10
|
+
};
|
|
11
|
+
export {
|
|
12
|
+
getResponseProxy,
|
|
13
|
+
redirect,
|
|
14
|
+
setHeaders,
|
|
15
|
+
setStatus
|
|
16
|
+
};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
|
|
2
|
+
import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
|
|
3
|
+
import { _ as _type_of } from "@swc/helpers/_/_type_of";
|
|
4
|
+
import { storage } from "@modern-js/runtime-utils/node";
|
|
5
|
+
var getResponseProxy = function() {
|
|
6
|
+
var context = storage.useContext();
|
|
7
|
+
return context === null || context === void 0 ? void 0 : context.responseProxy;
|
|
8
|
+
};
|
|
9
|
+
var setHeaders = function(headers) {
|
|
10
|
+
var responseProxy = getResponseProxy();
|
|
11
|
+
Object.entries(headers).forEach(function(param) {
|
|
12
|
+
var _param = _sliced_to_array(param, 2), key = _param[0], value = _param[1];
|
|
13
|
+
responseProxy.headers[key] = value;
|
|
14
|
+
});
|
|
15
|
+
};
|
|
16
|
+
var setStatus = function(status) {
|
|
17
|
+
var responseProxy = getResponseProxy();
|
|
18
|
+
responseProxy.status = status;
|
|
19
|
+
};
|
|
20
|
+
var redirect = function(url, init) {
|
|
21
|
+
var _init_status;
|
|
22
|
+
var status = init === void 0 ? 307 : typeof init === "number" ? init : (_init_status = init.status) !== null && _init_status !== void 0 ? _init_status : 307;
|
|
23
|
+
var _init_headers;
|
|
24
|
+
var headers = init === void 0 ? {} : typeof init === "number" ? {} : (_init_headers = init.headers) !== null && _init_headers !== void 0 ? _init_headers : {};
|
|
25
|
+
setStatus(status);
|
|
26
|
+
setHeaders(_object_spread({
|
|
27
|
+
Location: url
|
|
28
|
+
}, init && (typeof init === "undefined" ? "undefined" : _type_of(init)) === "object" ? Object.fromEntries(Object.entries(headers).map(function(param) {
|
|
29
|
+
var _param = _sliced_to_array(param, 2), k = _param[0], v = _param[1];
|
|
30
|
+
return [
|
|
31
|
+
k,
|
|
32
|
+
String(v)
|
|
33
|
+
];
|
|
34
|
+
})) : {}));
|
|
35
|
+
};
|
|
36
|
+
export {
|
|
37
|
+
getResponseProxy,
|
|
38
|
+
redirect,
|
|
39
|
+
setHeaders,
|
|
40
|
+
setStatus
|
|
41
|
+
};
|
|
@@ -3,12 +3,14 @@ import { runtime } from "@modern-js/plugin-v2/runtime";
|
|
|
3
3
|
import { merge } from "@modern-js/runtime-utils/merge";
|
|
4
4
|
import { compatPlugin } from "../compat";
|
|
5
5
|
import { handleSetupResult } from "../compat/hooks";
|
|
6
|
+
import { requestContextPlugin } from "../compat/requestContext";
|
|
6
7
|
import { setGlobalInternalRuntimeContext } from "../context";
|
|
7
8
|
function registerPlugin(internalPlugins, runtimeConfig) {
|
|
8
9
|
var _ref = runtimeConfig || {}, _ref_plugins = _ref.plugins, plugins = _ref_plugins === void 0 ? [] : _ref_plugins;
|
|
9
10
|
var runtimeContext = runtime.run({
|
|
10
11
|
plugins: [
|
|
11
|
-
compatPlugin()
|
|
12
|
+
compatPlugin(),
|
|
13
|
+
requestContextPlugin()
|
|
12
14
|
].concat(_to_consumable_array(internalPlugins), _to_consumable_array(plugins)),
|
|
13
15
|
config: runtimeConfig || {},
|
|
14
16
|
handleSetupResult
|
|
@@ -52,6 +52,7 @@ function createSSRContext(request, options) {
|
|
|
52
52
|
params,
|
|
53
53
|
headers: headersData,
|
|
54
54
|
host,
|
|
55
|
+
referer: headers.get("referer"),
|
|
55
56
|
raw: request
|
|
56
57
|
},
|
|
57
58
|
response: {
|
|
@@ -59,7 +60,7 @@ function createSSRContext(request, options) {
|
|
|
59
60
|
responseProxy.headers[key] = value;
|
|
60
61
|
},
|
|
61
62
|
status: function status(code) {
|
|
62
|
-
responseProxy.
|
|
63
|
+
responseProxy.status = code;
|
|
63
64
|
},
|
|
64
65
|
locals: locals || {}
|
|
65
66
|
},
|
|
@@ -76,17 +77,22 @@ var createRequestHandler = function() {
|
|
|
76
77
|
return _ts_generator(this, function(_state) {
|
|
77
78
|
requestHandler = function() {
|
|
78
79
|
var _ref2 = _async_to_generator(function(request, options) {
|
|
79
|
-
var headersData;
|
|
80
|
+
var headersData, responseProxy;
|
|
80
81
|
return _ts_generator(this, function(_state2) {
|
|
81
82
|
headersData = parseHeaders(request);
|
|
83
|
+
responseProxy = {
|
|
84
|
+
headers: {},
|
|
85
|
+
status: -1
|
|
86
|
+
};
|
|
82
87
|
return [
|
|
83
88
|
2,
|
|
84
89
|
storage.run({
|
|
85
90
|
headers: headersData,
|
|
86
91
|
request,
|
|
87
|
-
monitors: options.monitors
|
|
92
|
+
monitors: options.monitors,
|
|
93
|
+
responseProxy
|
|
88
94
|
}, /* @__PURE__ */ _async_to_generator(function() {
|
|
89
|
-
var _context_routerContext, _context_routerContext1, _context_routerContext2, Root, internalRuntimeContext, hooks, routeManifest, context, runBeforeRender,
|
|
95
|
+
var _context_routerContext, _context_routerContext1, _context_routerContext2, Root, internalRuntimeContext, hooks, routeManifest, context, runBeforeRender, ssrContext, getRedirectResponse, initialData, _context_routerContext3, _context_ssrContext, errors, redirectResponse, htmlTemplate, response;
|
|
90
96
|
return _ts_generator(this, function(_state3) {
|
|
91
97
|
switch (_state3.label) {
|
|
92
98
|
case 0:
|
|
@@ -125,10 +131,6 @@ var createRequestHandler = function() {
|
|
|
125
131
|
return _ref3.apply(this, arguments);
|
|
126
132
|
};
|
|
127
133
|
}();
|
|
128
|
-
responseProxy = {
|
|
129
|
-
headers: {},
|
|
130
|
-
code: -1
|
|
131
|
-
};
|
|
132
134
|
ssrContext = createSSRContext(request, _object_spread_props(_object_spread({}, options), {
|
|
133
135
|
responseProxy
|
|
134
136
|
}));
|
|
@@ -195,11 +197,11 @@ var createRequestHandler = function() {
|
|
|
195
197
|
var _param = _sliced_to_array(param, 2), key = _param[0], value = _param[1];
|
|
196
198
|
response.headers.set(key, value);
|
|
197
199
|
});
|
|
198
|
-
if (responseProxy.
|
|
200
|
+
if (responseProxy.status !== -1) {
|
|
199
201
|
return [
|
|
200
202
|
2,
|
|
201
203
|
new Response(response.body, {
|
|
202
|
-
status: responseProxy.
|
|
204
|
+
status: responseProxy.status,
|
|
203
205
|
headers: response.headers
|
|
204
206
|
})
|
|
205
207
|
];
|
package/dist/esm/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { isBrowser } from "./common";
|
|
2
2
|
import { getMonitors } from "./core/context/monitors";
|
|
3
3
|
import { getRequest } from "./core/context/request";
|
|
4
|
+
import { setHeaders, setStatus, redirect } from "./core/context/response";
|
|
4
5
|
import { createApp, useLoader, bootstrap, RuntimeReactContext, defineConfig, defineRuntimeConfig, useRuntimeContext } from "./core";
|
|
5
6
|
export {
|
|
6
7
|
RuntimeReactContext,
|
|
@@ -11,6 +12,9 @@ export {
|
|
|
11
12
|
getMonitors,
|
|
12
13
|
getRequest,
|
|
13
14
|
isBrowser,
|
|
15
|
+
redirect,
|
|
16
|
+
setHeaders,
|
|
17
|
+
setStatus,
|
|
14
18
|
useLoader,
|
|
15
19
|
useRuntimeContext
|
|
16
20
|
};
|
package/dist/esm/react-server.js
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
1
|
import { getRequest } from "./core/context/request";
|
|
2
|
+
import { getMonitors } from "./core/context/monitors";
|
|
3
|
+
import { setHeaders, setStatus, redirect } from "./core/context/response";
|
|
2
4
|
export {
|
|
3
|
-
|
|
5
|
+
getMonitors,
|
|
6
|
+
getRequest,
|
|
7
|
+
redirect,
|
|
8
|
+
setHeaders,
|
|
9
|
+
setStatus
|
|
4
10
|
};
|
|
@@ -433,11 +433,11 @@ function ssrLoaderCombinedModule(entrypoints, entrypoint, config, appContext) {
|
|
|
433
433
|
}
|
|
434
434
|
var runtimeGlobalContext = function() {
|
|
435
435
|
var _ref = _async_to_generator(function(param) {
|
|
436
|
-
var metaName, srcDirectory, nestedRoutesEntry, internalSrcAlias, globalApp, imports, rootLayoutPath, rootLayoutFile, rootLayoutBuffer, rootLayout, _ref2, moduleExports, hasAppConfig, hasAppInit, layoutPath;
|
|
436
|
+
var entryName, metaName, srcDirectory, nestedRoutesEntry, internalSrcAlias, globalApp, imports, rootLayoutPath, rootLayoutFile, rootLayoutBuffer, rootLayout, _ref2, moduleExports, hasAppConfig, hasAppInit, layoutPath;
|
|
437
437
|
return _ts_generator(this, function(_state) {
|
|
438
438
|
switch (_state.label) {
|
|
439
439
|
case 0:
|
|
440
|
-
metaName = param.metaName, srcDirectory = param.srcDirectory, nestedRoutesEntry = param.nestedRoutesEntry, internalSrcAlias = param.internalSrcAlias, globalApp = param.globalApp;
|
|
440
|
+
entryName = param.entryName, metaName = param.metaName, srcDirectory = param.srcDirectory, nestedRoutesEntry = param.nestedRoutesEntry, internalSrcAlias = param.internalSrcAlias, globalApp = param.globalApp;
|
|
441
441
|
imports = [
|
|
442
442
|
"import { setGlobalContext } from '@".concat(metaName, "/runtime/context';")
|
|
443
443
|
];
|
|
@@ -514,7 +514,7 @@ var runtimeGlobalContext = function() {
|
|
|
514
514
|
}
|
|
515
515
|
return [
|
|
516
516
|
2,
|
|
517
|
-
"".concat(imports.join("\n"), "\n\nimport { routes } from './routes';\n\nsetGlobalContext({\n layoutApp,\n routes,\n appInit,\n appConfig,\n});")
|
|
517
|
+
"".concat(imports.join("\n"), "\n\nimport { routes } from './routes';\n\nconst entryName = '").concat(entryName, "';\nsetGlobalContext({\n entryName,\n layoutApp,\n routes,\n appInit,\n appConfig,\n});")
|
|
518
518
|
];
|
|
519
519
|
}
|
|
520
520
|
});
|
|
@@ -70,7 +70,8 @@ import './${ENTRY_POINT_RUNTIME_REGISTER_FILE_NAME}';
|
|
|
70
70
|
`;
|
|
71
71
|
const getImportRuntimeConfigCode = (srcDirectory, internalSrcAlias, runtimeConfigFile) => {
|
|
72
72
|
if (runtimeConfigFile && findExists(JS_EXTENSIONS.map((ext) => path.resolve(srcDirectory, `${runtimeConfigFile}${ext}`)))) {
|
|
73
|
-
return `import
|
|
73
|
+
return `import modernRuntime from '${internalSrcAlias}/${runtimeConfigFile}';
|
|
74
|
+
const runtimeConfig = typeof modernRuntime === 'function' ? modernRuntime(getCurrentEntryName()) : modernRuntime`;
|
|
74
75
|
}
|
|
75
76
|
return `let runtimeConfig;`;
|
|
76
77
|
};
|
|
@@ -79,7 +80,8 @@ const getRegisterRuntimePluginCode = (entryName, name, config) => {
|
|
|
79
80
|
return `plugins.push(${name}Plugin(mergeConfig(${JSON.stringify(config)}, (runtimeConfig || {})['${configName}'], ((runtimeConfig || {})['${configName}ByEntries'] || {})['${entryName}'], (getGlobalAppConfig() || {})['${configName}'])));`;
|
|
80
81
|
};
|
|
81
82
|
const runtimeRegister = ({ entryName, srcDirectory, internalSrcAlias, metaName, runtimeConfigFile, runtimePlugins }) => `import { registerPlugin, mergeConfig } from '@${metaName}/runtime/plugin';
|
|
82
|
-
import { getGlobalAppConfig, getGlobalLayoutApp } from '@${metaName}/runtime/context';
|
|
83
|
+
import { getGlobalAppConfig, getGlobalLayoutApp, getCurrentEntryName } from '@${metaName}/runtime/context';
|
|
84
|
+
|
|
83
85
|
${getImportRuntimeConfigCode(srcDirectory, internalSrcAlias, runtimeConfigFile)}
|
|
84
86
|
|
|
85
87
|
const plugins = [];
|
|
@@ -90,13 +92,15 @@ ${getRegisterRuntimePluginCode(entryName, name, config)}
|
|
|
90
92
|
`).join("\n")}
|
|
91
93
|
registerPlugin(plugins, runtimeConfig);
|
|
92
94
|
`;
|
|
93
|
-
const runtimeGlobalContext = ({ srcDirectory, internalSrcAlias, metaName, entry, customEntry }) => {
|
|
95
|
+
const runtimeGlobalContext = ({ entryName, srcDirectory, internalSrcAlias, metaName, entry, customEntry }) => {
|
|
94
96
|
return `import { setGlobalContext } from '@${metaName}/runtime/context'
|
|
95
97
|
|
|
96
98
|
import App from '${// We need to get the path of App.tsx here, but the entry is `src/entry.tsx`
|
|
97
99
|
formatImportPath(customEntry ? entry.replace(/entry\.[tj]sx/, "App").replace(srcDirectory, internalSrcAlias) : entry.replace(srcDirectory, internalSrcAlias).replace(/\.[tj]sx/, ""))}';
|
|
98
100
|
|
|
101
|
+
const entryName = '${entryName}';
|
|
99
102
|
setGlobalContext({
|
|
103
|
+
entryName,
|
|
100
104
|
App,
|
|
101
105
|
});`;
|
|
102
106
|
};
|
|
@@ -75,9 +75,7 @@ async function render(App, id) {
|
|
|
75
75
|
}),
|
|
76
76
|
// garfish plugin params
|
|
77
77
|
_internalRouterBaseName: App.props.basename,
|
|
78
|
-
|
|
79
|
-
ssrContext: ssrData.context
|
|
80
|
-
}
|
|
78
|
+
ssrContext: ssrData.context
|
|
81
79
|
});
|
|
82
80
|
context.initialData = (_ssrData_data1 = ssrData.data) === null || _ssrData_data1 === void 0 ? void 0 : _ssrData_data1.initialData;
|
|
83
81
|
const initialData = await runBeforeRender(context);
|
|
@@ -42,8 +42,8 @@ function getHookRunners(runtimeContext) {
|
|
|
42
42
|
pickContext: (context) => {
|
|
43
43
|
return hooks.pickContext.call(context);
|
|
44
44
|
},
|
|
45
|
-
|
|
46
|
-
return hooks.
|
|
45
|
+
config: () => {
|
|
46
|
+
return hooks.config.call();
|
|
47
47
|
}
|
|
48
48
|
};
|
|
49
49
|
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
const makeRequestContext = (context) => {
|
|
2
|
+
const baseSSRContext = context.ssrContext;
|
|
3
|
+
const requestContext = baseSSRContext ? {
|
|
4
|
+
isBrowser: context.isBrowser,
|
|
5
|
+
request: baseSSRContext.request || {},
|
|
6
|
+
response: baseSSRContext.response || {},
|
|
7
|
+
logger: baseSSRContext.logger || {}
|
|
8
|
+
} : {};
|
|
9
|
+
return requestContext;
|
|
10
|
+
};
|
|
11
|
+
const requestContextPlugin = () => ({
|
|
12
|
+
name: "@modern-js/runtime-plugin-request-context",
|
|
13
|
+
setup(api) {
|
|
14
|
+
api.onBeforeRender((context) => {
|
|
15
|
+
const requestContext = makeRequestContext(context);
|
|
16
|
+
context.context = requestContext;
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
export {
|
|
21
|
+
makeRequestContext,
|
|
22
|
+
requestContextPlugin
|
|
23
|
+
};
|
|
@@ -76,9 +76,7 @@ const bootstrap = async (BootApp, id, root, ReactDOM) => {
|
|
|
76
76
|
loaderManager: createLoaderManager(initialLoadersState, {
|
|
77
77
|
skipStatic: true
|
|
78
78
|
}),
|
|
79
|
-
|
|
80
|
-
ssrContext: ssrData === null || ssrData === void 0 ? void 0 : ssrData.context
|
|
81
|
-
} : {}
|
|
79
|
+
ssrContext: (ssrData === null || ssrData === void 0 ? void 0 : ssrData.context) || {}
|
|
82
80
|
});
|
|
83
81
|
context.initialData = ssrData === null || ssrData === void 0 ? void 0 : (_ssrData_data1 = ssrData.data) === null || _ssrData_data1 === void 0 ? void 0 : _ssrData_data1.initialData;
|
|
84
82
|
const initialData = await runBeforeRender(context);
|
|
@@ -138,19 +136,9 @@ const bootstrap = async (BootApp, id, root, ReactDOM) => {
|
|
|
138
136
|
const useRuntimeContext = () => {
|
|
139
137
|
var _context_ssrContext, _context_ssrContext1;
|
|
140
138
|
const context = useContext(RuntimeReactContext);
|
|
141
|
-
const baseSSRContext = context.ssrContext;
|
|
142
|
-
const tSSRContext = baseSSRContext ? {
|
|
143
|
-
isBrowser: context.isBrowser,
|
|
144
|
-
request: baseSSRContext.request || {},
|
|
145
|
-
response: baseSSRContext.response || {},
|
|
146
|
-
logger: baseSSRContext.logger || {},
|
|
147
|
-
getInitData: () => {
|
|
148
|
-
return Object.freeze(context.initialData);
|
|
149
|
-
}
|
|
150
|
-
} : {};
|
|
151
139
|
const pickedContext = {
|
|
152
140
|
...context,
|
|
153
|
-
context:
|
|
141
|
+
context: context.context || {},
|
|
154
142
|
request: (_context_ssrContext = context.ssrContext) === null || _context_ssrContext === void 0 ? void 0 : _context_ssrContext.request,
|
|
155
143
|
response: (_context_ssrContext1 = context.ssrContext) === null || _context_ssrContext1 === void 0 ? void 0 : _context_ssrContext1.response
|
|
156
144
|
};
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { RuntimeReactContext, getInitialContext } from "./runtime";
|
|
2
2
|
const globalContext = {};
|
|
3
3
|
function setGlobalContext(context) {
|
|
4
|
+
globalContext.entryName = context.entryName;
|
|
4
5
|
globalContext.App = context.App;
|
|
5
6
|
globalContext.routes = context.routes;
|
|
6
7
|
globalContext.appInit = context.appInit;
|
|
@@ -8,6 +9,9 @@ function setGlobalContext(context) {
|
|
|
8
9
|
globalContext.layoutApp = context.layoutApp;
|
|
9
10
|
globalContext.RSCRoot = context.RSCRoot;
|
|
10
11
|
}
|
|
12
|
+
function getCurrentEntryName() {
|
|
13
|
+
return globalContext.entryName;
|
|
14
|
+
}
|
|
11
15
|
function getGlobalRSCRoot() {
|
|
12
16
|
return globalContext.RSCRoot;
|
|
13
17
|
}
|
|
@@ -36,6 +40,7 @@ function getGlobalLayoutApp() {
|
|
|
36
40
|
}
|
|
37
41
|
export {
|
|
38
42
|
RuntimeReactContext,
|
|
43
|
+
getCurrentEntryName,
|
|
39
44
|
getGlobalApp,
|
|
40
45
|
getGlobalAppConfig,
|
|
41
46
|
getGlobalAppInit,
|
|
@@ -1,16 +1,11 @@
|
|
|
1
1
|
const defaultMonitors = {
|
|
2
2
|
counter(name, ...args) {
|
|
3
3
|
},
|
|
4
|
-
info
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
},
|
|
10
|
-
warn(message, ...args) {
|
|
11
|
-
},
|
|
12
|
-
error(message, ...args) {
|
|
13
|
-
},
|
|
4
|
+
info: console.info,
|
|
5
|
+
debug: console.debug,
|
|
6
|
+
trace: console.trace,
|
|
7
|
+
warn: console.warn,
|
|
8
|
+
error: console.error,
|
|
14
9
|
timing(name, dur, ...args) {
|
|
15
10
|
}
|
|
16
11
|
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
const getResponseProxy = () => {
|
|
2
|
+
return null;
|
|
3
|
+
};
|
|
4
|
+
const setHeaders = (headers) => {
|
|
5
|
+
};
|
|
6
|
+
const setStatus = (status) => {
|
|
7
|
+
};
|
|
8
|
+
const redirect = (url, init) => {
|
|
9
|
+
console.warn(`You should not use this API in the browser, please use the router's redirect or useNavigate method.`);
|
|
10
|
+
};
|
|
11
|
+
export {
|
|
12
|
+
getResponseProxy,
|
|
13
|
+
redirect,
|
|
14
|
+
setHeaders,
|
|
15
|
+
setStatus
|
|
16
|
+
};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { storage } from "@modern-js/runtime-utils/node";
|
|
2
|
+
const getResponseProxy = () => {
|
|
3
|
+
const context = storage.useContext();
|
|
4
|
+
return context === null || context === void 0 ? void 0 : context.responseProxy;
|
|
5
|
+
};
|
|
6
|
+
const setHeaders = (headers) => {
|
|
7
|
+
const responseProxy = getResponseProxy();
|
|
8
|
+
Object.entries(headers).forEach(([key, value]) => {
|
|
9
|
+
responseProxy.headers[key] = value;
|
|
10
|
+
});
|
|
11
|
+
};
|
|
12
|
+
const setStatus = (status) => {
|
|
13
|
+
const responseProxy = getResponseProxy();
|
|
14
|
+
responseProxy.status = status;
|
|
15
|
+
};
|
|
16
|
+
const redirect = (url, init) => {
|
|
17
|
+
var _init_status;
|
|
18
|
+
const status = init === void 0 ? 307 : typeof init === "number" ? init : (_init_status = init.status) !== null && _init_status !== void 0 ? _init_status : 307;
|
|
19
|
+
var _init_headers;
|
|
20
|
+
const headers = init === void 0 ? {} : typeof init === "number" ? {} : (_init_headers = init.headers) !== null && _init_headers !== void 0 ? _init_headers : {};
|
|
21
|
+
setStatus(status);
|
|
22
|
+
setHeaders({
|
|
23
|
+
Location: url,
|
|
24
|
+
...init && typeof init === "object" ? Object.fromEntries(Object.entries(headers).map(([k, v]) => [
|
|
25
|
+
k,
|
|
26
|
+
String(v)
|
|
27
|
+
])) : {}
|
|
28
|
+
});
|
|
29
|
+
};
|
|
30
|
+
export {
|
|
31
|
+
getResponseProxy,
|
|
32
|
+
redirect,
|
|
33
|
+
setHeaders,
|
|
34
|
+
setStatus
|
|
35
|
+
};
|
|
@@ -2,12 +2,14 @@ import { runtime } from "@modern-js/plugin-v2/runtime";
|
|
|
2
2
|
import { merge } from "@modern-js/runtime-utils/merge";
|
|
3
3
|
import { compatPlugin } from "../compat";
|
|
4
4
|
import { handleSetupResult } from "../compat/hooks";
|
|
5
|
+
import { requestContextPlugin } from "../compat/requestContext";
|
|
5
6
|
import { setGlobalInternalRuntimeContext } from "../context";
|
|
6
7
|
function registerPlugin(internalPlugins, runtimeConfig) {
|
|
7
8
|
const { plugins = [] } = runtimeConfig || {};
|
|
8
9
|
const { runtimeContext } = runtime.run({
|
|
9
10
|
plugins: [
|
|
10
11
|
compatPlugin(),
|
|
12
|
+
requestContextPlugin(),
|
|
11
13
|
...internalPlugins,
|
|
12
14
|
...plugins
|
|
13
15
|
],
|
|
@@ -45,6 +45,7 @@ function createSSRContext(request, options) {
|
|
|
45
45
|
params,
|
|
46
46
|
headers: headersData,
|
|
47
47
|
host,
|
|
48
|
+
referer: headers.get("referer"),
|
|
48
49
|
raw: request
|
|
49
50
|
},
|
|
50
51
|
response: {
|
|
@@ -52,7 +53,7 @@ function createSSRContext(request, options) {
|
|
|
52
53
|
responseProxy.headers[key] = value;
|
|
53
54
|
},
|
|
54
55
|
status(code) {
|
|
55
|
-
responseProxy.
|
|
56
|
+
responseProxy.status = code;
|
|
56
57
|
},
|
|
57
58
|
locals: locals || {}
|
|
58
59
|
},
|
|
@@ -66,10 +67,15 @@ function createSSRContext(request, options) {
|
|
|
66
67
|
const createRequestHandler = async (handleRequest, createRequestOptions) => {
|
|
67
68
|
const requestHandler = async (request, options) => {
|
|
68
69
|
const headersData = parseHeaders(request);
|
|
70
|
+
const responseProxy = {
|
|
71
|
+
headers: {},
|
|
72
|
+
status: -1
|
|
73
|
+
};
|
|
69
74
|
return storage.run({
|
|
70
75
|
headers: headersData,
|
|
71
76
|
request,
|
|
72
|
-
monitors: options.monitors
|
|
77
|
+
monitors: options.monitors,
|
|
78
|
+
responseProxy
|
|
73
79
|
}, async () => {
|
|
74
80
|
var _context_routerContext, _context_routerContext1, _context_routerContext2;
|
|
75
81
|
const Root = createRoot();
|
|
@@ -85,10 +91,6 @@ const createRequestHandler = async (handleRequest, createRequestOptions) => {
|
|
|
85
91
|
const init = getGlobalAppInit();
|
|
86
92
|
return init === null || init === void 0 ? void 0 : init(context2);
|
|
87
93
|
};
|
|
88
|
-
const responseProxy = {
|
|
89
|
-
headers: {},
|
|
90
|
-
code: -1
|
|
91
|
-
};
|
|
92
94
|
const ssrContext = createSSRContext(request, {
|
|
93
95
|
...options,
|
|
94
96
|
responseProxy
|
|
@@ -143,9 +145,9 @@ const createRequestHandler = async (handleRequest, createRequestOptions) => {
|
|
|
143
145
|
Object.entries(responseProxy.headers).forEach(([key, value]) => {
|
|
144
146
|
response.headers.set(key, value);
|
|
145
147
|
});
|
|
146
|
-
if (responseProxy.
|
|
148
|
+
if (responseProxy.status !== -1) {
|
|
147
149
|
return new Response(response.body, {
|
|
148
|
-
status: responseProxy.
|
|
150
|
+
status: responseProxy.status,
|
|
149
151
|
headers: response.headers
|
|
150
152
|
});
|
|
151
153
|
}
|
package/dist/esm-node/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { isBrowser } from "./common";
|
|
2
2
|
import { getMonitors } from "./core/context/monitors";
|
|
3
3
|
import { getRequest } from "./core/context/request";
|
|
4
|
+
import { setHeaders, setStatus, redirect } from "./core/context/response";
|
|
4
5
|
import { createApp, useLoader, bootstrap, RuntimeReactContext, defineConfig, defineRuntimeConfig, useRuntimeContext } from "./core";
|
|
5
6
|
export {
|
|
6
7
|
RuntimeReactContext,
|
|
@@ -11,6 +12,9 @@ export {
|
|
|
11
12
|
getMonitors,
|
|
12
13
|
getRequest,
|
|
13
14
|
isBrowser,
|
|
15
|
+
redirect,
|
|
16
|
+
setHeaders,
|
|
17
|
+
setStatus,
|
|
14
18
|
useLoader,
|
|
15
19
|
useRuntimeContext
|
|
16
20
|
};
|
|
@@ -1,4 +1,10 @@
|
|
|
1
1
|
import { getRequest } from "./core/context/request";
|
|
2
|
+
import { getMonitors } from "./core/context/monitors";
|
|
3
|
+
import { setHeaders, setStatus, redirect } from "./core/context/response";
|
|
2
4
|
export {
|
|
3
|
-
|
|
5
|
+
getMonitors,
|
|
6
|
+
getRequest,
|
|
7
|
+
redirect,
|
|
8
|
+
setHeaders,
|
|
9
|
+
setStatus
|
|
4
10
|
};
|
|
@@ -362,7 +362,7 @@ function ssrLoaderCombinedModule(entrypoints, entrypoint, config, appContext) {
|
|
|
362
362
|
}
|
|
363
363
|
return null;
|
|
364
364
|
}
|
|
365
|
-
const runtimeGlobalContext = async ({ metaName, srcDirectory, nestedRoutesEntry, internalSrcAlias, globalApp }) => {
|
|
365
|
+
const runtimeGlobalContext = async ({ entryName, metaName, srcDirectory, nestedRoutesEntry, internalSrcAlias, globalApp }) => {
|
|
366
366
|
const imports = [
|
|
367
367
|
`import { setGlobalContext } from '@${metaName}/runtime/context';`
|
|
368
368
|
];
|
|
@@ -408,7 +408,9 @@ const runtimeGlobalContext = async ({ metaName, srcDirectory, nestedRoutesEntry,
|
|
|
408
408
|
|
|
409
409
|
import { routes } from './routes';
|
|
410
410
|
|
|
411
|
+
const entryName = '${entryName}';
|
|
411
412
|
setGlobalContext({
|
|
413
|
+
entryName,
|
|
412
414
|
layoutApp,
|
|
413
415
|
routes,
|
|
414
416
|
appInit,
|
|
@@ -19,6 +19,7 @@ async function handleGeneratorEntryCode(api, entrypoints) {
|
|
|
19
19
|
if (entrypoint.nestedRoutesEntry || entrypoint.pageRoutesEntry) {
|
|
20
20
|
var _entrypoint_fileSystemRoutes;
|
|
21
21
|
generatorRegisterCode(internalDirectory, entrypoint.entryName, await templates.runtimeGlobalContext({
|
|
22
|
+
entryName: entrypoint.entryName,
|
|
22
23
|
metaName: appContext.metaName,
|
|
23
24
|
srcDirectory: appContext.srcDirectory,
|
|
24
25
|
nestedRoutesEntry: entrypoint.nestedRoutesEntry,
|
|
@@ -25,7 +25,8 @@ export declare const runtimeRegister: ({ entryName, srcDirectory, internalSrcAli
|
|
|
25
25
|
runtimeConfigFile: string | false;
|
|
26
26
|
runtimePlugins: RuntimePluginConfig[];
|
|
27
27
|
}) => string;
|
|
28
|
-
export declare const runtimeGlobalContext: ({ srcDirectory, internalSrcAlias, metaName, entry, customEntry, }: {
|
|
28
|
+
export declare const runtimeGlobalContext: ({ entryName, srcDirectory, internalSrcAlias, metaName, entry, customEntry, }: {
|
|
29
|
+
entryName: string;
|
|
29
30
|
srcDirectory: string;
|
|
30
31
|
internalSrcAlias: string;
|
|
31
32
|
metaName: string;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
/// <reference types="node/http" />
|
|
3
|
+
/// <reference types="types" />
|
|
4
|
+
import type { RuntimeContext } from '../context';
|
|
5
|
+
import type { RuntimePluginFuture } from '../plugin/types';
|
|
6
|
+
export declare const makeRequestContext: (context: RuntimeContext) => import("../types").ServerContext | import("../types").ClientContext | {
|
|
7
|
+
isBrowser: boolean;
|
|
8
|
+
request: {
|
|
9
|
+
[propsName: string]: any;
|
|
10
|
+
params: Record<string, string>;
|
|
11
|
+
pathname: string;
|
|
12
|
+
query: Record<string, string>;
|
|
13
|
+
headers: import("http").IncomingHttpHeaders;
|
|
14
|
+
host: string;
|
|
15
|
+
url: string;
|
|
16
|
+
referer?: string | undefined;
|
|
17
|
+
userAgent?: string | undefined;
|
|
18
|
+
cookie?: string | undefined;
|
|
19
|
+
cookieMap?: Record<string, string> | undefined;
|
|
20
|
+
} & {
|
|
21
|
+
baseUrl: string;
|
|
22
|
+
raw: Request;
|
|
23
|
+
};
|
|
24
|
+
response: import("@modern-js/types").BaseResponseLike;
|
|
25
|
+
logger: import("@modern-js/types").Logger;
|
|
26
|
+
};
|
|
27
|
+
export declare const requestContextPlugin: () => RuntimePluginFuture;
|
|
@@ -10,4 +10,4 @@ export declare const defineConfig: (Component: React.ComponentType<any>, config:
|
|
|
10
10
|
* This function helps you to autocomplete configuration types.
|
|
11
11
|
* It accepts a direct config object, or a function that returns a config.
|
|
12
12
|
*/
|
|
13
|
-
export declare const defineRuntimeConfig: (config: RuntimeConfig) => RuntimeConfig;
|
|
13
|
+
export declare const defineRuntimeConfig: (config: RuntimeConfig | ((entryName: string) => RuntimeConfig)) => RuntimeConfig | ((entryName: string) => RuntimeConfig);
|