@modern-js/prod-server 2.4.1-beta.0 → 2.5.1-alpha.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.
Files changed (125) hide show
  1. package/CHANGELOG.md +18 -3
  2. package/dist/cjs/constants.js +62 -0
  3. package/dist/cjs/index.js +44 -0
  4. package/dist/cjs/libs/context/context.js +189 -0
  5. package/dist/cjs/libs/context/index.js +30 -0
  6. package/dist/cjs/libs/hook-api/index.js +151 -0
  7. package/dist/cjs/libs/hook-api/route.js +43 -0
  8. package/dist/cjs/libs/hook-api/template.js +97 -0
  9. package/dist/cjs/libs/loadConfig.js +76 -0
  10. package/dist/cjs/libs/logger.js +122 -0
  11. package/dist/cjs/libs/metrics.js +34 -0
  12. package/dist/cjs/libs/proxy.js +81 -0
  13. package/dist/cjs/libs/render/cache/__tests__/cache.fun.test.js +93 -0
  14. package/dist/cjs/libs/render/cache/__tests__/cache.test.js +210 -0
  15. package/dist/cjs/libs/render/cache/__tests__/cacheable.js +70 -0
  16. package/dist/cjs/libs/render/cache/__tests__/error-configuration.js +60 -0
  17. package/dist/cjs/libs/render/cache/__tests__/matched-cache.js +114 -0
  18. package/dist/cjs/libs/render/cache/index.js +97 -0
  19. package/dist/cjs/libs/render/cache/page-caches/index.js +33 -0
  20. package/dist/cjs/libs/render/cache/page-caches/lru.js +58 -0
  21. package/dist/cjs/libs/render/cache/spr.js +242 -0
  22. package/dist/cjs/libs/render/cache/type.js +15 -0
  23. package/dist/cjs/libs/render/cache/util.js +116 -0
  24. package/dist/cjs/libs/render/index.js +93 -0
  25. package/dist/cjs/libs/render/measure.js +75 -0
  26. package/dist/cjs/libs/render/reader.js +118 -0
  27. package/dist/cjs/libs/render/ssr.js +103 -0
  28. package/dist/cjs/libs/render/static.js +67 -0
  29. package/dist/cjs/libs/render/type.js +32 -0
  30. package/dist/cjs/libs/route/index.js +78 -0
  31. package/dist/cjs/libs/route/matcher.js +106 -0
  32. package/dist/cjs/libs/route/route.js +39 -0
  33. package/dist/cjs/libs/serve-file.js +77 -0
  34. package/dist/cjs/server/index.js +169 -0
  35. package/dist/cjs/server/modern-server-split.js +66 -0
  36. package/dist/cjs/server/modern-server.js +491 -0
  37. package/dist/cjs/type.js +15 -0
  38. package/dist/cjs/utils.js +152 -0
  39. package/dist/cjs/worker-server.js +93 -0
  40. package/dist/esm/constants.js +29 -0
  41. package/dist/esm/index.js +13 -0
  42. package/dist/esm/libs/context/context.js +274 -0
  43. package/dist/esm/libs/context/index.js +5 -0
  44. package/dist/esm/libs/hook-api/index.js +281 -0
  45. package/dist/esm/libs/hook-api/route.js +68 -0
  46. package/dist/esm/libs/hook-api/template.js +127 -0
  47. package/dist/esm/libs/loadConfig.js +82 -0
  48. package/dist/esm/libs/logger.js +205 -0
  49. package/dist/esm/libs/metrics.js +6 -0
  50. package/dist/esm/libs/proxy.js +244 -0
  51. package/dist/esm/libs/render/cache/__tests__/cache.fun.test.js +291 -0
  52. package/dist/esm/libs/render/cache/__tests__/cache.test.js +781 -0
  53. package/dist/esm/libs/render/cache/__tests__/cacheable.js +67 -0
  54. package/dist/esm/libs/render/cache/__tests__/error-configuration.js +45 -0
  55. package/dist/esm/libs/render/cache/__tests__/matched-cache.js +147 -0
  56. package/dist/esm/libs/render/cache/index.js +346 -0
  57. package/dist/esm/libs/render/cache/page-caches/index.js +154 -0
  58. package/dist/esm/libs/render/cache/page-caches/lru.js +84 -0
  59. package/dist/esm/libs/render/cache/spr.js +492 -0
  60. package/dist/esm/libs/render/cache/type.js +1 -0
  61. package/dist/esm/libs/render/cache/util.js +280 -0
  62. package/dist/esm/libs/render/index.js +234 -0
  63. package/dist/esm/libs/render/measure.js +146 -0
  64. package/dist/esm/libs/render/reader.js +339 -0
  65. package/dist/esm/libs/render/ssr.js +223 -0
  66. package/dist/esm/libs/render/static.js +216 -0
  67. package/dist/esm/libs/render/type.js +7 -0
  68. package/dist/esm/libs/route/index.js +130 -0
  69. package/dist/esm/libs/route/matcher.js +143 -0
  70. package/dist/esm/libs/route/route.js +40 -0
  71. package/dist/esm/libs/serve-file.js +184 -0
  72. package/dist/esm/server/index.js +505 -0
  73. package/dist/esm/server/modern-server-split.js +360 -0
  74. package/dist/esm/server/modern-server.js +1090 -0
  75. package/dist/esm/type.js +1 -0
  76. package/dist/esm/utils.js +147 -0
  77. package/dist/esm/worker-server.js +233 -0
  78. package/dist/esm-node/constants.js +35 -0
  79. package/dist/esm-node/index.js +18 -0
  80. package/dist/esm-node/libs/context/context.js +160 -0
  81. package/dist/esm-node/libs/context/index.js +6 -0
  82. package/dist/esm-node/libs/hook-api/index.js +119 -0
  83. package/dist/esm-node/libs/hook-api/route.js +20 -0
  84. package/dist/esm-node/libs/hook-api/template.js +73 -0
  85. package/dist/esm-node/libs/loadConfig.js +45 -0
  86. package/dist/esm-node/libs/logger.js +98 -0
  87. package/dist/esm-node/libs/metrics.js +11 -0
  88. package/dist/esm-node/libs/proxy.js +57 -0
  89. package/dist/esm-node/libs/render/cache/__tests__/cache.fun.test.js +83 -0
  90. package/dist/esm-node/libs/render/cache/__tests__/cache.test.js +210 -0
  91. package/dist/esm-node/libs/render/cache/__tests__/cacheable.js +47 -0
  92. package/dist/esm-node/libs/render/cache/__tests__/error-configuration.js +37 -0
  93. package/dist/esm-node/libs/render/cache/__tests__/matched-cache.js +91 -0
  94. package/dist/esm-node/libs/render/cache/index.js +76 -0
  95. package/dist/esm-node/libs/render/cache/page-caches/index.js +10 -0
  96. package/dist/esm-node/libs/render/cache/page-caches/lru.js +29 -0
  97. package/dist/esm-node/libs/render/cache/spr.js +220 -0
  98. package/dist/esm-node/libs/render/cache/type.js +0 -0
  99. package/dist/esm-node/libs/render/cache/util.js +80 -0
  100. package/dist/esm-node/libs/render/index.js +64 -0
  101. package/dist/esm-node/libs/render/measure.js +51 -0
  102. package/dist/esm-node/libs/render/reader.js +85 -0
  103. package/dist/esm-node/libs/render/ssr.js +80 -0
  104. package/dist/esm-node/libs/render/static.js +38 -0
  105. package/dist/esm-node/libs/render/type.js +9 -0
  106. package/dist/esm-node/libs/route/index.js +54 -0
  107. package/dist/esm-node/libs/route/matcher.js +87 -0
  108. package/dist/esm-node/libs/route/route.js +16 -0
  109. package/dist/esm-node/libs/serve-file.js +47 -0
  110. package/dist/esm-node/server/index.js +156 -0
  111. package/dist/esm-node/server/modern-server-split.js +43 -0
  112. package/dist/esm-node/server/modern-server.js +484 -0
  113. package/dist/esm-node/type.js +0 -0
  114. package/dist/esm-node/utils.js +120 -0
  115. package/dist/esm-node/worker-server.js +69 -0
  116. package/dist/js/modern/libs/render/index.js +4 -2
  117. package/dist/js/modern/server/modern-server.js +14 -8
  118. package/dist/js/node/libs/render/index.js +4 -2
  119. package/dist/js/node/server/modern-server.js +14 -8
  120. package/dist/js/treeshaking/libs/render/index.js +4 -3
  121. package/dist/js/treeshaking/server/modern-server.js +14 -8
  122. package/dist/types/libs/context/context.d.ts +1 -1
  123. package/dist/types/libs/render/index.d.ts +3 -1
  124. package/dist/types/utils.d.ts +1 -1
  125. package/package.json +17 -18
