@modern-js/prod-server 2.42.2 → 2.44.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/{render/cache/page-caches/index.js → hook-api/afterRenderForStream.js} +12 -14
- package/dist/cjs/libs/hook-api/index.js +12 -0
- package/dist/cjs/libs/render/index.js +4 -14
- package/dist/cjs/libs/render/ssr.js +15 -4
- package/dist/cjs/libs/render/{cache/page-caches/lru.js → ssrCache/cacheMod.js} +21 -31
- package/dist/cjs/libs/render/ssrCache/index.js +81 -0
- package/dist/cjs/libs/render/ssrCache/manager.js +104 -0
- package/dist/cjs/libs/render/static.js +5 -5
- package/dist/cjs/server/index.js +2 -0
- package/dist/cjs/server/modernServer.js +2 -9
- package/dist/esm/libs/hook-api/afterRenderForStream.js +33 -0
- package/dist/esm/libs/hook-api/index.js +10 -0
- package/dist/esm/libs/render/index.js +4 -14
- package/dist/esm/libs/render/ssr.js +19 -5
- package/dist/esm/libs/render/ssrCache/cacheMod.js +33 -0
- package/dist/esm/libs/render/ssrCache/index.js +146 -0
- package/dist/esm/libs/render/ssrCache/manager.js +159 -0
- package/dist/esm/libs/render/static.js +5 -5
- package/dist/esm/server/index.js +14 -4
- package/dist/esm/server/modernServer.js +32 -42
- 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/render/index.js +4 -14
- package/dist/esm-node/libs/render/ssr.js +15 -4
- package/dist/esm-node/libs/render/ssrCache/cacheMod.js +22 -0
- package/dist/esm-node/libs/render/ssrCache/index.js +57 -0
- package/dist/esm-node/libs/render/ssrCache/manager.js +80 -0
- package/dist/esm-node/libs/render/static.js +5 -5
- package/dist/esm-node/server/index.js +2 -0
- package/dist/esm-node/server/modernServer.js +2 -9
- package/dist/types/libs/context/index.d.ts +2 -0
- 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/preload/flushServerHeader.d.ts +2 -0
- package/dist/types/libs/proxy.d.ts +2 -0
- package/dist/types/libs/render/ssr.d.ts +5 -5
- package/dist/types/libs/render/ssrCache/cacheMod.d.ts +8 -0
- package/dist/types/libs/render/ssrCache/index.d.ts +7 -0
- package/dist/types/libs/render/ssrCache/manager.d.ts +14 -0
- package/dist/types/renderHtml.d.ts +2 -0
- package/dist/types/server/index.d.ts +2 -0
- package/dist/types/server/modernServer.d.ts +2 -3
- package/dist/types/type.d.ts +2 -0
- package/package.json +8 -11
- package/dist/cjs/libs/render/cache/__tests__/cache.fun.test.js +0 -95
- package/dist/cjs/libs/render/cache/__tests__/cache.test.js +0 -223
- package/dist/cjs/libs/render/cache/__tests__/cacheable.js +0 -93
- package/dist/cjs/libs/render/cache/__tests__/error-configuration.js +0 -71
- package/dist/cjs/libs/render/cache/__tests__/matched-cache.js +0 -173
- package/dist/cjs/libs/render/cache/index.js +0 -92
- package/dist/cjs/libs/render/cache/spr.js +0 -247
- package/dist/cjs/libs/render/cache/type.js +0 -16
- package/dist/cjs/libs/render/cache/util.js +0 -121
- package/dist/cjs/libs/render/reader.js +0 -131
- package/dist/esm/libs/render/cache/__tests__/cache.fun.test.js +0 -124
- package/dist/esm/libs/render/cache/__tests__/cache.test.js +0 -612
- package/dist/esm/libs/render/cache/__tests__/cacheable.js +0 -69
- package/dist/esm/libs/render/cache/__tests__/error-configuration.js +0 -47
- package/dist/esm/libs/render/cache/__tests__/matched-cache.js +0 -149
- package/dist/esm/libs/render/cache/index.js +0 -204
- package/dist/esm/libs/render/cache/page-caches/index.js +0 -34
- package/dist/esm/libs/render/cache/page-caches/lru.js +0 -57
- package/dist/esm/libs/render/cache/spr.js +0 -345
- package/dist/esm/libs/render/cache/type.js +0 -0
- package/dist/esm/libs/render/cache/util.js +0 -105
- package/dist/esm/libs/render/reader.js +0 -196
- package/dist/esm-node/libs/render/cache/__tests__/cache.fun.test.js +0 -72
- package/dist/esm-node/libs/render/cache/__tests__/cache.test.js +0 -222
- package/dist/esm-node/libs/render/cache/__tests__/cacheable.js +0 -69
- package/dist/esm-node/libs/render/cache/__tests__/error-configuration.js +0 -47
- package/dist/esm-node/libs/render/cache/__tests__/matched-cache.js +0 -149
- package/dist/esm-node/libs/render/cache/index.js +0 -72
- package/dist/esm-node/libs/render/cache/page-caches/index.js +0 -12
- package/dist/esm-node/libs/render/cache/page-caches/lru.js +0 -32
- package/dist/esm-node/libs/render/cache/spr.js +0 -212
- package/dist/esm-node/libs/render/cache/type.js +0 -0
- package/dist/esm-node/libs/render/cache/util.js +0 -80
- package/dist/esm-node/libs/render/reader.js +0 -93
- package/dist/types/libs/render/cache/__tests__/cache.fun.test.d.ts +0 -1
- package/dist/types/libs/render/cache/__tests__/cache.test.d.ts +0 -1
- package/dist/types/libs/render/cache/__tests__/cacheable.d.ts +0 -62
- package/dist/types/libs/render/cache/__tests__/error-configuration.d.ts +0 -28
- package/dist/types/libs/render/cache/__tests__/matched-cache.d.ts +0 -124
- package/dist/types/libs/render/cache/index.d.ts +0 -6
- package/dist/types/libs/render/cache/page-caches/index.d.ts +0 -2
- package/dist/types/libs/render/cache/page-caches/lru.d.ts +0 -15
- package/dist/types/libs/render/cache/spr.d.ts +0 -22
- package/dist/types/libs/render/cache/type.d.ts +0 -48
- package/dist/types/libs/render/cache/util.d.ts +0 -18
- package/dist/types/libs/render/reader.d.ts +0 -20
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
|
|
2
|
+
import { _ as _create_class } from "@swc/helpers/_/_create_class";
|
|
3
|
+
import { _ as _define_property } from "@swc/helpers/_/_define_property";
|
|
4
|
+
import path from "path";
|
|
5
|
+
import { SERVER_DIR, requireExistModule } from "@modern-js/utils";
|
|
6
|
+
var CACHE_FILENAME = "cache";
|
|
7
|
+
var ServerCacheMod = /* @__PURE__ */ function() {
|
|
8
|
+
"use strict";
|
|
9
|
+
function ServerCacheMod2() {
|
|
10
|
+
_class_call_check(this, ServerCacheMod2);
|
|
11
|
+
_define_property(this, "customContainer", void 0);
|
|
12
|
+
_define_property(this, "cacheOption", void 0);
|
|
13
|
+
}
|
|
14
|
+
_create_class(ServerCacheMod2, [
|
|
15
|
+
{
|
|
16
|
+
key: "loadServerCacheMod",
|
|
17
|
+
value: function loadServerCacheMod() {
|
|
18
|
+
var pwd = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : process.cwd();
|
|
19
|
+
var serverCacheFilepath = path.resolve(pwd, SERVER_DIR, CACHE_FILENAME);
|
|
20
|
+
var mod = requireExistModule(serverCacheFilepath, {
|
|
21
|
+
interop: false
|
|
22
|
+
});
|
|
23
|
+
this.customContainer = mod === null || mod === void 0 ? void 0 : mod.customContainer;
|
|
24
|
+
this.cacheOption = mod === null || mod === void 0 ? void 0 : mod.cacheOption;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
]);
|
|
28
|
+
return ServerCacheMod2;
|
|
29
|
+
}();
|
|
30
|
+
var cacheMod = new ServerCacheMod();
|
|
31
|
+
export {
|
|
32
|
+
cacheMod
|
|
33
|
+
};
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
|
|
2
|
+
import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
|
|
3
|
+
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
|
|
4
|
+
import { Transform } from "stream";
|
|
5
|
+
import { createMemoryStorage } from "@modern-js/runtime-utils/storer";
|
|
6
|
+
import { cacheMod } from "./cacheMod";
|
|
7
|
+
import { CacheManager } from "./manager";
|
|
8
|
+
var cacheStorage = createMemoryStorage("__ssr__cache");
|
|
9
|
+
function ssrCache(req, render, ssrContext) {
|
|
10
|
+
return _ssrCache.apply(this, arguments);
|
|
11
|
+
}
|
|
12
|
+
function _ssrCache() {
|
|
13
|
+
_ssrCache = _async_to_generator(function(req, render, ssrContext) {
|
|
14
|
+
var customContainer, cacheOption, cacheControl, cacheManager, renderResult, stream;
|
|
15
|
+
return _ts_generator(this, function(_state) {
|
|
16
|
+
switch (_state.label) {
|
|
17
|
+
case 0:
|
|
18
|
+
customContainer = cacheMod.customContainer, cacheOption = cacheMod.cacheOption;
|
|
19
|
+
return [
|
|
20
|
+
4,
|
|
21
|
+
matchCacheControl(req, cacheOption)
|
|
22
|
+
];
|
|
23
|
+
case 1:
|
|
24
|
+
cacheControl = _state.sent();
|
|
25
|
+
cacheManager = new CacheManager(customContainer ? customContainer : cacheStorage);
|
|
26
|
+
if (!cacheControl)
|
|
27
|
+
return [
|
|
28
|
+
3,
|
|
29
|
+
2
|
|
30
|
+
];
|
|
31
|
+
return [
|
|
32
|
+
2,
|
|
33
|
+
cacheManager.getCacheResult(req, cacheControl, render, ssrContext)
|
|
34
|
+
];
|
|
35
|
+
case 2:
|
|
36
|
+
return [
|
|
37
|
+
4,
|
|
38
|
+
render(ssrContext)
|
|
39
|
+
];
|
|
40
|
+
case 3:
|
|
41
|
+
renderResult = _state.sent();
|
|
42
|
+
if (typeof renderResult === "string") {
|
|
43
|
+
return [
|
|
44
|
+
2,
|
|
45
|
+
renderResult
|
|
46
|
+
];
|
|
47
|
+
} else {
|
|
48
|
+
stream = new Transform({
|
|
49
|
+
write: function write(chunk, _, callback) {
|
|
50
|
+
this.push(chunk);
|
|
51
|
+
callback();
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
return [
|
|
55
|
+
2,
|
|
56
|
+
renderResult(stream)
|
|
57
|
+
];
|
|
58
|
+
}
|
|
59
|
+
_state.label = 4;
|
|
60
|
+
case 4:
|
|
61
|
+
return [
|
|
62
|
+
2
|
|
63
|
+
];
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
return _ssrCache.apply(this, arguments);
|
|
68
|
+
}
|
|
69
|
+
function matchCacheControl(req, cacheOption) {
|
|
70
|
+
return _matchCacheControl.apply(this, arguments);
|
|
71
|
+
}
|
|
72
|
+
function _matchCacheControl() {
|
|
73
|
+
_matchCacheControl = _async_to_generator(function(req, cacheOption) {
|
|
74
|
+
var url, options, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, _step_value, key, option;
|
|
75
|
+
function isCacheOptionProvider(option2) {
|
|
76
|
+
return typeof option2 === "function";
|
|
77
|
+
}
|
|
78
|
+
function isCacheControl(option2) {
|
|
79
|
+
return typeof option2 === "object" && option2 !== null && "maxAge" in option2;
|
|
80
|
+
}
|
|
81
|
+
return _ts_generator(this, function(_state) {
|
|
82
|
+
if (!cacheOption) {
|
|
83
|
+
return [
|
|
84
|
+
2,
|
|
85
|
+
void 0
|
|
86
|
+
];
|
|
87
|
+
} else if (isCacheControl(cacheOption)) {
|
|
88
|
+
return [
|
|
89
|
+
2,
|
|
90
|
+
cacheOption
|
|
91
|
+
];
|
|
92
|
+
} else if (isCacheOptionProvider(cacheOption)) {
|
|
93
|
+
return [
|
|
94
|
+
2,
|
|
95
|
+
cacheOption(req)
|
|
96
|
+
];
|
|
97
|
+
} else {
|
|
98
|
+
url = req.url;
|
|
99
|
+
options = Object.entries(cacheOption);
|
|
100
|
+
_iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
|
|
101
|
+
try {
|
|
102
|
+
for (_iterator = options[Symbol.iterator](); !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
|
|
103
|
+
_step_value = _sliced_to_array(_step.value, 2), key = _step_value[0], option = _step_value[1];
|
|
104
|
+
if (key === "*" || new RegExp(key).test(url)) {
|
|
105
|
+
if (typeof option === "function") {
|
|
106
|
+
return [
|
|
107
|
+
2,
|
|
108
|
+
option(req)
|
|
109
|
+
];
|
|
110
|
+
} else {
|
|
111
|
+
return [
|
|
112
|
+
2,
|
|
113
|
+
option
|
|
114
|
+
];
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
} catch (err) {
|
|
119
|
+
_didIteratorError = true;
|
|
120
|
+
_iteratorError = err;
|
|
121
|
+
} finally {
|
|
122
|
+
try {
|
|
123
|
+
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
124
|
+
_iterator.return();
|
|
125
|
+
}
|
|
126
|
+
} finally {
|
|
127
|
+
if (_didIteratorError) {
|
|
128
|
+
throw _iteratorError;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
return [
|
|
133
|
+
2,
|
|
134
|
+
void 0
|
|
135
|
+
];
|
|
136
|
+
}
|
|
137
|
+
return [
|
|
138
|
+
2
|
|
139
|
+
];
|
|
140
|
+
});
|
|
141
|
+
});
|
|
142
|
+
return _matchCacheControl.apply(this, arguments);
|
|
143
|
+
}
|
|
144
|
+
export {
|
|
145
|
+
ssrCache
|
|
146
|
+
};
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
|
|
2
|
+
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
|
|
3
|
+
import { _ as _create_class } from "@swc/helpers/_/_create_class";
|
|
4
|
+
import { _ as _define_property } from "@swc/helpers/_/_define_property";
|
|
5
|
+
import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
|
|
6
|
+
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
|
|
7
|
+
import { Transform } from "stream";
|
|
8
|
+
var CacheManager = /* @__PURE__ */ function() {
|
|
9
|
+
"use strict";
|
|
10
|
+
function CacheManager2(container) {
|
|
11
|
+
_class_call_check(this, CacheManager2);
|
|
12
|
+
_define_property(this, "container", void 0);
|
|
13
|
+
this.container = container;
|
|
14
|
+
}
|
|
15
|
+
_create_class(CacheManager2, [
|
|
16
|
+
{
|
|
17
|
+
key: "getCacheResult",
|
|
18
|
+
value: function getCacheResult(req, cacheControl, render, ssrContext) {
|
|
19
|
+
var _this = this;
|
|
20
|
+
return _async_to_generator(function() {
|
|
21
|
+
var key, value, maxAge, staleWhileRevalidate, ttl, cache, interval;
|
|
22
|
+
return _ts_generator(this, function(_state) {
|
|
23
|
+
switch (_state.label) {
|
|
24
|
+
case 0:
|
|
25
|
+
key = _this.computedKey(req, cacheControl);
|
|
26
|
+
return [
|
|
27
|
+
4,
|
|
28
|
+
_this.container.get(key)
|
|
29
|
+
];
|
|
30
|
+
case 1:
|
|
31
|
+
value = _state.sent();
|
|
32
|
+
maxAge = cacheControl.maxAge, staleWhileRevalidate = cacheControl.staleWhileRevalidate;
|
|
33
|
+
ttl = maxAge + staleWhileRevalidate;
|
|
34
|
+
if (value) {
|
|
35
|
+
cache = JSON.parse(value);
|
|
36
|
+
interval = Date.now() - cache.cursor;
|
|
37
|
+
if (interval <= maxAge) {
|
|
38
|
+
return [
|
|
39
|
+
2,
|
|
40
|
+
cache.val
|
|
41
|
+
];
|
|
42
|
+
} else if (interval <= staleWhileRevalidate + maxAge) {
|
|
43
|
+
_this.processCache(key, render, ssrContext, ttl);
|
|
44
|
+
return [
|
|
45
|
+
2,
|
|
46
|
+
cache.val
|
|
47
|
+
];
|
|
48
|
+
} else {
|
|
49
|
+
return [
|
|
50
|
+
2,
|
|
51
|
+
_this.processCache(key, render, ssrContext, ttl)
|
|
52
|
+
];
|
|
53
|
+
}
|
|
54
|
+
} else {
|
|
55
|
+
return [
|
|
56
|
+
2,
|
|
57
|
+
_this.processCache(key, render, ssrContext, ttl)
|
|
58
|
+
];
|
|
59
|
+
}
|
|
60
|
+
return [
|
|
61
|
+
2
|
|
62
|
+
];
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
})();
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
{
|
|
69
|
+
key: "processCache",
|
|
70
|
+
value: function processCache(key, render, ssrContext, ttl) {
|
|
71
|
+
var _this = this;
|
|
72
|
+
return _async_to_generator(function() {
|
|
73
|
+
var renderResult, current, cache, html, stream;
|
|
74
|
+
return _ts_generator(this, function(_state) {
|
|
75
|
+
switch (_state.label) {
|
|
76
|
+
case 0:
|
|
77
|
+
return [
|
|
78
|
+
4,
|
|
79
|
+
render(ssrContext)
|
|
80
|
+
];
|
|
81
|
+
case 1:
|
|
82
|
+
renderResult = _state.sent();
|
|
83
|
+
if (!(typeof renderResult === "string"))
|
|
84
|
+
return [
|
|
85
|
+
3,
|
|
86
|
+
3
|
|
87
|
+
];
|
|
88
|
+
current = Date.now();
|
|
89
|
+
cache = {
|
|
90
|
+
val: renderResult,
|
|
91
|
+
cursor: current
|
|
92
|
+
};
|
|
93
|
+
return [
|
|
94
|
+
4,
|
|
95
|
+
_this.container.set(key, JSON.stringify(cache), {
|
|
96
|
+
ttl
|
|
97
|
+
})
|
|
98
|
+
];
|
|
99
|
+
case 2:
|
|
100
|
+
_state.sent();
|
|
101
|
+
return [
|
|
102
|
+
2,
|
|
103
|
+
renderResult
|
|
104
|
+
];
|
|
105
|
+
case 3:
|
|
106
|
+
stream = new Transform({
|
|
107
|
+
write: function write(chunk, _, callback) {
|
|
108
|
+
html += chunk.toString();
|
|
109
|
+
this.push(chunk);
|
|
110
|
+
callback();
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
stream.on("close", function() {
|
|
114
|
+
var current2 = Date.now();
|
|
115
|
+
var cache2 = {
|
|
116
|
+
val: html,
|
|
117
|
+
cursor: current2
|
|
118
|
+
};
|
|
119
|
+
_this.container.set(key, JSON.stringify(cache2), {
|
|
120
|
+
ttl
|
|
121
|
+
});
|
|
122
|
+
});
|
|
123
|
+
return [
|
|
124
|
+
2,
|
|
125
|
+
renderResult(stream)
|
|
126
|
+
];
|
|
127
|
+
case 4:
|
|
128
|
+
return [
|
|
129
|
+
2
|
|
130
|
+
];
|
|
131
|
+
}
|
|
132
|
+
});
|
|
133
|
+
})();
|
|
134
|
+
}
|
|
135
|
+
},
|
|
136
|
+
{
|
|
137
|
+
key: "computedKey",
|
|
138
|
+
value: function computedKey(req, cacheControl) {
|
|
139
|
+
var url = req.url;
|
|
140
|
+
var _url_split = _sliced_to_array(url.split("?"), 1), pathname = _url_split[0];
|
|
141
|
+
var customKey = cacheControl.customKey;
|
|
142
|
+
var defaultKey = pathname.replace(/.+\/+$/, "");
|
|
143
|
+
if (customKey) {
|
|
144
|
+
if (typeof customKey === "string") {
|
|
145
|
+
return customKey;
|
|
146
|
+
} else {
|
|
147
|
+
return customKey(defaultKey);
|
|
148
|
+
}
|
|
149
|
+
} else {
|
|
150
|
+
return defaultKey;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
]);
|
|
155
|
+
return CacheManager2;
|
|
156
|
+
}();
|
|
157
|
+
export {
|
|
158
|
+
CacheManager
|
|
159
|
+
};
|
|
@@ -2,7 +2,7 @@ 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 { mime } from "@modern-js/utils";
|
|
5
|
-
import {
|
|
5
|
+
import { fileReader } from "@modern-js/runtime-utils/fileReader";
|
|
6
6
|
function handleDirectory(ctx, entryPath, urlPath) {
|
|
7
7
|
return _handleDirectory.apply(this, arguments);
|
|
8
8
|
}
|
|
@@ -16,7 +16,7 @@ function _handleDirectory() {
|
|
|
16
16
|
filepath = path.join(entryPath, trimLeft(pathname, urlPath));
|
|
17
17
|
return [
|
|
18
18
|
4,
|
|
19
|
-
readFile(filepath)
|
|
19
|
+
fileReader.readFile(filepath)
|
|
20
20
|
];
|
|
21
21
|
case 1:
|
|
22
22
|
content = _state.sent();
|
|
@@ -33,7 +33,7 @@ function _handleDirectory() {
|
|
|
33
33
|
];
|
|
34
34
|
return [
|
|
35
35
|
4,
|
|
36
|
-
readFile("".concat(filepath, "index.html"))
|
|
36
|
+
fileReader.readFile("".concat(filepath, "index.html"))
|
|
37
37
|
];
|
|
38
38
|
case 2:
|
|
39
39
|
content = _state.sent();
|
|
@@ -49,7 +49,7 @@ function _handleDirectory() {
|
|
|
49
49
|
];
|
|
50
50
|
return [
|
|
51
51
|
4,
|
|
52
|
-
readFile("".concat(filepath, ".html"))
|
|
52
|
+
fileReader.readFile("".concat(filepath, ".html"))
|
|
53
53
|
];
|
|
54
54
|
case 4:
|
|
55
55
|
content = _state.sent();
|
|
@@ -60,7 +60,7 @@ function _handleDirectory() {
|
|
|
60
60
|
];
|
|
61
61
|
return [
|
|
62
62
|
4,
|
|
63
|
-
readFile("".concat(filepath, "/index.html"))
|
|
63
|
+
fileReader.readFile("".concat(filepath, "/index.html"))
|
|
64
64
|
];
|
|
65
65
|
case 5:
|
|
66
66
|
content = _state.sent();
|
package/dist/esm/server/index.js
CHANGED
|
@@ -217,11 +217,21 @@ var Server = /* @__PURE__ */ function() {
|
|
|
217
217
|
value: function close() {
|
|
218
218
|
var _this = this;
|
|
219
219
|
return _async_to_generator(function() {
|
|
220
|
+
var _this_server_close, _this_server;
|
|
220
221
|
return _ts_generator(this, function(_state) {
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
222
|
+
switch (_state.label) {
|
|
223
|
+
case 0:
|
|
224
|
+
return [
|
|
225
|
+
4,
|
|
226
|
+
(_this_server_close = (_this_server = _this.server).close) === null || _this_server_close === void 0 ? void 0 : _this_server_close.call(_this_server)
|
|
227
|
+
];
|
|
228
|
+
case 1:
|
|
229
|
+
_state.sent();
|
|
230
|
+
_this.app.close();
|
|
231
|
+
return [
|
|
232
|
+
2
|
|
233
|
+
];
|
|
234
|
+
}
|
|
225
235
|
});
|
|
226
236
|
})();
|
|
227
237
|
}
|
|
@@ -13,11 +13,11 @@ import { RouteMatchManager } from "../libs/route";
|
|
|
13
13
|
import { createRenderHandler } from "../libs/render";
|
|
14
14
|
import { createStaticFileHandler, faviconFallbackHandler } from "../libs/serveFile";
|
|
15
15
|
import { createErrorDocument, createMiddlewareCollecter, getStaticReg, mergeExtension, noop, debug, isRedirect } from "../utils";
|
|
16
|
-
import * as reader from "../libs/render/reader";
|
|
17
16
|
import { createProxyHandler } from "../libs/proxy";
|
|
18
17
|
import { createContext } from "../libs/context";
|
|
19
18
|
import { AGGRED_DIR, ERROR_DIGEST, ERROR_PAGE_TEXT, RUN_MODE, ServerReportTimings } from "../constants";
|
|
20
19
|
import { createAfterMatchContext, createAfterRenderContext, createMiddlewareContext } from "../libs/hook-api";
|
|
20
|
+
import { cacheMod } from "../libs/render/ssrCache/cacheMod";
|
|
21
21
|
var SERVER_DIR = "./server";
|
|
22
22
|
var ModernServer = /* @__PURE__ */ function() {
|
|
23
23
|
"use strict";
|
|
@@ -35,7 +35,6 @@ var ModernServer = /* @__PURE__ */ function() {
|
|
|
35
35
|
_define_property(this, "logger", void 0);
|
|
36
36
|
_define_property(this, "metrics", void 0);
|
|
37
37
|
_define_property(this, "runMode", void 0);
|
|
38
|
-
_define_property(this, "reader", reader);
|
|
39
38
|
_define_property(this, "proxyTarget", void 0);
|
|
40
39
|
_define_property(this, "routeRenderHandler", void 0);
|
|
41
40
|
_define_property(this, "staticGenerate", void 0);
|
|
@@ -73,7 +72,6 @@ var ModernServer = /* @__PURE__ */ function() {
|
|
|
73
72
|
case 0:
|
|
74
73
|
_this.runner = runner;
|
|
75
74
|
distDir = _this.distDir, conf = _this.conf;
|
|
76
|
-
_this.initReader();
|
|
77
75
|
debug("final server conf", _this.conf);
|
|
78
76
|
if ((_conf_bff = conf.bff) === null || _conf_bff === void 0 ? void 0 : _conf_bff.proxy) {
|
|
79
77
|
_createProxyHandler = createProxyHandler(conf.bff.proxy), handlers = _createProxyHandler.handlers, handleUpgrade = _createProxyHandler.handleUpgrade;
|
|
@@ -82,12 +80,10 @@ var ModernServer = /* @__PURE__ */ function() {
|
|
|
82
80
|
_this.addHandler(handler);
|
|
83
81
|
});
|
|
84
82
|
}
|
|
85
|
-
app === null || app === void 0 ? void 0 : app.on("close", function() {
|
|
86
|
-
_this.reader.close();
|
|
87
|
-
});
|
|
88
83
|
usageRoutes = _this.filterRoutes(_this.getRoutes());
|
|
89
84
|
_this.router.reset(usageRoutes);
|
|
90
85
|
_this.warmupSSRBundle();
|
|
86
|
+
cacheMod.loadServerCacheMod(_this.pwd);
|
|
91
87
|
return [
|
|
92
88
|
4,
|
|
93
89
|
_this.prepareFrameHandler()
|
|
@@ -218,47 +214,41 @@ var ModernServer = /* @__PURE__ */ function() {
|
|
|
218
214
|
}
|
|
219
215
|
},
|
|
220
216
|
{
|
|
221
|
-
key: "
|
|
217
|
+
key: "onServerChange",
|
|
222
218
|
value: (
|
|
223
219
|
/* —————————————————————— function will be overwrite —————————————————————— */
|
|
224
|
-
function
|
|
225
|
-
|
|
220
|
+
function onServerChange(param) {
|
|
221
|
+
var filepath = param.filepath;
|
|
222
|
+
var _this = this;
|
|
223
|
+
return _async_to_generator(function() {
|
|
224
|
+
var pwd, api, server, apiPath, serverPath, onlyApi, onlyWeb;
|
|
225
|
+
return _ts_generator(this, function(_state) {
|
|
226
|
+
switch (_state.label) {
|
|
227
|
+
case 0:
|
|
228
|
+
pwd = _this.pwd;
|
|
229
|
+
api = AGGRED_DIR.api, server = AGGRED_DIR.server;
|
|
230
|
+
apiPath = path.normalize(path.join(pwd, api));
|
|
231
|
+
serverPath = path.normalize(path.join(pwd, server));
|
|
232
|
+
onlyApi = filepath.startsWith(apiPath);
|
|
233
|
+
onlyWeb = filepath.startsWith(serverPath);
|
|
234
|
+
return [
|
|
235
|
+
4,
|
|
236
|
+
_this.prepareFrameHandler({
|
|
237
|
+
onlyWeb,
|
|
238
|
+
onlyApi
|
|
239
|
+
})
|
|
240
|
+
];
|
|
241
|
+
case 1:
|
|
242
|
+
_state.sent();
|
|
243
|
+
return [
|
|
244
|
+
2
|
|
245
|
+
];
|
|
246
|
+
}
|
|
247
|
+
});
|
|
248
|
+
})();
|
|
226
249
|
}
|
|
227
250
|
)
|
|
228
251
|
},
|
|
229
|
-
{
|
|
230
|
-
key: "onServerChange",
|
|
231
|
-
value: function onServerChange(param) {
|
|
232
|
-
var filepath = param.filepath;
|
|
233
|
-
var _this = this;
|
|
234
|
-
return _async_to_generator(function() {
|
|
235
|
-
var pwd, api, server, apiPath, serverPath, onlyApi, onlyWeb;
|
|
236
|
-
return _ts_generator(this, function(_state) {
|
|
237
|
-
switch (_state.label) {
|
|
238
|
-
case 0:
|
|
239
|
-
pwd = _this.pwd;
|
|
240
|
-
api = AGGRED_DIR.api, server = AGGRED_DIR.server;
|
|
241
|
-
apiPath = path.normalize(path.join(pwd, api));
|
|
242
|
-
serverPath = path.normalize(path.join(pwd, server));
|
|
243
|
-
onlyApi = filepath.startsWith(apiPath);
|
|
244
|
-
onlyWeb = filepath.startsWith(serverPath);
|
|
245
|
-
return [
|
|
246
|
-
4,
|
|
247
|
-
_this.prepareFrameHandler({
|
|
248
|
-
onlyWeb,
|
|
249
|
-
onlyApi
|
|
250
|
-
})
|
|
251
|
-
];
|
|
252
|
-
case 1:
|
|
253
|
-
_state.sent();
|
|
254
|
-
return [
|
|
255
|
-
2
|
|
256
|
-
];
|
|
257
|
-
}
|
|
258
|
-
});
|
|
259
|
-
})();
|
|
260
|
-
}
|
|
261
|
-
},
|
|
262
252
|
{
|
|
263
253
|
key: "getRoutes",
|
|
264
254
|
value: (
|
|
@@ -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
|
};
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import path from "path";
|
|
2
2
|
import { cutNameByHyphen, mime } from "@modern-js/utils";
|
|
3
|
+
import { fileReader } from "@modern-js/runtime-utils/fileReader";
|
|
3
4
|
import { ERROR_DIGEST } from "../../constants";
|
|
4
5
|
import { shouldFlushServerHeader } from "../preload/shouldFlushServerHeader";
|
|
5
6
|
import { handleDirectory } from "./static";
|
|
6
|
-
import { readFile } from "./reader";
|
|
7
7
|
import * as ssr from "./ssr";
|
|
8
8
|
import { injectServerData } from "./utils";
|
|
9
9
|
const calcFallback = (metaName) => `x-${cutNameByHyphen(metaName)}-ssr-fallback`;
|
|
@@ -18,7 +18,7 @@ const createRenderHandler = ({ distDir, staticGenerate, conf, forceCSR, nonce, s
|
|
|
18
18
|
return result;
|
|
19
19
|
}
|
|
20
20
|
const templatePath = entry;
|
|
21
|
-
const content = await readFile(templatePath);
|
|
21
|
+
const content = await fileReader.readFile(templatePath);
|
|
22
22
|
if (!content) {
|
|
23
23
|
return null;
|
|
24
24
|
}
|
|
@@ -41,22 +41,12 @@ 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
|
|
50
48
|
};
|
|
51
|
-
const result = await (ssrRender ? ssrRender(ctx, ssrRenderOptions, runner) : ssr.render(ctx,
|
|
52
|
-
distDir,
|
|
53
|
-
entryName: route.entryName,
|
|
54
|
-
urlPath: route.urlPath,
|
|
55
|
-
bundle: route.bundle,
|
|
56
|
-
template: content.toString(),
|
|
57
|
-
staticGenerate,
|
|
58
|
-
nonce
|
|
59
|
-
}, runner));
|
|
49
|
+
const result = await (ssrRender ? ssrRender(ctx, ssrRenderOptions, runner) : ssr.render(ctx, ssrRenderOptions, runner));
|
|
60
50
|
return result;
|
|
61
51
|
} catch (err) {
|
|
62
52
|
ctx.error(ERROR_DIGEST.ERENDER, err.stack || err.message);
|
|
@@ -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
|
|
3
|
+
import { createAfterStreamingRenderContext } from "../hook-api";
|
|
4
|
+
import { afterRenderInjectableStream } from "../hook-api/afterRenderForStream";
|
|
4
5
|
import { createLogger, createMetrics } from "./measure";
|
|
5
6
|
import { injectServerDataStream, injectServerData } from "./utils";
|
|
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) : "";
|
|
@@ -50,7 +53,7 @@ const render = async (ctx, renderOptions, runner) => {
|
|
|
50
53
|
runner.extendSSRContext(context);
|
|
51
54
|
const bundleJSContent = await Promise.resolve(require(bundleJS));
|
|
52
55
|
const serverRender = bundleJSContent[SERVER_RENDER_FUNCTION_NAME];
|
|
53
|
-
const content = await
|
|
56
|
+
const content = await ssrCache(ctx.req, serverRender, context);
|
|
54
57
|
const { url, status = 302 } = context.redirection;
|
|
55
58
|
if (url) {
|
|
56
59
|
return {
|
|
@@ -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
|
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { _ as _define_property } from "@swc/helpers/_/_define_property";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import { SERVER_DIR, requireExistModule } from "@modern-js/utils";
|
|
4
|
+
const CACHE_FILENAME = "cache";
|
|
5
|
+
class ServerCacheMod {
|
|
6
|
+
loadServerCacheMod(pwd = process.cwd()) {
|
|
7
|
+
const serverCacheFilepath = path.resolve(pwd, SERVER_DIR, CACHE_FILENAME);
|
|
8
|
+
const mod = requireExistModule(serverCacheFilepath, {
|
|
9
|
+
interop: false
|
|
10
|
+
});
|
|
11
|
+
this.customContainer = mod === null || mod === void 0 ? void 0 : mod.customContainer;
|
|
12
|
+
this.cacheOption = mod === null || mod === void 0 ? void 0 : mod.cacheOption;
|
|
13
|
+
}
|
|
14
|
+
constructor() {
|
|
15
|
+
_define_property(this, "customContainer", void 0);
|
|
16
|
+
_define_property(this, "cacheOption", void 0);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
const cacheMod = new ServerCacheMod();
|
|
20
|
+
export {
|
|
21
|
+
cacheMod
|
|
22
|
+
};
|