@bleedingdev/modern-js-server-core 3.11.3-ultramodern.10 → 3.11.3-ultramodern.11

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.
@@ -68,9 +68,10 @@ async function getHtmlTemplates(pwd, routes) {
68
68
  const templates = Object.fromEntries(htmls);
69
69
  return templates;
70
70
  }
71
- function injectTemplates(pwd, routes, htmlTemplatePromise) {
71
+ function injectTemplates(pwd, routes, htmlTemplatePromise, { lazy = false } = {}) {
72
72
  return async (c, next)=>{
73
73
  if (routes && !c.get('templates')) {
74
+ if (lazy) htmlTemplatePromise ||= getHtmlTemplates(pwd, routes);
74
75
  const templates = await (htmlTemplatePromise || getHtmlTemplates(pwd, routes));
75
76
  c.set('templates', templates);
76
77
  }
@@ -82,9 +83,12 @@ function reportBundleError(filepath, error, monitors) {
82
83
  if (monitors) monitors.error(`Load ${filepath} bundle failed, error = %s`, detail);
83
84
  else console.error(`Load ${filepath} bundle failed, error = ${detail}`);
84
85
  }
85
- const loadBundle = async (filepath, monitors, { required = false, failOnError = false } = {})=>{
86
+ const loadBundle = async (filepath, monitors, { required = false, failOnError = false } = {}, onLoad)=>{
86
87
  if (!required && !failOnError && !await utils_namespaceObject.fs.pathExists(filepath)) return;
88
+ let done;
89
+ let module;
87
90
  try {
91
+ done = onLoad?.();
88
92
  if (!required && failOnError) try {
89
93
  await utils_namespaceObject.fs.access(filepath);
90
94
  } catch (e) {
@@ -92,15 +96,17 @@ const loadBundle = async (filepath, monitors, { required = false, failOnError =
92
96
  if ('ENOENT' === code || 'ENOTDIR' === code) return;
93
97
  throw e;
94
98
  }
95
- const module = await (0, utils_namespaceObject.compatibleRequire)(filepath, false);
96
- return module;
99
+ module = await (0, utils_namespaceObject.compatibleRequire)(filepath, false);
97
100
  } catch (e) {
101
+ done?.('failed');
98
102
  reportBundleError(filepath, e, monitors);
99
103
  if (failOnError) throw e;
100
104
  return;
101
105
  }
106
+ done?.('loaded');
107
+ return module;
102
108
  };
103
- async function getServerManifest(pwd, routes, monitors, { failOnError = false } = {}) {
109
+ async function getServerManifest(pwd, routes, monitors, { failOnError = false, onBundleLoad, requestPath } = {}) {
104
110
  const loaderBundles = {};
105
111
  const renderBundles = {};
106
112
  await Promise.all(routes.filter((route)=>Boolean(route.bundle)).map(async (route)=>{
@@ -111,10 +117,16 @@ async function getServerManifest(pwd, routes, monitors, { failOnError = false }
111
117
  const renderBundle = await loadBundle(renderBundlePath, monitors, {
112
118
  required: failOnBundleError,
113
119
  failOnError: failOnBundleError
114
- });
120
+ }, onBundleLoad && (()=>onBundleLoad({
121
+ filepath: renderBundlePath,
122
+ requestPath
123
+ })));
115
124
  const loaderBundle = await loadBundle(loaderBundlePath, monitors, {
116
125
  failOnError: failOnBundleError
117
- });
126
+ }, onBundleLoad && (()=>onBundleLoad({
127
+ filepath: loaderBundlePath,
128
+ requestPath
129
+ })));
118
130
  renderBundle && (renderBundles[entryName] = renderBundle);
119
131
  if (loaderBundle) try {
120
132
  loaderBundles[entryName] = loaderBundle.loadModules ? await loaderBundle.loadModules() : loaderBundle;
@@ -137,11 +149,16 @@ async function getServerManifest(pwd, routes, monitors, { failOnError = false }
137
149
  nestedRoutesJson
138
150
  };
139
151
  }
140
- function injectServerManifest(pwd, routes, manifestPromise) {
152
+ function injectServerManifest(pwd, routes, manifestPromise, { lazy = false, onBundleLoad } = {}) {
141
153
  return async (c, next)=>{
142
154
  if (routes && !c.get('serverManifest')) {
143
155
  const monitors = c.get('monitors');
144
- const serverManifest = await (manifestPromise || getServerManifest(pwd, routes, monitors));
156
+ const load = ()=>getServerManifest(pwd, routes, monitors, {
157
+ onBundleLoad,
158
+ requestPath: c.req.path
159
+ });
160
+ if (lazy) manifestPromise ||= load();
161
+ const serverManifest = await (manifestPromise || load());
145
162
  c.set('serverManifest', serverManifest);
146
163
  }
147
164
  await next();
@@ -190,26 +207,37 @@ const injectResourcePlugin = ()=>({
190
207
  name: '@modern-js/plugin-inject-resource',
191
208
  setup (api) {
192
209
  api.onPrepare(async ()=>{
193
- const { middlewares, routes, distDirectory: pwd } = api.getServerContext();
210
+ const { middlewares, renderMiddlewares, routes, distDirectory: pwd, renderResources: { loading, onBundleLoad } = {} } = api.getServerContext();
211
+ const lazy = (0, utils_namespaceObject.isProd)() && 'lazy' === loading;
194
212
  let htmlTemplatePromise;
195
213
  let manifestPromise;
196
- if ((0, utils_namespaceObject.isProd)()) {
214
+ if ((0, utils_namespaceObject.isProd)() && !lazy) {
197
215
  const warmupRoutes = routes || [];
198
216
  const failOnError = warmupRoutes.some((route)=>route.isSSR && route.bundle) && !warmupRoutes.some((route)=>route.isRSC);
199
217
  manifestPromise = getServerManifest(pwd, warmupRoutes, void 0, {
200
- failOnError
218
+ failOnError,
219
+ onBundleLoad
201
220
  });
202
221
  htmlTemplatePromise = getHtmlTemplates(pwd, routes || []);
203
222
  if (failOnError) await manifestPromise;
204
223
  }
205
- middlewares.push({
206
- name: 'inject-server-manifest',
207
- handler: injectServerManifest(pwd, routes, manifestPromise)
208
- });
209
- middlewares.push({
210
- name: 'inject-html',
211
- handler: injectTemplates(pwd, routes, htmlTemplatePromise)
212
- });
224
+ const resourceMiddlewares = [
225
+ {
226
+ name: 'inject-server-manifest',
227
+ handler: injectServerManifest(pwd, routes, manifestPromise, {
228
+ lazy,
229
+ onBundleLoad
230
+ })
231
+ },
232
+ {
233
+ name: 'inject-html',
234
+ handler: injectTemplates(pwd, routes, htmlTemplatePromise, {
235
+ lazy
236
+ })
237
+ }
238
+ ];
239
+ if (lazy) renderMiddlewares.unshift(...resourceMiddlewares);
240
+ else middlewares.push(...resourceMiddlewares);
213
241
  });
214
242
  }
215
243
  });
@@ -50,6 +50,7 @@ var external_path_default = /*#__PURE__*/ __webpack_require__.n(external_path_na
50
50
  const index_js_namespaceObject = require("../../../utils/index.js");
51
51
  const publicDir_js_namespaceObject = require("../../../utils/publicDir.js");
52
52
  const external_staticCache_js_namespaceObject = require("./staticCache.js");
53
+ const statFile = (filename)=>utils_namespaceObject.fs.stat(filename).catch(()=>null);
53
54
  const isPathInside = (target, root)=>{
54
55
  const relative = external_path_default().relative(external_path_default().resolve(root), external_path_default().resolve(target));
55
56
  return '' === relative || !relative.startsWith(`..${external_path_default().sep}`) && '..' !== relative && !external_path_default().isAbsolute(relative);
@@ -75,7 +76,7 @@ async function serveStaticAsset(context, asset, respond) {
75
76
  return null;
76
77
  }
77
78
  if ('static' === kind && !asset.realpath) {
78
- const stat = await utils_namespaceObject.fs.stat(filename).catch(()=>null);
79
+ const stat = await (asset.stat ?? statFile)(filename);
79
80
  if (!stat?.isFile()) return null;
80
81
  }
81
82
  if (respond) return respond(asset, (representation)=>serveStaticAsset(context, {
@@ -217,12 +218,13 @@ function createStaticMiddleware(options) {
217
218
  const filepath = external_path_default().join(pwd, relativePath);
218
219
  if (!isPathInside(filepath, pwd)) return next();
219
220
  if (options.respondAsset) {
220
- const stat = await utils_namespaceObject.fs.stat(filepath).catch(()=>null);
221
+ const stat = await (options.stat ?? statFile)(filepath);
221
222
  if (!stat?.isFile()) return next();
222
223
  }
223
224
  const asset = {
224
225
  filename: filepath,
225
226
  kind: 'static',
227
+ stat: options.stat,
226
228
  cacheControl: cacheControl && (isHashedAsset(relativePath) ? cacheControl.hashed : cacheControl.unhashed)
227
229
  };
228
230
  const serve = (representation)=>serveStaticAsset(c, {
@@ -18,9 +18,10 @@ async function getHtmlTemplates(pwd, routes) {
18
18
  const templates = Object.fromEntries(htmls);
19
19
  return templates;
20
20
  }
21
- function injectTemplates(pwd, routes, htmlTemplatePromise) {
21
+ function injectTemplates(pwd, routes, htmlTemplatePromise, { lazy = false } = {}) {
22
22
  return async (c, next)=>{
23
23
  if (routes && !c.get('templates')) {
24
+ if (lazy) htmlTemplatePromise ||= getHtmlTemplates(pwd, routes);
24
25
  const templates = await (htmlTemplatePromise || getHtmlTemplates(pwd, routes));
25
26
  c.set('templates', templates);
26
27
  }
@@ -32,9 +33,12 @@ function reportBundleError(filepath, error, monitors) {
32
33
  if (monitors) monitors.error(`Load ${filepath} bundle failed, error = %s`, detail);
33
34
  else console.error(`Load ${filepath} bundle failed, error = ${detail}`);
34
35
  }
35
- const loadBundle = async (filepath, monitors, { required = false, failOnError = false } = {})=>{
36
+ const loadBundle = async (filepath, monitors, { required = false, failOnError = false } = {}, onLoad)=>{
36
37
  if (!required && !failOnError && !await fs.pathExists(filepath)) return;
38
+ let done;
39
+ let module;
37
40
  try {
41
+ done = onLoad?.();
38
42
  if (!required && failOnError) try {
39
43
  await fs.access(filepath);
40
44
  } catch (e) {
@@ -42,15 +46,17 @@ const loadBundle = async (filepath, monitors, { required = false, failOnError =
42
46
  if ('ENOENT' === code || 'ENOTDIR' === code) return;
43
47
  throw e;
44
48
  }
45
- const module = await compatibleRequire(filepath, false);
46
- return module;
49
+ module = await compatibleRequire(filepath, false);
47
50
  } catch (e) {
51
+ done?.('failed');
48
52
  reportBundleError(filepath, e, monitors);
49
53
  if (failOnError) throw e;
50
54
  return;
51
55
  }
56
+ done?.('loaded');
57
+ return module;
52
58
  };
53
- async function getServerManifest(pwd, routes, monitors, { failOnError = false } = {}) {
59
+ async function getServerManifest(pwd, routes, monitors, { failOnError = false, onBundleLoad, requestPath } = {}) {
54
60
  const loaderBundles = {};
55
61
  const renderBundles = {};
56
62
  await Promise.all(routes.filter((route)=>Boolean(route.bundle)).map(async (route)=>{
@@ -61,10 +67,16 @@ async function getServerManifest(pwd, routes, monitors, { failOnError = false }
61
67
  const renderBundle = await loadBundle(renderBundlePath, monitors, {
62
68
  required: failOnBundleError,
63
69
  failOnError: failOnBundleError
64
- });
70
+ }, onBundleLoad && (()=>onBundleLoad({
71
+ filepath: renderBundlePath,
72
+ requestPath
73
+ })));
65
74
  const loaderBundle = await loadBundle(loaderBundlePath, monitors, {
66
75
  failOnError: failOnBundleError
67
- });
76
+ }, onBundleLoad && (()=>onBundleLoad({
77
+ filepath: loaderBundlePath,
78
+ requestPath
79
+ })));
68
80
  renderBundle && (renderBundles[entryName] = renderBundle);
69
81
  if (loaderBundle) try {
70
82
  loaderBundles[entryName] = loaderBundle.loadModules ? await loaderBundle.loadModules() : loaderBundle;
@@ -87,11 +99,16 @@ async function getServerManifest(pwd, routes, monitors, { failOnError = false }
87
99
  nestedRoutesJson
88
100
  };
89
101
  }
90
- function injectServerManifest(pwd, routes, manifestPromise) {
102
+ function injectServerManifest(pwd, routes, manifestPromise, { lazy = false, onBundleLoad } = {}) {
91
103
  return async (c, next)=>{
92
104
  if (routes && !c.get('serverManifest')) {
93
105
  const monitors = c.get('monitors');
94
- const serverManifest = await (manifestPromise || getServerManifest(pwd, routes, monitors));
106
+ const load = ()=>getServerManifest(pwd, routes, monitors, {
107
+ onBundleLoad,
108
+ requestPath: c.req.path
109
+ });
110
+ if (lazy) manifestPromise ||= load();
111
+ const serverManifest = await (manifestPromise || load());
95
112
  c.set('serverManifest', serverManifest);
96
113
  }
97
114
  await next();
@@ -140,26 +157,37 @@ const injectResourcePlugin = ()=>({
140
157
  name: '@modern-js/plugin-inject-resource',
141
158
  setup (api) {
142
159
  api.onPrepare(async ()=>{
143
- const { middlewares, routes, distDirectory: pwd } = api.getServerContext();
160
+ const { middlewares, renderMiddlewares, routes, distDirectory: pwd, renderResources: { loading, onBundleLoad } = {} } = api.getServerContext();
161
+ const lazy = isProd() && 'lazy' === loading;
144
162
  let htmlTemplatePromise;
145
163
  let manifestPromise;
146
- if (isProd()) {
164
+ if (isProd() && !lazy) {
147
165
  const warmupRoutes = routes || [];
148
166
  const failOnError = warmupRoutes.some((route)=>route.isSSR && route.bundle) && !warmupRoutes.some((route)=>route.isRSC);
149
167
  manifestPromise = getServerManifest(pwd, warmupRoutes, void 0, {
150
- failOnError
168
+ failOnError,
169
+ onBundleLoad
151
170
  });
152
171
  htmlTemplatePromise = getHtmlTemplates(pwd, routes || []);
153
172
  if (failOnError) await manifestPromise;
154
173
  }
155
- middlewares.push({
156
- name: 'inject-server-manifest',
157
- handler: injectServerManifest(pwd, routes, manifestPromise)
158
- });
159
- middlewares.push({
160
- name: 'inject-html',
161
- handler: injectTemplates(pwd, routes, htmlTemplatePromise)
162
- });
174
+ const resourceMiddlewares = [
175
+ {
176
+ name: 'inject-server-manifest',
177
+ handler: injectServerManifest(pwd, routes, manifestPromise, {
178
+ lazy,
179
+ onBundleLoad
180
+ })
181
+ },
182
+ {
183
+ name: 'inject-html',
184
+ handler: injectTemplates(pwd, routes, htmlTemplatePromise, {
185
+ lazy
186
+ })
187
+ }
188
+ ];
189
+ if (lazy) renderMiddlewares.unshift(...resourceMiddlewares);
190
+ else middlewares.push(...resourceMiddlewares);
163
191
  });
164
192
  }
165
193
  });
@@ -5,6 +5,7 @@ import path from "path";
5
5
  import { sortRoutes } from "../../../utils/index.mjs";
6
6
  import { getPublicDirPatterns } from "../../../utils/publicDir.mjs";
7
7
  import { createHashedAssetMatcher, resolveStaticCacheControl, setStaticFileHeaders } from "./staticCache.mjs";
8
+ const statFile = (filename)=>fs.stat(filename).catch(()=>null);
8
9
  const isPathInside = (target, root)=>{
9
10
  const relative = path.relative(path.resolve(root), path.resolve(target));
10
11
  return '' === relative || !relative.startsWith(`..${path.sep}`) && '..' !== relative && !path.isAbsolute(relative);
@@ -30,7 +31,7 @@ async function serveStaticAsset(context, asset, respond) {
30
31
  return null;
31
32
  }
32
33
  if ('static' === kind && !asset.realpath) {
33
- const stat = await fs.stat(filename).catch(()=>null);
34
+ const stat = await (asset.stat ?? statFile)(filename);
34
35
  if (!stat?.isFile()) return null;
35
36
  }
36
37
  if (respond) return respond(asset, (representation)=>serveStaticAsset(context, {
@@ -172,12 +173,13 @@ function createStaticMiddleware(options) {
172
173
  const filepath = path.join(pwd, relativePath);
173
174
  if (!isPathInside(filepath, pwd)) return next();
174
175
  if (options.respondAsset) {
175
- const stat = await fs.stat(filepath).catch(()=>null);
176
+ const stat = await (options.stat ?? statFile)(filepath);
176
177
  if (!stat?.isFile()) return next();
177
178
  }
178
179
  const asset = {
179
180
  filename: filepath,
180
181
  kind: 'static',
182
+ stat: options.stat,
181
183
  cacheControl: cacheControl && (isHashedAsset(relativePath) ? cacheControl.hashed : cacheControl.unhashed)
182
184
  };
183
185
  const serve = (representation)=>serveStaticAsset(c, {
@@ -19,9 +19,10 @@ async function getHtmlTemplates(pwd, routes) {
19
19
  const templates = Object.fromEntries(htmls);
20
20
  return templates;
21
21
  }
22
- function injectTemplates(pwd, routes, htmlTemplatePromise) {
22
+ function injectTemplates(pwd, routes, htmlTemplatePromise, { lazy = false } = {}) {
23
23
  return async (c, next)=>{
24
24
  if (routes && !c.get('templates')) {
25
+ if (lazy) htmlTemplatePromise ||= getHtmlTemplates(pwd, routes);
25
26
  const templates = await (htmlTemplatePromise || getHtmlTemplates(pwd, routes));
26
27
  c.set('templates', templates);
27
28
  }
@@ -33,9 +34,12 @@ function reportBundleError(filepath, error, monitors) {
33
34
  if (monitors) monitors.error(`Load ${filepath} bundle failed, error = %s`, detail);
34
35
  else console.error(`Load ${filepath} bundle failed, error = ${detail}`);
35
36
  }
36
- const loadBundle = async (filepath, monitors, { required = false, failOnError = false } = {})=>{
37
+ const loadBundle = async (filepath, monitors, { required = false, failOnError = false } = {}, onLoad)=>{
37
38
  if (!required && !failOnError && !await fs.pathExists(filepath)) return;
39
+ let done;
40
+ let module;
38
41
  try {
42
+ done = onLoad?.();
39
43
  if (!required && failOnError) try {
40
44
  await fs.access(filepath);
41
45
  } catch (e) {
@@ -43,15 +47,17 @@ const loadBundle = async (filepath, monitors, { required = false, failOnError =
43
47
  if ('ENOENT' === code || 'ENOTDIR' === code) return;
44
48
  throw e;
45
49
  }
46
- const module = await compatibleRequire(filepath, false);
47
- return module;
50
+ module = await compatibleRequire(filepath, false);
48
51
  } catch (e) {
52
+ done?.('failed');
49
53
  reportBundleError(filepath, e, monitors);
50
54
  if (failOnError) throw e;
51
55
  return;
52
56
  }
57
+ done?.('loaded');
58
+ return module;
53
59
  };
54
- async function getServerManifest(pwd, routes, monitors, { failOnError = false } = {}) {
60
+ async function getServerManifest(pwd, routes, monitors, { failOnError = false, onBundleLoad, requestPath } = {}) {
55
61
  const loaderBundles = {};
56
62
  const renderBundles = {};
57
63
  await Promise.all(routes.filter((route)=>Boolean(route.bundle)).map(async (route)=>{
@@ -62,10 +68,16 @@ async function getServerManifest(pwd, routes, monitors, { failOnError = false }
62
68
  const renderBundle = await loadBundle(renderBundlePath, monitors, {
63
69
  required: failOnBundleError,
64
70
  failOnError: failOnBundleError
65
- });
71
+ }, onBundleLoad && (()=>onBundleLoad({
72
+ filepath: renderBundlePath,
73
+ requestPath
74
+ })));
66
75
  const loaderBundle = await loadBundle(loaderBundlePath, monitors, {
67
76
  failOnError: failOnBundleError
68
- });
77
+ }, onBundleLoad && (()=>onBundleLoad({
78
+ filepath: loaderBundlePath,
79
+ requestPath
80
+ })));
69
81
  renderBundle && (renderBundles[entryName] = renderBundle);
70
82
  if (loaderBundle) try {
71
83
  loaderBundles[entryName] = loaderBundle.loadModules ? await loaderBundle.loadModules() : loaderBundle;
@@ -88,11 +100,16 @@ async function getServerManifest(pwd, routes, monitors, { failOnError = false }
88
100
  nestedRoutesJson
89
101
  };
90
102
  }
91
- function injectServerManifest(pwd, routes, manifestPromise) {
103
+ function injectServerManifest(pwd, routes, manifestPromise, { lazy = false, onBundleLoad } = {}) {
92
104
  return async (c, next)=>{
93
105
  if (routes && !c.get('serverManifest')) {
94
106
  const monitors = c.get('monitors');
95
- const serverManifest = await (manifestPromise || getServerManifest(pwd, routes, monitors));
107
+ const load = ()=>getServerManifest(pwd, routes, monitors, {
108
+ onBundleLoad,
109
+ requestPath: c.req.path
110
+ });
111
+ if (lazy) manifestPromise ||= load();
112
+ const serverManifest = await (manifestPromise || load());
96
113
  c.set('serverManifest', serverManifest);
97
114
  }
98
115
  await next();
@@ -141,26 +158,37 @@ const injectResourcePlugin = ()=>({
141
158
  name: '@modern-js/plugin-inject-resource',
142
159
  setup (api) {
143
160
  api.onPrepare(async ()=>{
144
- const { middlewares, routes, distDirectory: pwd } = api.getServerContext();
161
+ const { middlewares, renderMiddlewares, routes, distDirectory: pwd, renderResources: { loading, onBundleLoad } = {} } = api.getServerContext();
162
+ const lazy = isProd() && 'lazy' === loading;
145
163
  let htmlTemplatePromise;
146
164
  let manifestPromise;
147
- if (isProd()) {
165
+ if (isProd() && !lazy) {
148
166
  const warmupRoutes = routes || [];
149
167
  const failOnError = warmupRoutes.some((route)=>route.isSSR && route.bundle) && !warmupRoutes.some((route)=>route.isRSC);
150
168
  manifestPromise = getServerManifest(pwd, warmupRoutes, void 0, {
151
- failOnError
169
+ failOnError,
170
+ onBundleLoad
152
171
  });
153
172
  htmlTemplatePromise = getHtmlTemplates(pwd, routes || []);
154
173
  if (failOnError) await manifestPromise;
155
174
  }
156
- middlewares.push({
157
- name: 'inject-server-manifest',
158
- handler: injectServerManifest(pwd, routes, manifestPromise)
159
- });
160
- middlewares.push({
161
- name: 'inject-html',
162
- handler: injectTemplates(pwd, routes, htmlTemplatePromise)
163
- });
175
+ const resourceMiddlewares = [
176
+ {
177
+ name: 'inject-server-manifest',
178
+ handler: injectServerManifest(pwd, routes, manifestPromise, {
179
+ lazy,
180
+ onBundleLoad
181
+ })
182
+ },
183
+ {
184
+ name: 'inject-html',
185
+ handler: injectTemplates(pwd, routes, htmlTemplatePromise, {
186
+ lazy
187
+ })
188
+ }
189
+ ];
190
+ if (lazy) renderMiddlewares.unshift(...resourceMiddlewares);
191
+ else middlewares.push(...resourceMiddlewares);
164
192
  });
165
193
  }
166
194
  });
@@ -6,6 +6,7 @@ import path from "path";
6
6
  import { sortRoutes } from "../../../utils/index.mjs";
7
7
  import { getPublicDirPatterns } from "../../../utils/publicDir.mjs";
8
8
  import { createHashedAssetMatcher, resolveStaticCacheControl, setStaticFileHeaders } from "./staticCache.mjs";
9
+ const statFile = (filename)=>fs.stat(filename).catch(()=>null);
9
10
  const isPathInside = (target, root)=>{
10
11
  const relative = path.relative(path.resolve(root), path.resolve(target));
11
12
  return '' === relative || !relative.startsWith(`..${path.sep}`) && '..' !== relative && !path.isAbsolute(relative);
@@ -31,7 +32,7 @@ async function serveStaticAsset(context, asset, respond) {
31
32
  return null;
32
33
  }
33
34
  if ('static' === kind && !asset.realpath) {
34
- const stat = await fs.stat(filename).catch(()=>null);
35
+ const stat = await (asset.stat ?? statFile)(filename);
35
36
  if (!stat?.isFile()) return null;
36
37
  }
37
38
  if (respond) return respond(asset, (representation)=>serveStaticAsset(context, {
@@ -173,12 +174,13 @@ function createStaticMiddleware(options) {
173
174
  const filepath = path.join(pwd, relativePath);
174
175
  if (!isPathInside(filepath, pwd)) return next();
175
176
  if (options.respondAsset) {
176
- const stat = await fs.stat(filepath).catch(()=>null);
177
+ const stat = await (options.stat ?? statFile)(filepath);
177
178
  if (!stat?.isFile()) return next();
178
179
  }
179
180
  const asset = {
180
181
  filename: filepath,
181
182
  kind: 'static',
183
+ stat: options.stat,
182
184
  cacheControl: cacheControl && (isHashedAsset(relativePath) ? cacheControl.hashed : cacheControl.unhashed)
183
185
  };
184
186
  const serve = (representation)=>serveStaticAsset(c, {
@@ -3,5 +3,6 @@ export type { ServerNodeContext, ServerNodeMiddleware } from './hono.js';
3
3
  export { connectMid2HonoMid, connectMockMid2HonoMid, httpCallBack2HonoMid, } from './hono.js';
4
4
  export { createNodeServer, createWebRequest, sendResponse, } from './node.js';
5
5
  export { getHtmlTemplates, getServerManifest, injectNodeSeverPlugin, injectResourcePlugin, injectRscManifestPlugin, serverStaticPlugin, } from './plugins/index.js';
6
- export type { ServerStaticPluginOptions, ServeStaticAsset, StaticAsset, StaticAssetRequest, StaticAssetResponder, StaticPublicFallbackResponder, } from './plugins/static.js';
6
+ export type { RenderBundleLoad, ServerRenderResourceOptions, } from './plugins/resource.js';
7
+ export type { ServerStaticPluginOptions, ServeStaticAsset, StaticAsset, StaticAssetRequest, StaticAssetResponder, StaticAssetStat, StaticPublicFallbackResponder, } from './plugins/static.js';
7
8
  export { serveStaticAsset } from './plugins/static.js';
@@ -1,13 +1,39 @@
1
1
  import type { Monitors, ServerRoute } from '@modern-js/types';
2
2
  import type { Middleware, ServerEnv, ServerManifest, ServerPlugin } from '../../../types/index.js';
3
+ /** A server bundle import that is about to start. */
4
+ export type RenderBundleLoad = {
5
+ filepath: string;
6
+ /** The request that triggered the import; absent during production warmup. */
7
+ requestPath?: string;
8
+ };
9
+ /** Render resource loading, configured by trusted server plugins during setup. */
10
+ export type ServerRenderResourceOptions = {
11
+ /**
12
+ * Production only. `eager` (default) imports server bundles while preparing
13
+ * and injects them before every middleware. `lazy` injects them with the
14
+ * render middlewares and imports them once, on the first render request.
15
+ */
16
+ loading?: 'eager' | 'lazy';
17
+ /**
18
+ * Called before each bundle import. The returned callback receives its
19
+ * outcome; it is not called when an optional bundle turns out to be absent.
20
+ */
21
+ onBundleLoad?: (bundle: RenderBundleLoad) => ((outcome: 'loaded' | 'failed') => void) | undefined;
22
+ };
23
+ type ResourceInjection = Pick<ServerRenderResourceOptions, 'onBundleLoad'> & {
24
+ /** Share the first request's load with every later request. */
25
+ lazy?: boolean;
26
+ };
3
27
  export declare function getHtmlTemplates(pwd: string, routes: ServerRoute[]): Promise<Record<string, string>>;
4
- export declare function injectTemplates(pwd: string, routes?: ServerRoute[], htmlTemplatePromise?: ReturnType<typeof getHtmlTemplates>): Middleware<ServerEnv>;
5
- export declare function getServerManifest(pwd: string, routes: ServerRoute[], monitors?: Monitors, { failOnError }?: {
6
- failOnError?: boolean | undefined;
7
- }): Promise<ServerManifest>;
8
- export declare function injectServerManifest(pwd: string, routes?: ServerRoute[], manifestPromise?: Promise<ServerManifest>): Middleware<ServerEnv>;
28
+ export declare function injectTemplates(pwd: string, routes?: ServerRoute[], htmlTemplatePromise?: ReturnType<typeof getHtmlTemplates>, { lazy }?: ResourceInjection): Middleware<ServerEnv>;
29
+ export declare function getServerManifest(pwd: string, routes: ServerRoute[], monitors?: Monitors, { failOnError, onBundleLoad, requestPath, }?: {
30
+ failOnError?: boolean;
31
+ requestPath?: string;
32
+ } & Pick<ServerRenderResourceOptions, 'onBundleLoad'>): Promise<ServerManifest>;
33
+ export declare function injectServerManifest(pwd: string, routes?: ServerRoute[], manifestPromise?: Promise<ServerManifest>, { lazy, onBundleLoad }?: ResourceInjection): Middleware<ServerEnv>;
9
34
  export declare function getRscServerManifest(pwd: string): Promise<any>;
10
35
  export declare function getClientManifest(pwd: string): Promise<any>;
11
36
  export declare function getRscSSRManifest(pwd: string): Promise<any>;
12
37
  export declare const injectRscManifestPlugin: (enableRsc: boolean) => ServerPlugin;
13
38
  export declare const injectResourcePlugin: () => ServerPlugin;
39
+ export {};
@@ -1,6 +1,10 @@
1
1
  import type { ServerRoute } from '@modern-js/types';
2
2
  import type { HtmlNormalizedConfig, Middleware, OutputNormalizedConfig, ServerNormalizedConfig, ServerPlugin } from '../../../types/index.js';
3
3
  import { type ResolvedStaticCacheControl } from './staticCache.js';
4
+ /** File metadata for a contained path; null when it cannot be read. */
5
+ export type StaticAssetStat = (filename: string) => Promise<{
6
+ isFile(): boolean;
7
+ } | null>;
4
8
  /** A selected file and the native response conventions to use for it. */
5
9
  export type StaticAsset = {
6
10
  filename: string;
@@ -17,6 +21,8 @@ export type StaticAsset = {
17
21
  root?: string;
18
22
  /** Resolve symlinks before checking root. Implies a regular file is required. */
19
23
  realpath?: boolean;
24
+ /** Metadata lookup for unresolved static files, after containment checks. */
25
+ stat?: StaticAssetStat;
20
26
  };
21
27
  export type StaticAssetRequest = {
22
28
  root: string;
@@ -40,6 +46,8 @@ export declare function serveStaticAsset(context: Parameters<Middleware>[0], ass
40
46
  export type ServerStaticPluginOptions = {
41
47
  respondAsset?: StaticAssetResponder;
42
48
  respondPublicFallback?: StaticPublicFallbackResponder;
49
+ /** Metadata lookup for matched static files, after containment checks. */
50
+ stat?: StaticAssetStat;
43
51
  };
44
52
  export declare const serverStaticPlugin: (options?: ServerStaticPluginOptions) => ServerPlugin;
45
53
  export type PublicMiddlewareOptions = {
@@ -2,6 +2,7 @@ import type { AsyncPipelineHook, ServerContext as BaseServerContext, ServerPlugi
2
2
  import type { Hooks } from '@modern-js/plugin/server';
3
3
  import type { AfterMatchContext, AfterRenderContext, AfterStreamingRenderContext } from '@modern-js/types';
4
4
  import type { Context, MiddlewareHandler } from 'hono';
5
+ import type { ServerRenderResourceOptions } from '../../adapters/node/plugins/resource.js';
5
6
  import type { ServerStaticPluginOptions } from '../../adapters/node/plugins/static.js';
6
7
  import type { APIServerStartInput, MiddlewareObj, ServerConfig, WebAdapter, WebServerStartInput } from './base.js';
7
8
  export type PrepareWebServerFn = (input: WebServerStartInput) => Promise<WebAdapter | null>;
@@ -24,6 +25,8 @@ export interface ServerPluginExtends extends BaseServerPluginExtends {
24
25
  extendContext: {
25
26
  /** Node static response extensions registered during plugin setup. */
26
27
  staticAssetResponders?: ServerStaticPluginOptions;
28
+ /** Node render resource loading registered during plugin setup. */
29
+ renderResources?: ServerRenderResourceOptions;
27
30
  middlewares: MiddlewareObj[];
28
31
  renderMiddlewares: MiddlewareObj[];
29
32
  [key: string]: any;
package/package.json CHANGED
@@ -17,7 +17,7 @@
17
17
  "modern",
18
18
  "modern.js"
19
19
  ],
20
- "version": "3.11.3-ultramodern.10",
20
+ "version": "3.11.3-ultramodern.11",
21
21
  "types": "./dist/types/index.d.ts",
22
22
  "main": "./dist/cjs/index.js",
23
23
  "exports": {
@@ -72,9 +72,9 @@
72
72
  "node": ">=20"
73
73
  },
74
74
  "dependencies": {
75
- "@modern-js/plugin": "npm:@bleedingdev/modern-js-plugin@3.11.3-ultramodern.10",
76
- "@modern-js/runtime-utils": "npm:@bleedingdev/modern-js-runtime-utils@3.11.3-ultramodern.10",
77
- "@modern-js/utils": "npm:@bleedingdev/modern-js-utils@3.11.3-ultramodern.10",
75
+ "@modern-js/plugin": "npm:@bleedingdev/modern-js-plugin@3.11.3-ultramodern.11",
76
+ "@modern-js/runtime-utils": "npm:@bleedingdev/modern-js-runtime-utils@3.11.3-ultramodern.11",
77
+ "@modern-js/utils": "npm:@bleedingdev/modern-js-utils@3.11.3-ultramodern.11",
78
78
  "@swc/helpers": "^0.5.24",
79
79
  "@web-std/fetch": "^4.2.1",
80
80
  "@web-std/file": "^3.0.3",
@@ -85,7 +85,7 @@
85
85
  "ts-deepmerge": "8.0.0"
86
86
  },
87
87
  "devDependencies": {
88
- "@modern-js/types": "npm:@bleedingdev/modern-js-types@3.11.3-ultramodern.10",
88
+ "@modern-js/types": "npm:@bleedingdev/modern-js-types@3.11.3-ultramodern.11",
89
89
  "@rslib/core": "1.0.7",
90
90
  "@scripts/rstest-config": "2.66.7",
91
91
  "@types/cloneable-readable": "^2.0.3",