@modern-js/prod-server 2.43.0 → 2.45.0
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/libs/hook-api/afterRenderForStream.js +34 -0
- package/dist/cjs/libs/hook-api/index.js +12 -0
- package/dist/cjs/libs/metrics.js +0 -2
- package/dist/cjs/libs/render/index.js +1 -3
- package/dist/cjs/libs/render/measure.js +2 -2
- package/dist/cjs/libs/render/ssr.js +13 -2
- package/dist/cjs/libs/render/ssrCache/manager.js +6 -6
- package/dist/cjs/workerServer.js +0 -3
- package/dist/esm/libs/hook-api/afterRenderForStream.js +33 -0
- package/dist/esm/libs/hook-api/index.js +10 -0
- package/dist/esm/libs/metrics.js +0 -2
- package/dist/esm/libs/render/index.js +1 -3
- package/dist/esm/libs/render/measure.js +2 -2
- package/dist/esm/libs/render/ssr.js +17 -3
- package/dist/esm/libs/render/ssrCache/manager.js +6 -6
- package/dist/esm/workerServer.js +0 -9
- package/dist/esm-node/libs/hook-api/afterRenderForStream.js +10 -0
- package/dist/esm-node/libs/hook-api/index.js +11 -0
- package/dist/esm-node/libs/metrics.js +0 -2
- package/dist/esm-node/libs/render/index.js +1 -3
- package/dist/esm-node/libs/render/measure.js +2 -2
- package/dist/esm-node/libs/render/ssr.js +13 -2
- package/dist/esm-node/libs/render/ssrCache/manager.js +6 -6
- package/dist/esm-node/workerServer.js +0 -3
- package/dist/types/libs/hook-api/afterRenderForStream.d.ts +4 -0
- package/dist/types/libs/hook-api/index.d.ts +2 -1
- package/dist/types/libs/render/ssr.d.ts +5 -5
- package/dist/types/libs/render/ssrCache/manager.d.ts +2 -2
- package/dist/types/server/modernServer.d.ts +2 -1
- package/dist/types/type.d.ts +2 -1
- package/package.json +8 -8
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var afterRenderForStream_exports = {};
|
|
20
|
+
__export(afterRenderForStream_exports, {
|
|
21
|
+
afterRenderInjectableStream: () => afterRenderInjectableStream
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(afterRenderForStream_exports);
|
|
24
|
+
var import_stream = require("stream");
|
|
25
|
+
const afterRenderInjectableStream = (fn) => new import_stream.Transform({
|
|
26
|
+
async write(chunk, _, callback) {
|
|
27
|
+
this.push(await fn(chunk.toString()));
|
|
28
|
+
callback();
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
32
|
+
0 && (module.exports = {
|
|
33
|
+
afterRenderInjectableStream
|
|
34
|
+
});
|
|
@@ -21,6 +21,7 @@ __export(hook_api_exports, {
|
|
|
21
21
|
base: () => base,
|
|
22
22
|
createAfterMatchContext: () => createAfterMatchContext,
|
|
23
23
|
createAfterRenderContext: () => createAfterRenderContext,
|
|
24
|
+
createAfterStreamingRenderContext: () => createAfterStreamingRenderContext,
|
|
24
25
|
createMiddlewareContext: () => createMiddlewareContext
|
|
25
26
|
});
|
|
26
27
|
module.exports = __toCommonJS(hook_api_exports);
|
|
@@ -51,6 +52,16 @@ const createAfterRenderContext = (context, route, content) => {
|
|
|
51
52
|
template: new import_template.TemplateAPI(content)
|
|
52
53
|
};
|
|
53
54
|
};
|
|
55
|
+
const createAfterStreamingRenderContext = (context, route) => {
|
|
56
|
+
const baseContext = base(context);
|
|
57
|
+
return (chunk) => {
|
|
58
|
+
return {
|
|
59
|
+
...baseContext,
|
|
60
|
+
route,
|
|
61
|
+
chunk
|
|
62
|
+
};
|
|
63
|
+
};
|
|
64
|
+
};
|
|
54
65
|
const createMiddlewareContext = (context) => {
|
|
55
66
|
const baseContext = base(context);
|
|
56
67
|
baseContext.response.locals = context.res.locals || {};
|
|
@@ -69,5 +80,6 @@ const createMiddlewareContext = (context) => {
|
|
|
69
80
|
base,
|
|
70
81
|
createAfterMatchContext,
|
|
71
82
|
createAfterRenderContext,
|
|
83
|
+
createAfterStreamingRenderContext,
|
|
72
84
|
createMiddlewareContext
|
|
73
85
|
});
|
package/dist/cjs/libs/metrics.js
CHANGED
|
@@ -74,9 +74,7 @@ const createRenderHandler = ({ distDir, staticGenerate, conf, forceCSR, nonce, s
|
|
|
74
74
|
}
|
|
75
75
|
const ssrRenderOptions = {
|
|
76
76
|
distDir,
|
|
77
|
-
|
|
78
|
-
urlPath: route.urlPath,
|
|
79
|
-
bundle: route.bundle,
|
|
77
|
+
route,
|
|
80
78
|
template: content.toString(),
|
|
81
79
|
staticGenerate,
|
|
82
80
|
nonce
|
|
@@ -27,14 +27,14 @@ const createMetrics = (context, metrics) => {
|
|
|
27
27
|
const { entryName: entry, request } = context;
|
|
28
28
|
const { pathname = "" } = request || {};
|
|
29
29
|
const emitTimer = (name, cost, tags = {}) => {
|
|
30
|
-
metrics.emitTimer(name, cost, {
|
|
30
|
+
metrics.emitTimer(name, cost, void 0, {
|
|
31
31
|
...tags,
|
|
32
32
|
pathname,
|
|
33
33
|
entry
|
|
34
34
|
});
|
|
35
35
|
};
|
|
36
36
|
const emitCounter = (name, counter, tags = {}) => {
|
|
37
|
-
metrics.emitCounter(name, counter, {
|
|
37
|
+
metrics.emitCounter(name, counter, void 0, {
|
|
38
38
|
...tags,
|
|
39
39
|
pathname,
|
|
40
40
|
entry
|
|
@@ -33,12 +33,15 @@ __export(ssr_exports, {
|
|
|
33
33
|
module.exports = __toCommonJS(ssr_exports);
|
|
34
34
|
var import_path = __toESM(require("path"));
|
|
35
35
|
var import_utils = require("@modern-js/utils");
|
|
36
|
+
var import_hook_api = require("../hook-api");
|
|
37
|
+
var import_afterRenderForStream = require("../hook-api/afterRenderForStream");
|
|
36
38
|
var import_measure = require("./measure");
|
|
37
39
|
var import_utils2 = require("./utils");
|
|
38
40
|
var import_ssrCache = require("./ssrCache");
|
|
39
41
|
const render = async (ctx, renderOptions, runner) => {
|
|
40
42
|
var _ctx_res;
|
|
41
|
-
const {
|
|
43
|
+
const { distDir, route, template, staticGenerate, enableUnsafeCtx = false, nonce } = renderOptions;
|
|
44
|
+
const { urlPath, bundle, entryName } = route;
|
|
42
45
|
const bundleJS = import_path.default.join(distDir, bundle);
|
|
43
46
|
const loadableUri = import_path.default.join(distDir, import_utils.LOADABLE_STATS_FILE);
|
|
44
47
|
const loadableStats = import_utils.fs.existsSync(loadableUri) ? require(loadableUri) : "";
|
|
@@ -99,9 +102,17 @@ const render = async (ctx, renderOptions, runner) => {
|
|
|
99
102
|
contentType: import_utils.mime.contentType("html")
|
|
100
103
|
};
|
|
101
104
|
} else {
|
|
105
|
+
let contentStream = (0, import_utils2.injectServerDataStream)(content, ctx);
|
|
106
|
+
const afterStreamingRenderContext = (0, import_hook_api.createAfterStreamingRenderContext)(ctx, route);
|
|
107
|
+
contentStream = contentStream.pipe((0, import_afterRenderForStream.afterRenderInjectableStream)((chunk) => {
|
|
108
|
+
const context2 = afterStreamingRenderContext(chunk);
|
|
109
|
+
return runner.afterStreamingRender(context2, {
|
|
110
|
+
onLast: ({ chunk: chunk2 }) => chunk2
|
|
111
|
+
});
|
|
112
|
+
}));
|
|
102
113
|
return {
|
|
103
114
|
content: "",
|
|
104
|
-
contentStream
|
|
115
|
+
contentStream,
|
|
105
116
|
contentType: import_utils.mime.contentType("html")
|
|
106
117
|
};
|
|
107
118
|
}
|
|
@@ -26,7 +26,7 @@ var import_stream = require("stream");
|
|
|
26
26
|
class CacheManager {
|
|
27
27
|
async getCacheResult(req, cacheControl, render, ssrContext) {
|
|
28
28
|
const key = this.computedKey(req, cacheControl);
|
|
29
|
-
const value = await this.
|
|
29
|
+
const value = await this.container.get(key);
|
|
30
30
|
const { maxAge, staleWhileRevalidate } = cacheControl;
|
|
31
31
|
const ttl = maxAge + staleWhileRevalidate;
|
|
32
32
|
if (value) {
|
|
@@ -52,7 +52,7 @@ class CacheManager {
|
|
|
52
52
|
val: renderResult,
|
|
53
53
|
cursor: current
|
|
54
54
|
};
|
|
55
|
-
await this.
|
|
55
|
+
await this.container.set(key, JSON.stringify(cache), {
|
|
56
56
|
ttl
|
|
57
57
|
});
|
|
58
58
|
return renderResult;
|
|
@@ -71,7 +71,7 @@ class CacheManager {
|
|
|
71
71
|
val: html,
|
|
72
72
|
cursor: current
|
|
73
73
|
};
|
|
74
|
-
this.
|
|
74
|
+
this.container.set(key, JSON.stringify(cache), {
|
|
75
75
|
ttl
|
|
76
76
|
});
|
|
77
77
|
});
|
|
@@ -93,9 +93,9 @@ class CacheManager {
|
|
|
93
93
|
return defaultKey;
|
|
94
94
|
}
|
|
95
95
|
}
|
|
96
|
-
constructor(
|
|
97
|
-
(0, import_define_property._)(this, "
|
|
98
|
-
this.
|
|
96
|
+
constructor(container) {
|
|
97
|
+
(0, import_define_property._)(this, "container", void 0);
|
|
98
|
+
this.container = container;
|
|
99
99
|
}
|
|
100
100
|
}
|
|
101
101
|
// Annotate the CommonJS export names for ESM import in node:
|
package/dist/cjs/workerServer.js
CHANGED
|
@@ -40,9 +40,6 @@ const createLogger = () => ({
|
|
|
40
40
|
},
|
|
41
41
|
debug(message, ...args) {
|
|
42
42
|
console.debug(message, ...args);
|
|
43
|
-
},
|
|
44
|
-
log(message, ...args) {
|
|
45
|
-
console.log(message, ...args);
|
|
46
43
|
}
|
|
47
44
|
});
|
|
48
45
|
const calcFallback = (metaName) => `x-${metaName.split(/[-_]/)[0]}-ssr-fallback`;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
|
|
2
|
+
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
|
|
3
|
+
import { Transform } from "stream";
|
|
4
|
+
var afterRenderInjectableStream = function(fn) {
|
|
5
|
+
return new Transform({
|
|
6
|
+
write: function write(chunk, _, callback) {
|
|
7
|
+
return _async_to_generator(function() {
|
|
8
|
+
var _2, _1;
|
|
9
|
+
return _ts_generator(this, function(_state) {
|
|
10
|
+
switch (_state.label) {
|
|
11
|
+
case 0:
|
|
12
|
+
_1 = (_2 = this).push;
|
|
13
|
+
return [
|
|
14
|
+
4,
|
|
15
|
+
fn(chunk.toString())
|
|
16
|
+
];
|
|
17
|
+
case 1:
|
|
18
|
+
_1.apply(_2, [
|
|
19
|
+
_state.sent()
|
|
20
|
+
]);
|
|
21
|
+
callback();
|
|
22
|
+
return [
|
|
23
|
+
2
|
|
24
|
+
];
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
}).apply(this);
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
};
|
|
31
|
+
export {
|
|
32
|
+
afterRenderInjectableStream
|
|
33
|
+
};
|
|
@@ -25,6 +25,15 @@ var createAfterRenderContext = function(context, route, content) {
|
|
|
25
25
|
template: new TemplateAPI(content)
|
|
26
26
|
});
|
|
27
27
|
};
|
|
28
|
+
var createAfterStreamingRenderContext = function(context, route) {
|
|
29
|
+
var baseContext = base(context);
|
|
30
|
+
return function(chunk) {
|
|
31
|
+
return _object_spread_props(_object_spread({}, baseContext), {
|
|
32
|
+
route,
|
|
33
|
+
chunk
|
|
34
|
+
});
|
|
35
|
+
};
|
|
36
|
+
};
|
|
28
37
|
var createMiddlewareContext = function(context) {
|
|
29
38
|
var baseContext = base(context);
|
|
30
39
|
baseContext.response.locals = context.res.locals || {};
|
|
@@ -41,5 +50,6 @@ export {
|
|
|
41
50
|
base,
|
|
42
51
|
createAfterMatchContext,
|
|
43
52
|
createAfterRenderContext,
|
|
53
|
+
createAfterStreamingRenderContext,
|
|
44
54
|
createMiddlewareContext
|
|
45
55
|
};
|
package/dist/esm/libs/metrics.js
CHANGED
|
@@ -8,14 +8,14 @@ var createMetrics = function(context, metrics) {
|
|
|
8
8
|
var _ref = request || {}, _ref_pathname = _ref.pathname, pathname = _ref_pathname === void 0 ? "" : _ref_pathname;
|
|
9
9
|
var emitTimer = function(name, cost) {
|
|
10
10
|
var tags = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : {};
|
|
11
|
-
metrics.emitTimer(name, cost, _object_spread_props(_object_spread({}, tags), {
|
|
11
|
+
metrics.emitTimer(name, cost, void 0, _object_spread_props(_object_spread({}, tags), {
|
|
12
12
|
pathname,
|
|
13
13
|
entry
|
|
14
14
|
}));
|
|
15
15
|
};
|
|
16
16
|
var emitCounter = function(name, counter) {
|
|
17
17
|
var tags = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : {};
|
|
18
|
-
metrics.emitCounter(name, counter, _object_spread_props(_object_spread({}, tags), {
|
|
18
|
+
metrics.emitCounter(name, counter, void 0, _object_spread_props(_object_spread({}, tags), {
|
|
19
19
|
pathname,
|
|
20
20
|
entry
|
|
21
21
|
}));
|
|
@@ -2,16 +2,19 @@ import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
|
|
|
2
2
|
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
|
|
3
3
|
import path from "path";
|
|
4
4
|
import { fs, mime, LOADABLE_STATS_FILE, ROUTE_MANIFEST_FILE, SERVER_RENDER_FUNCTION_NAME } from "@modern-js/utils";
|
|
5
|
+
import { createAfterStreamingRenderContext } from "../hook-api";
|
|
6
|
+
import { afterRenderInjectableStream } from "../hook-api/afterRenderForStream";
|
|
5
7
|
import { createLogger, createMetrics } from "./measure";
|
|
6
8
|
import { injectServerDataStream, injectServerData } from "./utils";
|
|
7
9
|
import { ssrCache } from "./ssrCache";
|
|
8
10
|
var render = function() {
|
|
9
11
|
var _ref = _async_to_generator(function(ctx, renderOptions, runner) {
|
|
10
|
-
var _ctx_res,
|
|
12
|
+
var _ctx_res, distDir, route, template, staticGenerate, _renderOptions_enableUnsafeCtx, enableUnsafeCtx, nonce, urlPath, bundle, entryName, bundleJS, loadableUri, loadableStats, routesManifestUri, routeManifest, context, bundleJSContent, serverRender, content, _context_redirection, url, _context_redirection_status, status, contentStream, afterStreamingRenderContext;
|
|
11
13
|
return _ts_generator(this, function(_state) {
|
|
12
14
|
switch (_state.label) {
|
|
13
15
|
case 0:
|
|
14
|
-
|
|
16
|
+
distDir = renderOptions.distDir, route = renderOptions.route, template = renderOptions.template, staticGenerate = renderOptions.staticGenerate, _renderOptions_enableUnsafeCtx = renderOptions.enableUnsafeCtx, enableUnsafeCtx = _renderOptions_enableUnsafeCtx === void 0 ? false : _renderOptions_enableUnsafeCtx, nonce = renderOptions.nonce;
|
|
17
|
+
urlPath = route.urlPath, bundle = route.bundle, entryName = route.entryName;
|
|
15
18
|
bundleJS = path.join(distDir, bundle);
|
|
16
19
|
loadableUri = path.join(distDir, LOADABLE_STATS_FILE);
|
|
17
20
|
loadableStats = fs.existsSync(loadableUri) ? require(loadableUri) : "";
|
|
@@ -88,11 +91,22 @@ var render = function() {
|
|
|
88
91
|
}
|
|
89
92
|
];
|
|
90
93
|
} else {
|
|
94
|
+
contentStream = injectServerDataStream(content, ctx);
|
|
95
|
+
afterStreamingRenderContext = createAfterStreamingRenderContext(ctx, route);
|
|
96
|
+
contentStream = contentStream.pipe(afterRenderInjectableStream(function(chunk) {
|
|
97
|
+
var context2 = afterStreamingRenderContext(chunk);
|
|
98
|
+
return runner.afterStreamingRender(context2, {
|
|
99
|
+
onLast: function(param) {
|
|
100
|
+
var _$chunk = param.chunk;
|
|
101
|
+
return _$chunk;
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
}));
|
|
91
105
|
return [
|
|
92
106
|
2,
|
|
93
107
|
{
|
|
94
108
|
content: "",
|
|
95
|
-
contentStream
|
|
109
|
+
contentStream,
|
|
96
110
|
contentType: mime.contentType("html")
|
|
97
111
|
}
|
|
98
112
|
];
|
|
@@ -7,10 +7,10 @@ import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
|
|
|
7
7
|
import { Transform } from "stream";
|
|
8
8
|
var CacheManager = /* @__PURE__ */ function() {
|
|
9
9
|
"use strict";
|
|
10
|
-
function CacheManager2(
|
|
10
|
+
function CacheManager2(container) {
|
|
11
11
|
_class_call_check(this, CacheManager2);
|
|
12
|
-
_define_property(this, "
|
|
13
|
-
this.
|
|
12
|
+
_define_property(this, "container", void 0);
|
|
13
|
+
this.container = container;
|
|
14
14
|
}
|
|
15
15
|
_create_class(CacheManager2, [
|
|
16
16
|
{
|
|
@@ -25,7 +25,7 @@ var CacheManager = /* @__PURE__ */ function() {
|
|
|
25
25
|
key = _this.computedKey(req, cacheControl);
|
|
26
26
|
return [
|
|
27
27
|
4,
|
|
28
|
-
_this.
|
|
28
|
+
_this.container.get(key)
|
|
29
29
|
];
|
|
30
30
|
case 1:
|
|
31
31
|
value = _state.sent();
|
|
@@ -92,7 +92,7 @@ var CacheManager = /* @__PURE__ */ function() {
|
|
|
92
92
|
};
|
|
93
93
|
return [
|
|
94
94
|
4,
|
|
95
|
-
_this.
|
|
95
|
+
_this.container.set(key, JSON.stringify(cache), {
|
|
96
96
|
ttl
|
|
97
97
|
})
|
|
98
98
|
];
|
|
@@ -116,7 +116,7 @@ var CacheManager = /* @__PURE__ */ function() {
|
|
|
116
116
|
val: html,
|
|
117
117
|
cursor: current2
|
|
118
118
|
};
|
|
119
|
-
_this.
|
|
119
|
+
_this.container.set(key, JSON.stringify(cache2), {
|
|
120
120
|
ttl
|
|
121
121
|
});
|
|
122
122
|
});
|
package/dist/esm/workerServer.js
CHANGED
|
@@ -47,15 +47,6 @@ var createLogger = function() {
|
|
|
47
47
|
(_console = console).debug.apply(_console, [
|
|
48
48
|
message
|
|
49
49
|
].concat(_to_consumable_array(args)));
|
|
50
|
-
},
|
|
51
|
-
log: function log(message) {
|
|
52
|
-
for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
|
53
|
-
args[_key - 1] = arguments[_key];
|
|
54
|
-
}
|
|
55
|
-
var _console;
|
|
56
|
-
(_console = console).log.apply(_console, [
|
|
57
|
-
message
|
|
58
|
-
].concat(_to_consumable_array(args)));
|
|
59
50
|
}
|
|
60
51
|
};
|
|
61
52
|
};
|
|
@@ -25,6 +25,16 @@ const createAfterRenderContext = (context, route, content) => {
|
|
|
25
25
|
template: new TemplateAPI(content)
|
|
26
26
|
};
|
|
27
27
|
};
|
|
28
|
+
const createAfterStreamingRenderContext = (context, route) => {
|
|
29
|
+
const baseContext = base(context);
|
|
30
|
+
return (chunk) => {
|
|
31
|
+
return {
|
|
32
|
+
...baseContext,
|
|
33
|
+
route,
|
|
34
|
+
chunk
|
|
35
|
+
};
|
|
36
|
+
};
|
|
37
|
+
};
|
|
28
38
|
const createMiddlewareContext = (context) => {
|
|
29
39
|
const baseContext = base(context);
|
|
30
40
|
baseContext.response.locals = context.res.locals || {};
|
|
@@ -42,5 +52,6 @@ export {
|
|
|
42
52
|
base,
|
|
43
53
|
createAfterMatchContext,
|
|
44
54
|
createAfterRenderContext,
|
|
55
|
+
createAfterStreamingRenderContext,
|
|
45
56
|
createMiddlewareContext
|
|
46
57
|
};
|
|
@@ -41,9 +41,7 @@ const createRenderHandler = ({ distDir, staticGenerate, conf, forceCSR, nonce, s
|
|
|
41
41
|
}
|
|
42
42
|
const ssrRenderOptions = {
|
|
43
43
|
distDir,
|
|
44
|
-
|
|
45
|
-
urlPath: route.urlPath,
|
|
46
|
-
bundle: route.bundle,
|
|
44
|
+
route,
|
|
47
45
|
template: content.toString(),
|
|
48
46
|
staticGenerate,
|
|
49
47
|
nonce
|
|
@@ -3,14 +3,14 @@ const createMetrics = (context, metrics) => {
|
|
|
3
3
|
const { entryName: entry, request } = context;
|
|
4
4
|
const { pathname = "" } = request || {};
|
|
5
5
|
const emitTimer = (name, cost, tags = {}) => {
|
|
6
|
-
metrics.emitTimer(name, cost, {
|
|
6
|
+
metrics.emitTimer(name, cost, void 0, {
|
|
7
7
|
...tags,
|
|
8
8
|
pathname,
|
|
9
9
|
entry
|
|
10
10
|
});
|
|
11
11
|
};
|
|
12
12
|
const emitCounter = (name, counter, tags = {}) => {
|
|
13
|
-
metrics.emitCounter(name, counter, {
|
|
13
|
+
metrics.emitCounter(name, counter, void 0, {
|
|
14
14
|
...tags,
|
|
15
15
|
pathname,
|
|
16
16
|
entry
|
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
import path from "path";
|
|
2
2
|
import { fs, mime, LOADABLE_STATS_FILE, ROUTE_MANIFEST_FILE, SERVER_RENDER_FUNCTION_NAME } from "@modern-js/utils";
|
|
3
|
+
import { createAfterStreamingRenderContext } from "../hook-api";
|
|
4
|
+
import { afterRenderInjectableStream } from "../hook-api/afterRenderForStream";
|
|
3
5
|
import { createLogger, createMetrics } from "./measure";
|
|
4
6
|
import { injectServerDataStream, injectServerData } from "./utils";
|
|
5
7
|
import { ssrCache } from "./ssrCache";
|
|
6
8
|
const render = async (ctx, renderOptions, runner) => {
|
|
7
9
|
var _ctx_res;
|
|
8
|
-
const {
|
|
10
|
+
const { distDir, route, template, staticGenerate, enableUnsafeCtx = false, nonce } = renderOptions;
|
|
11
|
+
const { urlPath, bundle, entryName } = route;
|
|
9
12
|
const bundleJS = path.join(distDir, bundle);
|
|
10
13
|
const loadableUri = path.join(distDir, LOADABLE_STATS_FILE);
|
|
11
14
|
const loadableStats = fs.existsSync(loadableUri) ? require(loadableUri) : "";
|
|
@@ -66,9 +69,17 @@ const render = async (ctx, renderOptions, runner) => {
|
|
|
66
69
|
contentType: mime.contentType("html")
|
|
67
70
|
};
|
|
68
71
|
} else {
|
|
72
|
+
let contentStream = injectServerDataStream(content, ctx);
|
|
73
|
+
const afterStreamingRenderContext = createAfterStreamingRenderContext(ctx, route);
|
|
74
|
+
contentStream = contentStream.pipe(afterRenderInjectableStream((chunk) => {
|
|
75
|
+
const context2 = afterStreamingRenderContext(chunk);
|
|
76
|
+
return runner.afterStreamingRender(context2, {
|
|
77
|
+
onLast: ({ chunk: chunk2 }) => chunk2
|
|
78
|
+
});
|
|
79
|
+
}));
|
|
69
80
|
return {
|
|
70
81
|
content: "",
|
|
71
|
-
contentStream
|
|
82
|
+
contentStream,
|
|
72
83
|
contentType: mime.contentType("html")
|
|
73
84
|
};
|
|
74
85
|
}
|
|
@@ -3,7 +3,7 @@ import { Transform } from "stream";
|
|
|
3
3
|
class CacheManager {
|
|
4
4
|
async getCacheResult(req, cacheControl, render, ssrContext) {
|
|
5
5
|
const key = this.computedKey(req, cacheControl);
|
|
6
|
-
const value = await this.
|
|
6
|
+
const value = await this.container.get(key);
|
|
7
7
|
const { maxAge, staleWhileRevalidate } = cacheControl;
|
|
8
8
|
const ttl = maxAge + staleWhileRevalidate;
|
|
9
9
|
if (value) {
|
|
@@ -29,7 +29,7 @@ class CacheManager {
|
|
|
29
29
|
val: renderResult,
|
|
30
30
|
cursor: current
|
|
31
31
|
};
|
|
32
|
-
await this.
|
|
32
|
+
await this.container.set(key, JSON.stringify(cache), {
|
|
33
33
|
ttl
|
|
34
34
|
});
|
|
35
35
|
return renderResult;
|
|
@@ -48,7 +48,7 @@ class CacheManager {
|
|
|
48
48
|
val: html,
|
|
49
49
|
cursor: current
|
|
50
50
|
};
|
|
51
|
-
this.
|
|
51
|
+
this.container.set(key, JSON.stringify(cache), {
|
|
52
52
|
ttl
|
|
53
53
|
});
|
|
54
54
|
});
|
|
@@ -70,9 +70,9 @@ class CacheManager {
|
|
|
70
70
|
return defaultKey;
|
|
71
71
|
}
|
|
72
72
|
}
|
|
73
|
-
constructor(
|
|
74
|
-
_define_property(this, "
|
|
75
|
-
this.
|
|
73
|
+
constructor(container) {
|
|
74
|
+
_define_property(this, "container", void 0);
|
|
75
|
+
this.container = container;
|
|
76
76
|
}
|
|
77
77
|
}
|
|
78
78
|
export {
|
|
@@ -16,9 +16,6 @@ const createLogger = () => ({
|
|
|
16
16
|
},
|
|
17
17
|
debug(message, ...args) {
|
|
18
18
|
console.debug(message, ...args);
|
|
19
|
-
},
|
|
20
|
-
log(message, ...args) {
|
|
21
|
-
console.log(message, ...args);
|
|
22
19
|
}
|
|
23
20
|
});
|
|
24
21
|
const calcFallback = (metaName) => `x-${metaName.split(/[-_]/)[0]}-ssr-fallback`;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import type { ModernServerContext, HookContext, AfterMatchContext, AfterRenderContext, MiddlewareContext, ServerRoute } from '@modern-js/types';
|
|
1
|
+
import type { ModernServerContext, HookContext, AfterMatchContext, AfterRenderContext, MiddlewareContext, ServerRoute, AfterStreamingRenderContext } from '@modern-js/types';
|
|
2
2
|
export declare const base: (context: ModernServerContext) => HookContext;
|
|
3
3
|
export declare const createAfterMatchContext: (context: ModernServerContext, entryName: string) => AfterMatchContext;
|
|
4
4
|
export declare const createAfterRenderContext: (context: ModernServerContext, route: Partial<ServerRoute>, content: string) => AfterRenderContext;
|
|
5
|
+
export declare const createAfterStreamingRenderContext: (context: ModernServerContext, route: Partial<ServerRoute>) => (chunk: string) => AfterStreamingRenderContext;
|
|
5
6
|
export declare const createMiddlewareContext: (context: ModernServerContext) => MiddlewareContext;
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import type { ModernServerContext } from '@modern-js/types';
|
|
2
2
|
import { RenderResult, ServerHookRunner } from '../../type';
|
|
3
|
-
|
|
3
|
+
import type { ModernRoute } from '../route';
|
|
4
|
+
export type SSRRenderOptions = {
|
|
4
5
|
distDir: string;
|
|
5
|
-
bundle: string;
|
|
6
|
-
urlPath: string;
|
|
7
6
|
template: string;
|
|
8
|
-
|
|
7
|
+
route: ModernRoute;
|
|
9
8
|
staticGenerate: boolean;
|
|
10
9
|
enableUnsafeCtx?: boolean;
|
|
11
10
|
nonce?: string;
|
|
12
|
-
}
|
|
11
|
+
};
|
|
12
|
+
export declare const render: (ctx: ModernServerContext, renderOptions: SSRRenderOptions, runner: ServerHookRunner) => Promise<RenderResult>;
|
|
@@ -6,8 +6,8 @@ import { Readable } from 'stream';
|
|
|
6
6
|
import { CacheControl, Container } from '@modern-js/types';
|
|
7
7
|
import { RenderFunction, SSRServerContext } from '../type';
|
|
8
8
|
export declare class CacheManager {
|
|
9
|
-
private
|
|
10
|
-
constructor(
|
|
9
|
+
private container;
|
|
10
|
+
constructor(container: Container<string, string>);
|
|
11
11
|
getCacheResult(req: IncomingMessage, cacheControl: CacheControl, render: RenderFunction, ssrContext: SSRServerContext): Promise<string | Readable>;
|
|
12
12
|
private processCache;
|
|
13
13
|
private computedKey;
|
|
@@ -5,8 +5,9 @@
|
|
|
5
5
|
import { IncomingMessage, ServerResponse, Server } from 'http';
|
|
6
6
|
import { Adapter, WebAdapter, APIServerStartInput, ServerOptions } from '@modern-js/server-core';
|
|
7
7
|
import { type ModernServerContext, type ServerRoute } from '@modern-js/types';
|
|
8
|
+
import { Logger } from '@modern-js/utils/logger';
|
|
8
9
|
import type { ContextOptions } from '../libs/context';
|
|
9
|
-
import { ModernServerOptions, NextFunction, ServerHookRunner, Metrics,
|
|
10
|
+
import { ModernServerOptions, NextFunction, ServerHookRunner, Metrics, ModernServerInterface, BuildOptions, ModernServerHandler } from '../type';
|
|
10
11
|
import { RouteMatchManager, ModernRouteInterface, ModernRoute } from '../libs/route';
|
|
11
12
|
import { RenderHandler } from '../libs/render';
|
|
12
13
|
import { mergeExtension } from '../utils';
|
package/dist/types/type.d.ts
CHANGED
|
@@ -7,6 +7,7 @@ import { Readable } from 'stream';
|
|
|
7
7
|
import { serverManager, ServerOptions } from '@modern-js/server-core';
|
|
8
8
|
import type { ServerPlugin } from '@modern-js/server-core';
|
|
9
9
|
import type { Metrics, Logger, NextFunction, ModernServerContext, InternalPlugins, Reporter } from '@modern-js/types';
|
|
10
|
+
import { Logger as LocalLogger } from '@modern-js/utils/logger';
|
|
10
11
|
import type { ModernRouteInterface } from './libs/route';
|
|
11
12
|
declare module 'http' {
|
|
12
13
|
interface IncomingMessage {
|
|
@@ -27,7 +28,7 @@ export type ModernServerOptions = {
|
|
|
27
28
|
internalPlugins?: InternalPlugins;
|
|
28
29
|
routes?: ModernRouteInterface[];
|
|
29
30
|
staticGenerate?: boolean;
|
|
30
|
-
logger?:
|
|
31
|
+
logger?: LocalLogger;
|
|
31
32
|
metrics?: Metrics;
|
|
32
33
|
apiOnly?: boolean;
|
|
33
34
|
ssrOnly?: boolean;
|
package/package.json
CHANGED
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
"modern",
|
|
16
16
|
"modern.js"
|
|
17
17
|
],
|
|
18
|
-
"version": "2.
|
|
18
|
+
"version": "2.45.0",
|
|
19
19
|
"jsnext:source": "./src/index.ts",
|
|
20
20
|
"types": "./dist/types/index.d.ts",
|
|
21
21
|
"main": "./dist/cjs/index.js",
|
|
@@ -73,10 +73,10 @@
|
|
|
73
73
|
"node-html-parser": "^6.1.5",
|
|
74
74
|
"path-to-regexp": "^6.2.0",
|
|
75
75
|
"serve-static": "^1.14.1",
|
|
76
|
-
"@modern-js/
|
|
77
|
-
"@modern-js/
|
|
78
|
-
"@modern-js/runtime-utils": "2.
|
|
79
|
-
"@modern-js/
|
|
76
|
+
"@modern-js/plugin": "2.45.0",
|
|
77
|
+
"@modern-js/server-core": "2.45.0",
|
|
78
|
+
"@modern-js/runtime-utils": "2.45.0",
|
|
79
|
+
"@modern-js/utils": "2.45.0"
|
|
80
80
|
},
|
|
81
81
|
"devDependencies": {
|
|
82
82
|
"@types/cookie": "0.5.1",
|
|
@@ -94,9 +94,9 @@
|
|
|
94
94
|
"portfinder": "^1.0.28",
|
|
95
95
|
"typescript": "^5",
|
|
96
96
|
"ws": "^8.13.0",
|
|
97
|
-
"@modern-js/types": "2.
|
|
98
|
-
"@scripts/
|
|
99
|
-
"@scripts/
|
|
97
|
+
"@modern-js/types": "2.45.0",
|
|
98
|
+
"@scripts/build": "2.45.0",
|
|
99
|
+
"@scripts/jest-config": "2.45.0"
|
|
100
100
|
},
|
|
101
101
|
"sideEffects": false,
|
|
102
102
|
"publishConfig": {
|