@@ -0,0 +1,484 @@
1
+ import { createServer } from "http";
2
+ import path from "path";
3
+ import { fs, isPromise, mime, ROUTE_SPEC_FILE } from "@modern-js/utils";
4
+ import {
5
+ RouteMatchManager
6
+ } from "../libs/route";
7
+ import { createRenderHandler } from "../libs/render";
8
+ import {
9
+ createStaticFileHandler,
10
+ faviconFallbackHandler
11
+ } from "../libs/serve-file";
12
+ import {
13
+ createErrorDocument,
14
+ createMiddlewareCollecter,
15
+ getStaticReg,
16
+ mergeExtension,
17
+ noop,
18
+ debug,
19
+ isRedirect
20
+ } from "../utils";
21
+ import * as reader from "../libs/render/reader";
22
+ import { createProxyHandler } from "../libs/proxy";
23
+ import { createContext } from "../libs/context";
24
+ import { templateInjectableStream } from "../libs/hook-api/template";
25
+ import {
26
+ AGGRED_DIR,
27
+ ERROR_DIGEST,
28
+ ERROR_PAGE_TEXT,
29
+ RUN_MODE
30
+ } from "../constants";
31
+ import {
32
+ createAfterMatchContext,
33
+ createAfterRenderContext,
34
+ createMiddlewareContext
35
+ } from "../libs/hook-api";
36
+ const API_DIR = "./api";
37
+ const SERVER_DIR = "./server";
38
+ class ModernServer {
39
+ constructor({
40
+ pwd,
41
+ config,
42
+ routes,
43
+ staticGenerate,
44
+ logger,
45
+ metrics,
46
+ runMode,
47
+ proxyTarget
48
+ }) {
49
+ this.handlers = [];
50
+ this.reader = reader;
51
+ this.beforeRouteHandler = null;
52
+ this.frameWebHandler = null;
53
+ this.frameAPIHandler = null;
54
+ this.proxyHandler = null;
55
+ require("ignore-styles");
56
+ this.pwd = pwd;
57
+ this.distDir = path.join(pwd, config.output.path || "dist");
58
+ this.workDir = this.distDir;
59
+ this.conf = config;
60
+ debug("server conf", this.conf);
61
+ this.logger = logger;
62
+ this.metrics = metrics;
63
+ this.router = new RouteMatchManager();
64
+ this.presetRoutes = routes;
65
+ this.proxyTarget = proxyTarget;
66
+ this.staticGenerate = staticGenerate || false;
67
+ this.runMode = runMode || RUN_MODE.FULL;
68
+ }
69
+ async onInit(runner, app) {
70
+ var _a, _b;
71
+ this.runner = runner;
72
+ const { distDir, staticGenerate, conf } = this;
73
+ debug("final server conf", this.conf);
74
+ this.proxyHandler = createProxyHandler((_a = conf.bff) == null ? void 0 : _a.proxy);
75
+ if (this.proxyHandler) {
76
+ this.proxyHandler.forEach((handler) => {
77
+ this.addHandler(handler);
78
+ });
79
+ }
80
+ this.reader.init();
81
+ app.on("close", () => {
82
+ this.reader.close();
83
+ });
84
+ const usageRoutes = this.filterRoutes(this.getRoutes());
85
+ this.router.reset(usageRoutes);
86
+ this.warmupSSRBundle();
87
+ await this.prepareFrameHandler();
88
+ await this.prepareBeforeRouteHandler(usageRoutes, distDir);
89
+ const staticPathRegExp = getStaticReg(
90
+ this.conf.output || {},
91
+ this.conf.html
92
+ );
93
+ this.staticFileHandler = createStaticFileHandler(
94
+ [
95
+ {
96
+ path: staticPathRegExp,
97
+ target: distDir
98
+ }
99
+ ],
100
+ this.conf.output
101
+ );
102
+ const ssrConfig = (_b = this.conf.server) == null ? void 0 : _b.ssr;
103
+ const forceCSR = typeof ssrConfig === "object" ? ssrConfig.forceCSR : false;
104
+ this.routeRenderHandler = createRenderHandler({
105
+ distDir,
106
+ staticGenerate,
107
+ forceCSR
108
+ });
109
+ await this.setupBeforeProdMiddleware();
110
+ this.addHandler(this.staticFileHandler);
111
+ this.addHandler(faviconFallbackHandler);
112
+ this.addBeforeRouteHandler();
113
+ this.addHandler(this.routeHandler.bind(this));
114
+ this.compose();
115
+ }
116
+ onRepack(_) {
117
+ }
118
+ addBeforeRouteHandler() {
119
+ this.addHandler(async (context, next) => {
120
+ if (this.beforeRouteHandler) {
121
+ await this.beforeRouteHandler(context);
122
+ if (this.isSend(context.res)) {
123
+ return;
124
+ }
125
+ }
126
+ return next();
127
+ });
128
+ }
129
+ onServerChange({ filepath }) {
130
+ const { pwd } = this;
131
+ const { api, server } = AGGRED_DIR;
132
+ const apiPath = path.normalize(path.join(pwd, api));
133
+ const serverPath = path.normalize(path.join(pwd, server));
134
+ const onlyApi = filepath.startsWith(apiPath);
135
+ const onlyWeb = filepath.startsWith(serverPath);
136
+ this.prepareFrameHandler({ onlyWeb, onlyApi });
137
+ }
138
+ getRequestHandler() {
139
+ return this.requestHandler.bind(this);
140
+ }
141
+ async render(req, res, url) {
142
+ req.logger = req.logger || this.logger;
143
+ req.metrics = req.metrics || this.metrics;
144
+ const context = createContext(req, res);
145
+ const matched = this.router.match(url || context.path);
146
+ if (!matched) {
147
+ return null;
148
+ }
149
+ const route = matched.generate(context.url);
150
+ const result = await this.handleWeb(context, route);
151
+ if (!result) {
152
+ return null;
153
+ }
154
+ return result.content.toString();
155
+ }
156
+ async createHTTPServer(handler) {
157
+ return createServer(handler);
158
+ }
159
+ getRoutes() {
160
+ if (this.presetRoutes) {
161
+ return this.presetRoutes;
162
+ }
163
+ const file = path.join(this.distDir, ROUTE_SPEC_FILE);
164
+ if (fs.existsSync(file)) {
165
+ const content = fs.readJSONSync(file);
166
+ return content.routes;
167
+ }
168
+ return [];
169
+ }
170
+ addHandler(handler) {
171
+ this.handlers.push(handler);
172
+ }
173
+ render404(context) {
174
+ context.error(ERROR_DIGEST.ENOTF, "404 Not Found");
175
+ this.renderErrorPage(context, 404);
176
+ }
177
+ async prepareBeforeRouteHandler(specs, distDir) {
178
+ const { runner } = this;
179
+ const handler = await runner.preparebeforeRouteHandler(
180
+ {
181
+ serverRoutes: specs,
182
+ distDir
183
+ },
184
+ {
185
+ onLast: () => null
186
+ }
187
+ );
188
+ this.beforeRouteHandler = handler;
189
+ }
190
+ async prepareFrameHandler(options) {
191
+ const { workDir, runner } = this;
192
+ const { onlyApi, onlyWeb } = options || {};
193
+ const { getMiddlewares, ...collector } = createMiddlewareCollecter();
194
+ await runner.gather(collector);
195
+ const { api: pluginAPIExt, web: pluginWebExt } = getMiddlewares();
196
+ const apiDir = path.join(workDir, API_DIR);
197
+ const serverDir = path.join(workDir, SERVER_DIR);
198
+ if (await fs.pathExists(path.join(serverDir)) && !onlyApi) {
199
+ const webExtension = mergeExtension(pluginWebExt);
200
+ this.frameWebHandler = await this.prepareWebHandler(webExtension);
201
+ }
202
+ if (fs.existsSync(apiDir) && !onlyWeb) {
203
+ const apiExtension = mergeExtension(pluginAPIExt);
204
+ this.frameAPIHandler = await this.prepareAPIHandler(apiExtension);
205
+ }
206
+ }
207
+ async prepareWebHandler(extension) {
208
+ const { workDir, runner } = this;
209
+ const handler = await runner.prepareWebServer(
210
+ {
211
+ pwd: workDir,
212
+ config: extension
213
+ },
214
+ { onLast: () => null }
215
+ );
216
+ return handler;
217
+ }
218
+ async prepareAPIHandler(extension) {
219
+ const { workDir, runner, conf } = this;
220
+ const { bff } = conf;
221
+ const prefix = (bff == null ? void 0 : bff.prefix) || "/api";
222
+ return runner.prepareApiServer(
223
+ {
224
+ pwd: workDir,
225
+ config: extension,
226
+ prefix: Array.isArray(prefix) ? prefix[0] : prefix,
227
+ httpMethodDecider: bff == null ? void 0 : bff.httpMethodDecider,
228
+ render: this.render.bind(this)
229
+ },
230
+ { onLast: () => null }
231
+ );
232
+ }
233
+ filterRoutes(routes) {
234
+ return routes;
235
+ }
236
+ async setupBeforeProdMiddleware() {
237
+ const { conf, runner } = this;
238
+ const preMiddleware = await runner.beforeProdServer(conf);
239
+ preMiddleware.flat().forEach((mid) => {
240
+ this.addHandler(mid);
241
+ });
242
+ }
243
+ async handleAPI(context) {
244
+ const { req, res } = context;
245
+ if (!this.frameAPIHandler) {
246
+ throw new Error("can not found api handler");
247
+ }
248
+ await this.frameAPIHandler(req, res);
249
+ }
250
+ async handleWeb(context, route) {
251
+ return this.routeRenderHandler({
252
+ ctx: context,
253
+ route,
254
+ runner: this.runner
255
+ });
256
+ }
257
+ async proxy() {
258
+ return null;
259
+ }
260
+ warmupSSRBundle() {
261
+ const { distDir } = this;
262
+ const bundles = this.router.getBundles();
263
+ bundles.forEach((bundle) => {
264
+ const filepath = path.join(distDir, bundle);
265
+ if (fs.existsSync(filepath)) {
266
+ require(filepath);
267
+ }
268
+ });
269
+ }
270
+ createContext(req, res, options = {}) {
271
+ return createContext(req, res, options);
272
+ }
273
+ async routeHandler(context) {
274
+ const { res } = context;
275
+ const matched = this.router.match(context.path);
276
+ if (!matched) {
277
+ this.render404(context);
278
+ return;
279
+ }
280
+ let route = matched.generate(context.url);
281
+ if (route.isApi) {
282
+ await this.handleAPI(context);
283
+ return;
284
+ }
285
+ const afterMatchContext = createAfterMatchContext(context, route.entryName);
286
+ if (this.runMode === RUN_MODE.FULL) {
287
+ await this.runner.afterMatch(afterMatchContext, { onLast: noop });
288
+ }
289
+ if (this.isSend(res)) {
290
+ return;
291
+ }
292
+ const { current, url, status } = afterMatchContext.router;
293
+ if (url) {
294
+ this.redirect(res, url, status);
295
+ return;
296
+ }
297
+ if (route.entryName !== current) {
298
+ const matched2 = this.router.matchEntry(current);
299
+ if (!matched2) {
300
+ this.render404(context);
301
+ return;
302
+ }
303
+ route = matched2.generate(context.url);
304
+ }
305
+ context.setParams(route.params);
306
+ context.setServerData("router", {
307
+ baseUrl: route.urlPath,
308
+ params: route.params
309
+ });
310
+ if (this.frameWebHandler) {
311
+ res.locals = res.locals || {};
312
+ const middlewareContext = createMiddlewareContext(context);
313
+ await this.frameWebHandler(middlewareContext);
314
+ res.locals = {
315
+ ...res.locals,
316
+ ...middlewareContext.response.locals
317
+ };
318
+ }
319
+ if (this.isSend(res)) {
320
+ return;
321
+ }
322
+ if (route.responseHeaders) {
323
+ Object.keys(route.responseHeaders).forEach((key) => {
324
+ const value = route.responseHeaders[key];
325
+ if (value) {
326
+ context.res.setHeader(key, value);
327
+ }
328
+ });
329
+ }
330
+ const renderResult = await this.handleWeb(context, route);
331
+ if (!renderResult) {
332
+ this.render404(context);
333
+ return;
334
+ }
335
+ if (renderResult.redirect) {
336
+ this.redirect(
337
+ res,
338
+ renderResult.content,
339
+ renderResult.statusCode
340
+ );
341
+ return;
342
+ }
343
+ if (this.isSend(res)) {
344
+ return;
345
+ }
346
+ res.setHeader("content-type", renderResult.contentType);
347
+ const { contentStream } = renderResult;
348
+ if (contentStream) {
349
+ contentStream.pipe(
350
+ templateInjectableStream({
351
+ prependHead: route.entryName ? `<script>window._SERVER_DATA=${JSON.stringify(
352
+ context.serverData
353
+ )}<\/script>` : void 0
354
+ })
355
+ ).pipe(res);
356
+ return;
357
+ }
358
+ let response = renderResult.content;
359
+ if (route.entryName) {
360
+ const afterRenderContext = createAfterRenderContext(
361
+ context,
362
+ response.toString()
363
+ );
364
+ if (this.runMode === RUN_MODE.FULL) {
365
+ await this.runner.afterRender(afterRenderContext, { onLast: noop });
366
+ }
367
+ if (this.isSend(res)) {
368
+ return;
369
+ }
370
+ afterRenderContext.template.prependHead(
371
+ `<script>window._SERVER_DATA=${JSON.stringify(
372
+ context.serverData
373
+ )}<\/script>`
374
+ );
375
+ response = afterRenderContext.template.get();
376
+ }
377
+ res.end(response);
378
+ }
379
+ isSend(res) {
380
+ if (res.headersSent) {
381
+ return true;
382
+ }
383
+ if (res.getHeader("Location") && isRedirect(res.statusCode)) {
384
+ res.end();
385
+ return true;
386
+ }
387
+ return false;
388
+ }
389
+ compose() {
390
+ const { handlers } = this;
391
+ if (!Array.isArray(handlers)) {
392
+ throw new TypeError("Middleware stack must be an array!");
393
+ }
394
+ for (const fn of handlers) {
395
+ if (typeof fn !== "function") {
396
+ throw new TypeError("Middleware must be composed of functions!");
397
+ }
398
+ }
399
+ this._handler = (context, next) => {
400
+ let i = 0;
401
+ const dispatch = (error) => {
402
+ if (error) {
403
+ return this.onError(context, error);
404
+ }
405
+ const handler = handlers[i++];
406
+ if (!handler) {
407
+ return next();
408
+ }
409
+ try {
410
+ const result = handler(context, dispatch);
411
+ if (isPromise(result)) {
412
+ return result.catch(onError);
413
+ }
414
+ } catch (e) {
415
+ return onError(e);
416
+ }
417
+ };
418
+ const onError = (err) => {
419
+ this.onError(context, err);
420
+ };
421
+ return dispatch();
422
+ };
423
+ }
424
+ requestHandler(req, res, next = () => {
425
+ }) {
426
+ res.statusCode = 200;
427
+ req.logger = req.logger || this.logger;
428
+ req.metrics = req.metrics || this.metrics;
429
+ let context;
430
+ try {
431
+ context = this.createContext(req, res);
432
+ } catch (e) {
433
+ this.logger.error(e);
434
+ res.statusCode = 500;
435
+ res.setHeader("content-type", mime.contentType("html"));
436
+ return res.end(createErrorDocument(500, ERROR_PAGE_TEXT[500]));
437
+ }
438
+ try {
439
+ return this._handler(context, next);
440
+ } catch (err) {
441
+ return this.onError(context, err);
442
+ }
443
+ }
444
+ redirect(res, url, status = 302) {
445
+ res.setHeader("Location", url);
446
+ res.statusCode = status;
447
+ res.end();
448
+ }
449
+ onError(context, err) {
450
+ context.error(ERROR_DIGEST.EINTER, err);
451
+ this.renderErrorPage(context, 500);
452
+ }
453
+ async renderErrorPage(context, status) {
454
+ const { res } = context;
455
+ context.status = status;
456
+ res.setHeader("content-type", mime.contentType("html"));
457
+ const statusPage = `/${status}`;
458
+ const customErrorPage = `/_error`;
459
+ const matched = this.router.match(statusPage) || this.router.match(customErrorPage);
460
+ if (matched) {
461
+ const route = matched.generate(context.url);
462
+ const { entryName } = route;
463
+ if (entryName === status.toString() || entryName === "_error") {
464
+ try {
465
+ const file = await this.routeRenderHandler({
466
+ route,
467
+ ctx: context,
468
+ runner: this.runner
469
+ });
470
+ if (file) {
471
+ context.res.end(file.content);
472
+ return;
473
+ }
474
+ } catch (e) {
475
+ }
476
+ }
477
+ }
478
+ const text = ERROR_PAGE_TEXT[status] || ERROR_PAGE_TEXT[500];
479
+ context.res.end(createErrorDocument(status, text));
480
+ }
481
+ }
482
+ export {
483
+ ModernServer
484
+ };
File without changes
@@ -0,0 +1,120 @@
1
+ import { createDebugger, isProd } from "@modern-js/utils";
2
+ const debug = createDebugger("prod-server");
3
+ const mergeExtension = (users) => {
4
+ const output = [];
5
+ return { middleware: output.concat(users) };
6
+ };
7
+ const noop = () => {
8
+ };
9
+ const createErrorDocument = (status, text) => {
10
+ const title = `${status}: ${text}`;
11
+ return `<!DOCTYPE html>
12
+ <html lang="en">
13
+ <head>
14
+ <meta charset="utf-8">
15
+ <meta name="viewport" content="width=device-width">
16
+ <title>${title}</title>
17
+ <style>
18
+ html,body {
19
+ margin: 0;
20
+ }
21
+
22
+ .page-container {
23
+ color: #000;
24
+ background: #fff;
25
+ height: 100vh;
26
+ text-align: center;
27
+ display: flex;
28
+ flex-direction: column;
29
+ align-items: center;
30
+ justify-content: center;
31
+ }
32
+ </style>
33
+ </head>
34
+ <body>
35
+ <div class="page-container">
36
+ <h1>${status}</h1>
37
+ <div>${text}</div>
38
+ </body>
39
+ </html>
40
+ `;
41
+ };
42
+ const createMiddlewareCollecter = () => {
43
+ const webMiddlewares = [];
44
+ const apiMiddlewares = [];
45
+ const addWebMiddleware = (input) => {
46
+ webMiddlewares.push(input);
47
+ };
48
+ const addAPIMiddleware = (input) => {
49
+ apiMiddlewares.push(input);
50
+ };
51
+ const getMiddlewares = () => ({
52
+ web: webMiddlewares,
53
+ api: apiMiddlewares
54
+ });
55
+ return {
56
+ getMiddlewares,
57
+ addWebMiddleware,
58
+ addAPIMiddleware
59
+ };
60
+ };
61
+ const useLocalPrefix = (url) => {
62
+ return isProd() && !url.includes(".");
63
+ };
64
+ const getStaticReg = (output = {}, html = {}) => {
65
+ const {
66
+ distPath: { css: cssPath, js: jsPath, media: mediaPath } = {},
67
+ assetPrefix = "/"
68
+ } = output;
69
+ const { favicon, faviconByEntries } = html;
70
+ const prefix = useLocalPrefix(assetPrefix) ? assetPrefix : "";
71
+ const favicons = prepareFavicons(favicon, faviconByEntries);
72
+ const staticFiles = [cssPath, jsPath, mediaPath].filter((v) => Boolean(v));
73
+ const staticReg = ["static/", "upload/", ...staticFiles];
74
+ const iconReg = ["favicon.ico", "icon.png", ...favicons];
75
+ const regPrefix = prefix.endsWith("/") ? prefix : `${prefix}/`;
76
+ const staticPathRegExp = new RegExp(
77
+ `^${regPrefix}(${[...staticReg, ...iconReg].join("|")})`
78
+ );
79
+ return staticPathRegExp;
80
+ };
81
+ const prepareFavicons = (favicon, faviconByEntries) => {
82
+ const faviconNames = [];
83
+ if (favicon) {
84
+ faviconNames.push(favicon.substring(favicon.lastIndexOf("/") + 1));
85
+ }
86
+ if (faviconByEntries) {
87
+ Object.keys(faviconByEntries).forEach((f) => {
88
+ const curFavicon = faviconByEntries[f];
89
+ if (curFavicon) {
90
+ faviconNames.push(
91
+ curFavicon.substring(curFavicon.lastIndexOf("/") + 1)
92
+ );
93
+ }
94
+ });
95
+ }
96
+ return faviconNames;
97
+ };
98
+ const headersWithoutCookie = (headers) => {
99
+ if (typeof headers.cookie !== "undefined") {
100
+ const safeHeaders = { ...headers };
101
+ delete safeHeaders.cookie;
102
+ return safeHeaders;
103
+ }
104
+ return headers;
105
+ };
106
+ const isRedirect = (code) => {
107
+ return [301, 302, 307, 308].includes(code);
108
+ };
109
+ export {
110
+ createErrorDocument,
111
+ createMiddlewareCollecter,
112
+ debug,
113
+ getStaticReg,
114
+ headersWithoutCookie,
115
+ isRedirect,
116
+ mergeExtension,
117
+ noop,
118
+ prepareFavicons,
119
+ useLocalPrefix
120
+ };
@@ -0,0 +1,69 @@
1
+ import { Logger } from "./libs/logger";
2
+ import { RouteMatchManager } from "./libs/route";
3
+ import { metrics as defaultMetrics } from "./libs/metrics";
4
+ const handleUrl = (url) => {
5
+ return url.replace(/^https?:\/\/.*?\//gi, "/");
6
+ };
7
+ const createHandler = (manifest) => {
8
+ const routeMgr = new RouteMatchManager();
9
+ const { pages, routes } = manifest;
10
+ routeMgr.reset(routes);
11
+ return async (ctx) => {
12
+ var _a, _b, _c, _d, _e, _f;
13
+ const pageMatch = routeMgr.match(ctx.url);
14
+ if (!pageMatch) {
15
+ ctx.body = "404: Page not found";
16
+ ctx.status = 404;
17
+ return;
18
+ }
19
+ const page = pages[pageMatch.spec.urlPath];
20
+ (_b = (_a = ctx.request).query) != null ? _b : _a.query = ctx.query;
21
+ (_d = (_c = ctx.request).pathname) != null ? _d : _c.pathname = ctx.pathname;
22
+ (_f = (_e = ctx.request).params) != null ? _f : _e.params = ctx.params;
23
+ const params = pageMatch.parseURLParams(ctx.url);
24
+ if (page.serverRender) {
25
+ try {
26
+ ctx.body = await page.serverRender({
27
+ entryName: page.entryName,
28
+ template: page.template,
29
+ query: ctx.query,
30
+ request: ctx.request,
31
+ response: ctx.response,
32
+ pathname: ctx.pathname,
33
+ req: ctx.request,
34
+ res: ctx.response,
35
+ params: ctx.params || params || {},
36
+ logger: ctx.logger || new Logger({
37
+ level: "warn"
38
+ }),
39
+ metrics: ctx.metrics || defaultMetrics,
40
+ loadableStats: ctx.loadableStats,
41
+ routeManifest: ctx.routeManifest
42
+ });
43
+ ctx.status = 200;
44
+ return;
45
+ } catch (e) {
46
+ if (page.template) {
47
+ ctx.body = page.template;
48
+ ctx.status = 200;
49
+ return;
50
+ } else {
51
+ ctx.body = "404: not found";
52
+ ctx.status = 404;
53
+ return;
54
+ }
55
+ }
56
+ }
57
+ if (page.template) {
58
+ ctx.body = page.template;
59
+ ctx.status = 200;
60
+ return;
61
+ }
62
+ ctx.body = "404: not found";
63
+ ctx.status = 404;
64
+ };
65
+ };
66
+ export {
67
+ createHandler,
68
+ handleUrl
69
+ };
@@ -26,7 +26,8 @@ import { readFile } from "./reader";
26
26
  import * as ssr from "./ssr";
27
27
  const createRenderHandler = ({
28
28
  distDir,
29
- staticGenerate
29
+ staticGenerate,
30
+ forceCSR
30
31
  }) => function render(_0) {
31
32
  return __async(this, arguments, function* ({
32
33
  ctx,
@@ -50,7 +51,8 @@ const createRenderHandler = ({
50
51
  if (!content) {
51
52
  return null;
52
53
  }
53
- if (route.isSSR) {
54
+ const useCSR = forceCSR && ctx.query.csr;
55
+ if (route.isSSR && !useCSR) {
54
56
  try {
55
57
  const result = yield ssr.render(
56
58
  ctx,