@lolyjs/core 0.2.0-alpha.19 → 0.2.0-alpha.20

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/index.js CHANGED
@@ -10097,7 +10097,30 @@ function loadLayoutServerHook(layoutFile) {
10097
10097
  try {
10098
10098
  const mod = __require(file);
10099
10099
  const serverHook = typeof mod?.getServerSideProps === "function" ? mod.getServerSideProps : null;
10100
- return serverHook;
10100
+ let middlewares = [];
10101
+ const rawMiddlewares = mod?.[NAMING.BEFORE_MIDDLEWARES];
10102
+ if (rawMiddlewares !== void 0) {
10103
+ if (!Array.isArray(rawMiddlewares)) {
10104
+ console.warn(
10105
+ `[framework][server-hook] ${NAMING.BEFORE_MIDDLEWARES} must be an array in ${file}, ignoring invalid value`
10106
+ );
10107
+ } else {
10108
+ for (let i = 0; i < rawMiddlewares.length; i++) {
10109
+ const mw = rawMiddlewares[i];
10110
+ if (typeof mw !== "function") {
10111
+ console.warn(
10112
+ `[framework][server-hook] Middleware at index ${i} in ${NAMING.BEFORE_MIDDLEWARES} is not a function in ${file}, skipping`
10113
+ );
10114
+ continue;
10115
+ }
10116
+ middlewares.push(mw);
10117
+ }
10118
+ }
10119
+ }
10120
+ return {
10121
+ serverHook,
10122
+ middlewares
10123
+ };
10101
10124
  } catch (error) {
10102
10125
  console.error(
10103
10126
  `[framework][server-hook] Error loading layout server hook from ${file}:`,
@@ -10182,9 +10205,16 @@ function loadRoutes(appDir) {
10182
10205
  appDir
10183
10206
  );
10184
10207
  const layoutServerHooks = [];
10208
+ const layoutMiddlewares = [];
10185
10209
  for (const layoutFile of layoutFiles) {
10186
- const layoutServerHook = loadLayoutServerHook(layoutFile);
10187
- layoutServerHooks.push(layoutServerHook);
10210
+ const layoutHookData = loadLayoutServerHook(layoutFile);
10211
+ if (layoutHookData) {
10212
+ layoutServerHooks.push(layoutHookData.serverHook);
10213
+ layoutMiddlewares.push(layoutHookData.middlewares);
10214
+ } else {
10215
+ layoutServerHooks.push(null);
10216
+ layoutMiddlewares.push([]);
10217
+ }
10188
10218
  }
10189
10219
  const { middlewares, serverHook, dynamic, generateStaticParams } = loadServerHookForDir(currentDir);
10190
10220
  routes.push({
@@ -10200,6 +10230,8 @@ function loadRoutes(appDir) {
10200
10230
  // Keep 'loader' field name for backward compatibility
10201
10231
  layoutServerHooks,
10202
10232
  // Server hooks for each layout (same order as layouts)
10233
+ layoutMiddlewares,
10234
+ // Middlewares for each layout (same order as layouts)
10203
10235
  dynamic,
10204
10236
  generateStaticParams
10205
10237
  });
@@ -10763,9 +10795,16 @@ function loadRoutesFromManifest(projectRoot) {
10763
10795
  );
10764
10796
  const pageDir = path8.dirname(pageFile);
10765
10797
  const layoutServerHooks = [];
10798
+ const layoutMiddlewares = [];
10766
10799
  for (const layoutFile of layoutFiles) {
10767
- const layoutServerHook = loadLayoutServerHook(layoutFile);
10768
- layoutServerHooks.push(layoutServerHook);
10800
+ const layoutHookData = loadLayoutServerHook(layoutFile);
10801
+ if (layoutHookData) {
10802
+ layoutServerHooks.push(layoutHookData.serverHook);
10803
+ layoutMiddlewares.push(layoutHookData.middlewares);
10804
+ } else {
10805
+ layoutServerHooks.push(null);
10806
+ layoutMiddlewares.push([]);
10807
+ }
10769
10808
  }
10770
10809
  const { middlewares, serverHook, dynamic, generateStaticParams } = loadServerHookForDir(pageDir);
10771
10810
  pageRoutes.push({
@@ -10781,6 +10820,8 @@ function loadRoutesFromManifest(projectRoot) {
10781
10820
  // Keep 'loader' field name for backward compatibility
10782
10821
  layoutServerHooks,
10783
10822
  // Server hooks for each layout (same order as layouts)
10823
+ layoutMiddlewares,
10824
+ // Middlewares for each layout (same order as layouts)
10784
10825
  dynamic: entry.dynamic ?? dynamic,
10785
10826
  generateStaticParams
10786
10827
  });
@@ -11309,9 +11350,16 @@ function loadNotFoundRouteFromFilesystem(appDir) {
11309
11350
  appDir
11310
11351
  );
11311
11352
  const layoutServerHooks = [];
11353
+ const layoutMiddlewares = [];
11312
11354
  for (const layoutFile of layoutFiles) {
11313
- const layoutServerHook = loadLayoutServerHook(layoutFile);
11314
- layoutServerHooks.push(layoutServerHook);
11355
+ const layoutHookData = loadLayoutServerHook(layoutFile);
11356
+ if (layoutHookData) {
11357
+ layoutServerHooks.push(layoutHookData.serverHook);
11358
+ layoutMiddlewares.push(layoutHookData.middlewares);
11359
+ } else {
11360
+ layoutServerHooks.push(null);
11361
+ layoutMiddlewares.push([]);
11362
+ }
11315
11363
  }
11316
11364
  const { middlewares, serverHook, dynamic, generateStaticParams } = loadServerHookForDir(notFoundDir);
11317
11365
  return {
@@ -11327,6 +11375,8 @@ function loadNotFoundRouteFromFilesystem(appDir) {
11327
11375
  // Keep 'loader' field name for backward compatibility
11328
11376
  layoutServerHooks,
11329
11377
  // Server hooks for each layout (same order as layouts)
11378
+ layoutMiddlewares,
11379
+ // Middlewares for each layout (same order as layouts)
11330
11380
  dynamic,
11331
11381
  generateStaticParams
11332
11382
  };
@@ -11358,9 +11408,16 @@ function loadErrorRouteFromFilesystem(appDir) {
11358
11408
  appDir
11359
11409
  );
11360
11410
  const layoutServerHooks = [];
11411
+ const layoutMiddlewares = [];
11361
11412
  for (const layoutFile of layoutFiles) {
11362
- const layoutServerHook = loadLayoutServerHook(layoutFile);
11363
- layoutServerHooks.push(layoutServerHook);
11413
+ const layoutHookData = loadLayoutServerHook(layoutFile);
11414
+ if (layoutHookData) {
11415
+ layoutServerHooks.push(layoutHookData.serverHook);
11416
+ layoutMiddlewares.push(layoutHookData.middlewares);
11417
+ } else {
11418
+ layoutServerHooks.push(null);
11419
+ layoutMiddlewares.push([]);
11420
+ }
11364
11421
  }
11365
11422
  const { middlewares, serverHook, dynamic, generateStaticParams } = loadServerHookForDir(appDir);
11366
11423
  return {
@@ -11376,6 +11433,8 @@ function loadErrorRouteFromFilesystem(appDir) {
11376
11433
  // Keep 'loader' field name for backward compatibility
11377
11434
  layoutServerHooks,
11378
11435
  // Server hooks for each layout (same order as layouts)
11436
+ layoutMiddlewares,
11437
+ // Middlewares for each layout (same order as layouts)
11379
11438
  dynamic,
11380
11439
  generateStaticParams
11381
11440
  };
@@ -15197,6 +15256,29 @@ async function handlePageRequestInternal(options) {
15197
15256
  if (!skipLayoutHooks && notFoundPage.layoutServerHooks && notFoundPage.layoutServerHooks.length > 0) {
15198
15257
  for (let i = 0; i < notFoundPage.layoutServerHooks.length; i++) {
15199
15258
  const layoutServerHook = notFoundPage.layoutServerHooks[i];
15259
+ const layoutMiddlewares = notFoundPage.layoutMiddlewares?.[i] || [];
15260
+ if (layoutMiddlewares.length > 0) {
15261
+ for (const mw of layoutMiddlewares) {
15262
+ try {
15263
+ await Promise.resolve(
15264
+ mw(ctx2, async () => {
15265
+ })
15266
+ );
15267
+ } catch (error) {
15268
+ const reqLogger2 = getRequestLogger(req);
15269
+ const layoutFile = notFoundPage.layoutFiles[i];
15270
+ const relativeLayoutPath = layoutFile ? path21.relative(projectRoot || process.cwd(), layoutFile) : "unknown";
15271
+ reqLogger2.error("Layout middleware failed for not-found page", error instanceof Error ? error : new Error(String(error)), {
15272
+ layoutIndex: i,
15273
+ layoutFile: relativeLayoutPath
15274
+ });
15275
+ throw error;
15276
+ }
15277
+ if (ctx2.res.headersSent) {
15278
+ return;
15279
+ }
15280
+ }
15281
+ }
15200
15282
  if (layoutServerHook) {
15201
15283
  try {
15202
15284
  const layoutResult = await layoutServerHook(ctx2);
@@ -15314,6 +15396,29 @@ async function handlePageRequestInternal(options) {
15314
15396
  if (!skipLayoutHooks && route.layoutServerHooks && route.layoutServerHooks.length > 0) {
15315
15397
  for (let i = 0; i < route.layoutServerHooks.length; i++) {
15316
15398
  const layoutServerHook = route.layoutServerHooks[i];
15399
+ const layoutMiddlewares = route.layoutMiddlewares?.[i] || [];
15400
+ if (layoutMiddlewares.length > 0) {
15401
+ for (const mw of layoutMiddlewares) {
15402
+ try {
15403
+ await Promise.resolve(
15404
+ mw(ctx, async () => {
15405
+ })
15406
+ );
15407
+ } catch (error) {
15408
+ const layoutFile = route.layoutFiles[i];
15409
+ const relativeLayoutPath = layoutFile ? path21.relative(projectRoot || process.cwd(), layoutFile) : "unknown";
15410
+ reqLogger.error("Layout middleware failed", error instanceof Error ? error : new Error(String(error)), {
15411
+ route: route.pattern,
15412
+ layoutIndex: i,
15413
+ layoutFile: relativeLayoutPath
15414
+ });
15415
+ throw error;
15416
+ }
15417
+ if (ctx.res.headersSent) {
15418
+ return;
15419
+ }
15420
+ }
15421
+ }
15317
15422
  if (layoutServerHook) {
15318
15423
  try {
15319
15424
  const layoutResult = await layoutServerHook(ctx);
@@ -15509,6 +15614,28 @@ async function renderErrorPageWithStream(errorPage, req, res, error, routeChunks
15509
15614
  if (!skipLayoutHooks && errorPage.layoutServerHooks && errorPage.layoutServerHooks.length > 0) {
15510
15615
  for (let i = 0; i < errorPage.layoutServerHooks.length; i++) {
15511
15616
  const layoutServerHook = errorPage.layoutServerHooks[i];
15617
+ const layoutMiddlewares = errorPage.layoutMiddlewares?.[i] || [];
15618
+ if (layoutMiddlewares.length > 0) {
15619
+ for (const mw of layoutMiddlewares) {
15620
+ try {
15621
+ await Promise.resolve(
15622
+ mw(ctx, async () => {
15623
+ })
15624
+ );
15625
+ } catch (error2) {
15626
+ const layoutFile = errorPage.layoutFiles[i];
15627
+ const relativeLayoutPath = layoutFile ? path21.relative(projectRoot || process.cwd(), layoutFile) : "unknown";
15628
+ reqLogger.error("Layout middleware failed for error page", error2 instanceof Error ? error2 : new Error(String(error2)), {
15629
+ layoutIndex: i,
15630
+ layoutFile: relativeLayoutPath
15631
+ });
15632
+ throw error2;
15633
+ }
15634
+ if (ctx.res.headersSent) {
15635
+ return;
15636
+ }
15637
+ }
15638
+ }
15512
15639
  if (layoutServerHook) {
15513
15640
  try {
15514
15641
  const layoutResult = await layoutServerHook(ctx);