@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/cli.js CHANGED
@@ -10057,7 +10057,30 @@ function loadLayoutServerHook(layoutFile) {
10057
10057
  try {
10058
10058
  const mod = __require(file);
10059
10059
  const serverHook = typeof mod?.getServerSideProps === "function" ? mod.getServerSideProps : null;
10060
- return serverHook;
10060
+ let middlewares = [];
10061
+ const rawMiddlewares = mod?.[NAMING.BEFORE_MIDDLEWARES];
10062
+ if (rawMiddlewares !== void 0) {
10063
+ if (!Array.isArray(rawMiddlewares)) {
10064
+ console.warn(
10065
+ `[framework][server-hook] ${NAMING.BEFORE_MIDDLEWARES} must be an array in ${file}, ignoring invalid value`
10066
+ );
10067
+ } else {
10068
+ for (let i = 0; i < rawMiddlewares.length; i++) {
10069
+ const mw = rawMiddlewares[i];
10070
+ if (typeof mw !== "function") {
10071
+ console.warn(
10072
+ `[framework][server-hook] Middleware at index ${i} in ${NAMING.BEFORE_MIDDLEWARES} is not a function in ${file}, skipping`
10073
+ );
10074
+ continue;
10075
+ }
10076
+ middlewares.push(mw);
10077
+ }
10078
+ }
10079
+ }
10080
+ return {
10081
+ serverHook,
10082
+ middlewares
10083
+ };
10061
10084
  } catch (error) {
10062
10085
  console.error(
10063
10086
  `[framework][server-hook] Error loading layout server hook from ${file}:`,
@@ -10142,9 +10165,16 @@ function loadRoutes(appDir) {
10142
10165
  appDir
10143
10166
  );
10144
10167
  const layoutServerHooks = [];
10168
+ const layoutMiddlewares = [];
10145
10169
  for (const layoutFile of layoutFiles) {
10146
- const layoutServerHook = loadLayoutServerHook(layoutFile);
10147
- layoutServerHooks.push(layoutServerHook);
10170
+ const layoutHookData = loadLayoutServerHook(layoutFile);
10171
+ if (layoutHookData) {
10172
+ layoutServerHooks.push(layoutHookData.serverHook);
10173
+ layoutMiddlewares.push(layoutHookData.middlewares);
10174
+ } else {
10175
+ layoutServerHooks.push(null);
10176
+ layoutMiddlewares.push([]);
10177
+ }
10148
10178
  }
10149
10179
  const { middlewares, serverHook, dynamic, generateStaticParams } = loadServerHookForDir(currentDir);
10150
10180
  routes.push({
@@ -10160,6 +10190,8 @@ function loadRoutes(appDir) {
10160
10190
  // Keep 'loader' field name for backward compatibility
10161
10191
  layoutServerHooks,
10162
10192
  // Server hooks for each layout (same order as layouts)
10193
+ layoutMiddlewares,
10194
+ // Middlewares for each layout (same order as layouts)
10163
10195
  dynamic,
10164
10196
  generateStaticParams
10165
10197
  });
@@ -10723,9 +10755,16 @@ function loadRoutesFromManifest(projectRoot) {
10723
10755
  );
10724
10756
  const pageDir = path7.dirname(pageFile);
10725
10757
  const layoutServerHooks = [];
10758
+ const layoutMiddlewares = [];
10726
10759
  for (const layoutFile of layoutFiles) {
10727
- const layoutServerHook = loadLayoutServerHook(layoutFile);
10728
- layoutServerHooks.push(layoutServerHook);
10760
+ const layoutHookData = loadLayoutServerHook(layoutFile);
10761
+ if (layoutHookData) {
10762
+ layoutServerHooks.push(layoutHookData.serverHook);
10763
+ layoutMiddlewares.push(layoutHookData.middlewares);
10764
+ } else {
10765
+ layoutServerHooks.push(null);
10766
+ layoutMiddlewares.push([]);
10767
+ }
10729
10768
  }
10730
10769
  const { middlewares, serverHook, dynamic, generateStaticParams } = loadServerHookForDir(pageDir);
10731
10770
  pageRoutes.push({
@@ -10741,6 +10780,8 @@ function loadRoutesFromManifest(projectRoot) {
10741
10780
  // Keep 'loader' field name for backward compatibility
10742
10781
  layoutServerHooks,
10743
10782
  // Server hooks for each layout (same order as layouts)
10783
+ layoutMiddlewares,
10784
+ // Middlewares for each layout (same order as layouts)
10744
10785
  dynamic: entry.dynamic ?? dynamic,
10745
10786
  generateStaticParams
10746
10787
  });
@@ -11269,9 +11310,16 @@ function loadNotFoundRouteFromFilesystem(appDir) {
11269
11310
  appDir
11270
11311
  );
11271
11312
  const layoutServerHooks = [];
11313
+ const layoutMiddlewares = [];
11272
11314
  for (const layoutFile of layoutFiles) {
11273
- const layoutServerHook = loadLayoutServerHook(layoutFile);
11274
- layoutServerHooks.push(layoutServerHook);
11315
+ const layoutHookData = loadLayoutServerHook(layoutFile);
11316
+ if (layoutHookData) {
11317
+ layoutServerHooks.push(layoutHookData.serverHook);
11318
+ layoutMiddlewares.push(layoutHookData.middlewares);
11319
+ } else {
11320
+ layoutServerHooks.push(null);
11321
+ layoutMiddlewares.push([]);
11322
+ }
11275
11323
  }
11276
11324
  const { middlewares, serverHook, dynamic, generateStaticParams } = loadServerHookForDir(notFoundDir);
11277
11325
  return {
@@ -11287,6 +11335,8 @@ function loadNotFoundRouteFromFilesystem(appDir) {
11287
11335
  // Keep 'loader' field name for backward compatibility
11288
11336
  layoutServerHooks,
11289
11337
  // Server hooks for each layout (same order as layouts)
11338
+ layoutMiddlewares,
11339
+ // Middlewares for each layout (same order as layouts)
11290
11340
  dynamic,
11291
11341
  generateStaticParams
11292
11342
  };
@@ -11318,9 +11368,16 @@ function loadErrorRouteFromFilesystem(appDir) {
11318
11368
  appDir
11319
11369
  );
11320
11370
  const layoutServerHooks = [];
11371
+ const layoutMiddlewares = [];
11321
11372
  for (const layoutFile of layoutFiles) {
11322
- const layoutServerHook = loadLayoutServerHook(layoutFile);
11323
- layoutServerHooks.push(layoutServerHook);
11373
+ const layoutHookData = loadLayoutServerHook(layoutFile);
11374
+ if (layoutHookData) {
11375
+ layoutServerHooks.push(layoutHookData.serverHook);
11376
+ layoutMiddlewares.push(layoutHookData.middlewares);
11377
+ } else {
11378
+ layoutServerHooks.push(null);
11379
+ layoutMiddlewares.push([]);
11380
+ }
11324
11381
  }
11325
11382
  const { middlewares, serverHook, dynamic, generateStaticParams } = loadServerHookForDir(appDir);
11326
11383
  return {
@@ -11336,6 +11393,8 @@ function loadErrorRouteFromFilesystem(appDir) {
11336
11393
  // Keep 'loader' field name for backward compatibility
11337
11394
  layoutServerHooks,
11338
11395
  // Server hooks for each layout (same order as layouts)
11396
+ layoutMiddlewares,
11397
+ // Middlewares for each layout (same order as layouts)
11339
11398
  dynamic,
11340
11399
  generateStaticParams
11341
11400
  };
@@ -12805,6 +12864,29 @@ async function handlePageRequestInternal(options) {
12805
12864
  if (!skipLayoutHooks && notFoundPage.layoutServerHooks && notFoundPage.layoutServerHooks.length > 0) {
12806
12865
  for (let i = 0; i < notFoundPage.layoutServerHooks.length; i++) {
12807
12866
  const layoutServerHook = notFoundPage.layoutServerHooks[i];
12867
+ const layoutMiddlewares = notFoundPage.layoutMiddlewares?.[i] || [];
12868
+ if (layoutMiddlewares.length > 0) {
12869
+ for (const mw of layoutMiddlewares) {
12870
+ try {
12871
+ await Promise.resolve(
12872
+ mw(ctx2, async () => {
12873
+ })
12874
+ );
12875
+ } catch (error) {
12876
+ const reqLogger2 = getRequestLogger(req);
12877
+ const layoutFile = notFoundPage.layoutFiles[i];
12878
+ const relativeLayoutPath = layoutFile ? path17.relative(projectRoot || process.cwd(), layoutFile) : "unknown";
12879
+ reqLogger2.error("Layout middleware failed for not-found page", error instanceof Error ? error : new Error(String(error)), {
12880
+ layoutIndex: i,
12881
+ layoutFile: relativeLayoutPath
12882
+ });
12883
+ throw error;
12884
+ }
12885
+ if (ctx2.res.headersSent) {
12886
+ return;
12887
+ }
12888
+ }
12889
+ }
12808
12890
  if (layoutServerHook) {
12809
12891
  try {
12810
12892
  const layoutResult = await layoutServerHook(ctx2);
@@ -12922,6 +13004,29 @@ async function handlePageRequestInternal(options) {
12922
13004
  if (!skipLayoutHooks && route.layoutServerHooks && route.layoutServerHooks.length > 0) {
12923
13005
  for (let i = 0; i < route.layoutServerHooks.length; i++) {
12924
13006
  const layoutServerHook = route.layoutServerHooks[i];
13007
+ const layoutMiddlewares = route.layoutMiddlewares?.[i] || [];
13008
+ if (layoutMiddlewares.length > 0) {
13009
+ for (const mw of layoutMiddlewares) {
13010
+ try {
13011
+ await Promise.resolve(
13012
+ mw(ctx, async () => {
13013
+ })
13014
+ );
13015
+ } catch (error) {
13016
+ const layoutFile = route.layoutFiles[i];
13017
+ const relativeLayoutPath = layoutFile ? path17.relative(projectRoot || process.cwd(), layoutFile) : "unknown";
13018
+ reqLogger.error("Layout middleware failed", error instanceof Error ? error : new Error(String(error)), {
13019
+ route: route.pattern,
13020
+ layoutIndex: i,
13021
+ layoutFile: relativeLayoutPath
13022
+ });
13023
+ throw error;
13024
+ }
13025
+ if (ctx.res.headersSent) {
13026
+ return;
13027
+ }
13028
+ }
13029
+ }
12925
13030
  if (layoutServerHook) {
12926
13031
  try {
12927
13032
  const layoutResult = await layoutServerHook(ctx);
@@ -13117,6 +13222,28 @@ async function renderErrorPageWithStream(errorPage, req, res, error, routeChunks
13117
13222
  if (!skipLayoutHooks && errorPage.layoutServerHooks && errorPage.layoutServerHooks.length > 0) {
13118
13223
  for (let i = 0; i < errorPage.layoutServerHooks.length; i++) {
13119
13224
  const layoutServerHook = errorPage.layoutServerHooks[i];
13225
+ const layoutMiddlewares = errorPage.layoutMiddlewares?.[i] || [];
13226
+ if (layoutMiddlewares.length > 0) {
13227
+ for (const mw of layoutMiddlewares) {
13228
+ try {
13229
+ await Promise.resolve(
13230
+ mw(ctx, async () => {
13231
+ })
13232
+ );
13233
+ } catch (error2) {
13234
+ const layoutFile = errorPage.layoutFiles[i];
13235
+ const relativeLayoutPath = layoutFile ? path17.relative(projectRoot || process.cwd(), layoutFile) : "unknown";
13236
+ reqLogger.error("Layout middleware failed for error page", error2 instanceof Error ? error2 : new Error(String(error2)), {
13237
+ layoutIndex: i,
13238
+ layoutFile: relativeLayoutPath
13239
+ });
13240
+ throw error2;
13241
+ }
13242
+ if (ctx.res.headersSent) {
13243
+ return;
13244
+ }
13245
+ }
13246
+ }
13120
13247
  if (layoutServerHook) {
13121
13248
  try {
13122
13249
  const layoutResult = await layoutServerHook(ctx);