vinext 0.0.25 → 0.0.26

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 (86) hide show
  1. package/README.md +6 -1
  2. package/dist/check.js +4 -4
  3. package/dist/check.js.map +1 -1
  4. package/dist/cli.js +32 -1
  5. package/dist/cli.js.map +1 -1
  6. package/dist/client/entry.js.map +1 -1
  7. package/dist/client/vinext-next-data.d.ts +22 -0
  8. package/dist/client/vinext-next-data.d.ts.map +1 -0
  9. package/dist/client/vinext-next-data.js +2 -0
  10. package/dist/client/vinext-next-data.js.map +1 -0
  11. package/dist/config/config-matchers.d.ts.map +1 -1
  12. package/dist/config/config-matchers.js +6 -2
  13. package/dist/config/config-matchers.js.map +1 -1
  14. package/dist/config/next-config.d.ts +31 -4
  15. package/dist/config/next-config.d.ts.map +1 -1
  16. package/dist/config/next-config.js +151 -13
  17. package/dist/config/next-config.js.map +1 -1
  18. package/dist/deploy.d.ts +11 -0
  19. package/dist/deploy.d.ts.map +1 -1
  20. package/dist/deploy.js +42 -24
  21. package/dist/deploy.js.map +1 -1
  22. package/dist/entries/app-browser-entry.d.ts +9 -0
  23. package/dist/entries/app-browser-entry.d.ts.map +1 -0
  24. package/dist/entries/app-browser-entry.js +340 -0
  25. package/dist/entries/app-browser-entry.js.map +1 -0
  26. package/dist/{server/app-dev-server.d.ts → entries/app-rsc-entry.d.ts} +4 -17
  27. package/dist/entries/app-rsc-entry.d.ts.map +1 -0
  28. package/dist/{server/app-dev-server.js → entries/app-rsc-entry.js} +360 -1205
  29. package/dist/entries/app-rsc-entry.js.map +1 -0
  30. package/dist/entries/app-ssr-entry.d.ts +8 -0
  31. package/dist/entries/app-ssr-entry.d.ts.map +1 -0
  32. package/dist/entries/app-ssr-entry.js +449 -0
  33. package/dist/entries/app-ssr-entry.js.map +1 -0
  34. package/dist/entries/pages-client-entry.d.ts +4 -0
  35. package/dist/entries/pages-client-entry.d.ts.map +1 -0
  36. package/dist/entries/pages-client-entry.js +94 -0
  37. package/dist/entries/pages-client-entry.js.map +1 -0
  38. package/dist/entries/pages-entry-helpers.d.ts +7 -0
  39. package/dist/entries/pages-entry-helpers.d.ts.map +1 -0
  40. package/dist/entries/pages-entry-helpers.js +18 -0
  41. package/dist/entries/pages-entry-helpers.js.map +1 -0
  42. package/dist/entries/pages-server-entry.d.ts +8 -0
  43. package/dist/entries/pages-server-entry.d.ts.map +1 -0
  44. package/dist/entries/pages-server-entry.js +993 -0
  45. package/dist/entries/pages-server-entry.js.map +1 -0
  46. package/dist/index.d.ts +1 -25
  47. package/dist/index.d.ts.map +1 -1
  48. package/dist/index.js +206 -1242
  49. package/dist/index.js.map +1 -1
  50. package/dist/server/instrumentation.d.ts +1 -1
  51. package/dist/server/instrumentation.js +1 -1
  52. package/dist/server/instrumentation.js.map +1 -1
  53. package/dist/server/middleware-codegen.d.ts +1 -1
  54. package/dist/server/middleware-codegen.js +1 -1
  55. package/dist/server/middleware-codegen.js.map +1 -1
  56. package/dist/server/prod-server.d.ts.map +1 -1
  57. package/dist/server/prod-server.js +18 -3
  58. package/dist/server/prod-server.js.map +1 -1
  59. package/dist/server/request-pipeline.d.ts +92 -0
  60. package/dist/server/request-pipeline.d.ts.map +1 -0
  61. package/dist/server/request-pipeline.js +202 -0
  62. package/dist/server/request-pipeline.js.map +1 -0
  63. package/dist/shims/constants.d.ts +120 -3
  64. package/dist/shims/constants.d.ts.map +1 -1
  65. package/dist/shims/constants.js +170 -3
  66. package/dist/shims/constants.js.map +1 -1
  67. package/dist/shims/headers.d.ts.map +1 -1
  68. package/dist/shims/headers.js +1 -0
  69. package/dist/shims/headers.js.map +1 -1
  70. package/dist/shims/link.d.ts.map +1 -1
  71. package/dist/shims/link.js +2 -2
  72. package/dist/shims/link.js.map +1 -1
  73. package/dist/shims/metadata.d.ts +7 -1
  74. package/dist/shims/metadata.d.ts.map +1 -1
  75. package/dist/shims/metadata.js +9 -3
  76. package/dist/shims/metadata.js.map +1 -1
  77. package/dist/shims/og.d.ts +6 -6
  78. package/dist/shims/og.js +6 -6
  79. package/dist/shims/og.js.map +1 -1
  80. package/dist/utils/project.d.ts +15 -0
  81. package/dist/utils/project.d.ts.map +1 -1
  82. package/dist/utils/project.js +48 -0
  83. package/dist/utils/project.js.map +1 -1
  84. package/package.json +1 -1
  85. package/dist/server/app-dev-server.d.ts.map +0 -1
  86. package/dist/server/app-dev-server.js.map +0 -1
@@ -1,16 +1,22 @@
1
1
  /**
2
- * App Router dev server handler.
2
+ * App Router RSC entry generator.
3
3
  *
4
- * This module generates virtual entry points for the RSC/SSR/browser
5
- * environments that @vitejs/plugin-rsc manages. The RSC entry does
6
- * route matching and renders the component tree, then delegates to
7
- * the SSR entry for HTML generation.
4
+ * Generates the virtual RSC entry module for the App Router.
5
+ * The RSC entry does route matching and renders the component tree,
6
+ * then delegates to the SSR entry for HTML generation.
7
+ *
8
+ * Previously housed in server/app-dev-server.ts.
8
9
  */
9
10
  import fs from "node:fs";
10
11
  import { fileURLToPath } from "node:url";
11
- import { generateDevOriginCheckCode } from "./dev-origin-check.js";
12
- import { generateSafeRegExpCode, generateMiddlewareMatcherCode, generateNormalizePathCode } from "./middleware-codegen.js";
13
- import { isProxyFile } from "./middleware.js";
12
+ import { generateDevOriginCheckCode } from "../server/dev-origin-check.js";
13
+ import { generateSafeRegExpCode, generateMiddlewareMatcherCode, generateNormalizePathCode } from "../server/middleware-codegen.js";
14
+ import { isProxyFile } from "../server/middleware.js";
15
+ // Pre-computed absolute paths for generated-code imports. The virtual RSC
16
+ // entry can't use relative imports (it has no real file location), so we
17
+ // resolve these at code-generation time and embed them as absolute paths.
18
+ const configMatchersPath = fileURLToPath(new URL("../config/config-matchers.js", import.meta.url)).replace(/\\/g, "/");
19
+ const requestPipelinePath = fileURLToPath(new URL("../server/request-pipeline.js", import.meta.url)).replace(/\\/g, "/");
14
20
  /**
15
21
  * Generate the virtual RSC entry module.
16
22
  *
@@ -25,6 +31,7 @@ export function generateRscEntry(appDir, routes, middlewarePath, metadataRoutes,
25
31
  const rewrites = config?.rewrites ?? { beforeFiles: [], afterFiles: [], fallback: [] };
26
32
  const headers = config?.headers ?? [];
27
33
  const allowedOrigins = config?.allowedOrigins ?? [];
34
+ const bodySizeLimit = config?.bodySizeLimit ?? 1 * 1024 * 1024;
28
35
  // Build import map for all page and layout files
29
36
  const imports = [];
30
37
  const importMap = new Map();
@@ -205,7 +212,9 @@ import { LayoutSegmentProvider } from "vinext/layout-segment-context";
205
212
  import { MetadataHead, mergeMetadata, resolveModuleMetadata, ViewportHead, mergeViewport, resolveModuleViewport } from "vinext/metadata";
206
213
  ${middlewarePath ? `import * as middlewareModule from ${JSON.stringify(middlewarePath.replace(/\\/g, "/"))};` : ""}
207
214
  ${instrumentationPath ? `import * as _instrumentation from ${JSON.stringify(instrumentationPath.replace(/\\/g, "/"))};` : ""}
208
- ${effectiveMetaRoutes.length > 0 ? `import { sitemapToXml, robotsToText, manifestToJson } from ${JSON.stringify(fileURLToPath(new URL("./metadata-routes.js", import.meta.url)).replace(/\\/g, "/"))};` : ""}
215
+ ${effectiveMetaRoutes.length > 0 ? `import { sitemapToXml, robotsToText, manifestToJson } from ${JSON.stringify(fileURLToPath(new URL("../server/metadata-routes.js", import.meta.url)).replace(/\\/g, "/"))};` : ""}
216
+ import { requestContextFromRequest, matchRedirect, matchRewrite, matchHeaders, isExternalUrl, proxyExternalRequest, sanitizeDestination } from ${JSON.stringify(configMatchersPath)};
217
+ import { validateCsrfOrigin, validateImageUrl, guardProtocolRelativeUrl, stripBasePath, normalizeTrailingSlash, processMiddlewareHeaders } from ${JSON.stringify(requestPipelinePath)};
209
218
  import { _consumeRequestScopedCacheLife, _runWithCacheState } from "next/cache";
210
219
  import { runWithFetchCache } from "vinext/fetch-cache";
211
220
  import { runWithPrivateCache as _runWithPrivateCache } from "vinext/cache-runtime";
@@ -339,7 +348,7 @@ function __sanitizeErrorForClient(error) {
339
348
  // thrown during RSC streaming (e.g. inside Suspense boundaries).
340
349
  // For non-navigation errors in production, generates a digest hash so the
341
350
  // error can be correlated with server logs without leaking details.
342
- function rscOnError(error) {
351
+ function rscOnError(error, requestInfo, errorContext) {
343
352
  if (error && typeof error === "object" && "digest" in error) {
344
353
  return String(error.digest);
345
354
  }
@@ -385,6 +394,16 @@ function rscOnError(error) {
385
394
  return undefined;
386
395
  }
387
396
 
397
+ if (requestInfo && errorContext && error) {
398
+ _reportRequestError(
399
+ error instanceof Error ? error : new Error(String(error)),
400
+ requestInfo,
401
+ errorContext,
402
+ ).catch((reportErr) => {
403
+ console.error("[vinext] Failed to report render error:", reportErr);
404
+ });
405
+ }
406
+
388
407
  // In production, generate a digest hash for non-navigation errors
389
408
  if (process.env.NODE_ENV === "production" && error) {
390
409
  const msg = error instanceof Error ? error.message : String(error);
@@ -394,23 +413,53 @@ function rscOnError(error) {
394
413
  return undefined;
395
414
  }
396
415
 
416
+ function createRscOnErrorHandler(request, pathname, routePath) {
417
+ const requestInfo = {
418
+ path: pathname,
419
+ method: request.method,
420
+ headers: Object.fromEntries(request.headers.entries()),
421
+ };
422
+ const errorContext = {
423
+ routerKind: "App Router",
424
+ routePath: routePath || pathname,
425
+ routeType: "render",
426
+ };
427
+ return function(error) {
428
+ return rscOnError(error, requestInfo, errorContext);
429
+ };
430
+ }
431
+
397
432
  ${imports.join("\n")}
398
433
 
399
- ${instrumentationPath ? `// Run instrumentation register() once at module evaluation time — before any
400
- // requests are handled. This runs inside the Worker process (or RSC environment),
401
- // which is exactly where request handling happens. Matches Next.js semantics:
402
- // register() is called once on startup in the process that handles requests.
403
- if (typeof _instrumentation.register === "function") {
404
- await _instrumentation.register();
405
- }
406
- // Store the onRequestError handler on globalThis so it is visible to
407
- // reportRequestError() (imported as _reportRequestError above) regardless
408
- // of which Vite environment module graph it is called from. With
409
- // @vitejs/plugin-rsc the RSC and SSR environments run in the same Node.js
410
- // process and share globalThis. With @cloudflare/vite-plugin everything
411
- // runs inside the Worker so globalThis is the Worker's global — also correct.
412
- if (typeof _instrumentation.onRequestError === "function") {
413
- globalThis.__VINEXT_onRequestErrorHandler__ = _instrumentation.onRequestError;
434
+ ${instrumentationPath ? `// Run instrumentation register() exactly once, lazily on the first request.
435
+ // Previously this was a top-level await, which blocked the entire module graph
436
+ // from finishing initialization until register() resolved adding that latency
437
+ // to every cold start. Moving it here preserves the "runs before any request is
438
+ // handled" guarantee while not blocking V8 isolate initialization.
439
+ // On Cloudflare Workers, module evaluation happens synchronously in the isolate
440
+ // startup phase; a top-level await extends that phase and increases cold-start
441
+ // wall time for all requests, not just the first.
442
+ let __instrumentationInitialized = false;
443
+ let __instrumentationInitPromise = null;
444
+ async function __ensureInstrumentation() {
445
+ if (__instrumentationInitialized) return;
446
+ if (__instrumentationInitPromise) return __instrumentationInitPromise;
447
+ __instrumentationInitPromise = (async () => {
448
+ if (typeof _instrumentation.register === "function") {
449
+ await _instrumentation.register();
450
+ }
451
+ // Store the onRequestError handler on globalThis so it is visible to
452
+ // reportRequestError() (imported as _reportRequestError above) regardless
453
+ // of which Vite environment module graph it is called from. With
454
+ // @vitejs/plugin-rsc the RSC and SSR environments run in the same Node.js
455
+ // process and share globalThis. With @cloudflare/vite-plugin everything
456
+ // runs inside the Worker so globalThis is the Worker's global — also correct.
457
+ if (typeof _instrumentation.onRequestError === "function") {
458
+ globalThis.__VINEXT_onRequestErrorHandler__ = _instrumentation.onRequestError;
459
+ }
460
+ __instrumentationInitialized = true;
461
+ })();
462
+ return __instrumentationInitPromise;
414
463
  }` : ""}
415
464
 
416
465
  const routes = [
@@ -452,16 +501,26 @@ async function renderHTTPAccessFallbackPage(route, statusCode, isRscRequest, req
452
501
 
453
502
  // Resolve metadata and viewport from parent layouts so that not-found/error
454
503
  // pages inherit title, description, OG tags etc. — matching Next.js behavior.
455
- const metadataList = [];
456
- const viewportList = [];
457
- for (const layoutMod of layouts) {
458
- if (layoutMod) {
459
- const meta = await resolveModuleMetadata(layoutMod);
460
- if (meta) metadataList.push(meta);
461
- const vp = await resolveModuleViewport(layoutMod);
462
- if (vp) viewportList.push(vp);
463
- }
504
+ // Build the serial parent chain for layout metadata (same as buildPageElement).
505
+ const _filteredLayouts = layouts.filter(Boolean);
506
+ const _fallbackParams = opts?.matchedParams ?? route?.params ?? {};
507
+ const _layoutMetaPromises = [];
508
+ let _accumulatedMeta = Promise.resolve({});
509
+ for (let _i = 0; _i < _filteredLayouts.length; _i++) {
510
+ const _parentForLayout = _accumulatedMeta;
511
+ const _metaP = resolveModuleMetadata(_filteredLayouts[_i], _fallbackParams, undefined, _parentForLayout)
512
+ .catch((err) => { console.error("[vinext] Layout generateMetadata() failed:", err); return null; });
513
+ _layoutMetaPromises.push(_metaP);
514
+ _accumulatedMeta = _metaP.then(async (_r) =>
515
+ _r ? mergeMetadata([await _parentForLayout, _r]) : await _parentForLayout
516
+ );
464
517
  }
518
+ const [_metaResults, _vpResults] = await Promise.all([
519
+ Promise.all(_layoutMetaPromises),
520
+ Promise.all(_filteredLayouts.map((mod) => resolveModuleViewport(mod, _fallbackParams).catch((err) => { console.error("[vinext] Layout generateViewport() failed:", err); return null; }))),
521
+ ]);
522
+ const metadataList = _metaResults.filter(Boolean);
523
+ const viewportList = _vpResults.filter(Boolean);
465
524
  const resolvedMetadata = metadataList.length > 0 ? mergeMetadata(metadataList) : null;
466
525
  const resolvedViewport = mergeViewport(viewportList);
467
526
 
@@ -506,9 +565,19 @@ async function renderHTTPAccessFallbackPage(route, statusCode, isRscRequest, req
506
565
  });
507
566
  }
508
567
  ` : ""}
509
- const rscStream = renderToReadableStream(element, { onError: rscOnError });
510
- setHeadersContext(null);
511
- setNavigationContext(null);
568
+ const _pathname = new URL(request.url).pathname;
569
+ const onRenderError = createRscOnErrorHandler(
570
+ request,
571
+ _pathname,
572
+ route?.pattern ?? _pathname,
573
+ );
574
+ const rscStream = renderToReadableStream(element, { onError: onRenderError });
575
+ // Do NOT clear context here — the RSC stream is consumed lazily by the client.
576
+ // Clearing context now would cause async server components (e.g. NextIntlClientProviderServer)
577
+ // that run during stream consumption to see null headers/navigation context and throw,
578
+ // resulting in missing provider context on the client (e.g. next-intl useTranslations fails
579
+ // with "context from NextIntlClientProvider was not found").
580
+ // Context is cleared naturally when the ALS scope from runWithHeadersContext unwinds.
512
581
  return new Response(rscStream, {
513
582
  status: statusCode,
514
583
  headers: { "Content-Type": "text/x-component; charset=utf-8", "Vary": "RSC, Accept" },
@@ -524,7 +593,13 @@ async function renderHTTPAccessFallbackPage(route, statusCode, isRscRequest, req
524
593
  element = createElement(LayoutComponent, { children: element, params: _asyncFallbackParamsHtml });
525
594
  }
526
595
  }
527
- const rscStream = renderToReadableStream(element, { onError: rscOnError });
596
+ const _pathname = new URL(request.url).pathname;
597
+ const onRenderError = createRscOnErrorHandler(
598
+ request,
599
+ _pathname,
600
+ route?.pattern ?? _pathname,
601
+ );
602
+ const rscStream = renderToReadableStream(element, { onError: onRenderError });
528
603
  // Collect font data from RSC environment
529
604
  const fontData = {
530
605
  links: _getSSRFontLinks(),
@@ -560,6 +635,7 @@ async function renderErrorBoundaryPage(route, error, isRscRequest, request, matc
560
635
  // Resolve the error boundary component: leaf error.tsx first, then walk per-layout
561
636
  // errors from innermost to outermost (matching ancestor inheritance), then global-error.tsx.
562
637
  let ErrorComponent = route?.error?.default ?? null;
638
+ let _isGlobalError = false;
563
639
  if (!ErrorComponent && route?.errors) {
564
640
  for (let i = route.errors.length - 1; i >= 0; i--) {
565
641
  if (route.errors[i]?.default) {
@@ -568,7 +644,12 @@ async function renderErrorBoundaryPage(route, error, isRscRequest, request, matc
568
644
  }
569
645
  }
570
646
  }
571
- ErrorComponent = ErrorComponent${globalErrorVar ? ` ?? ${globalErrorVar}?.default` : ""};
647
+ ${globalErrorVar ? `
648
+ if (!ErrorComponent) {
649
+ ErrorComponent = ${globalErrorVar}?.default ?? null;
650
+ _isGlobalError = !!ErrorComponent;
651
+ }
652
+ ` : ""}
572
653
  if (!ErrorComponent) return null;
573
654
 
574
655
  const rawError = error instanceof Error ? error : new Error(String(error));
@@ -583,52 +664,74 @@ async function renderErrorBoundaryPage(route, error, isRscRequest, request, matc
583
664
  let element = createElement(ErrorComponent, {
584
665
  error: errorObj,
585
666
  });
586
- const layouts = route?.layouts ?? rootLayouts;
587
- if (isRscRequest) {
588
- // For RSC requests (client-side navigation), wrap with the same component
589
- // wrappers that buildPageElement() uses (LayoutSegmentProvider, GlobalErrorBoundary).
590
- // This ensures React can reconcile the tree without destroying the DOM.
591
- // Same rationale as renderHTTPAccessFallbackPage — see comment there.
592
- const _errTreePositions = route?.layoutTreePositions;
593
- const _errRouteSegs = route?.routeSegments || [];
594
- const _errParams = matchedParams ?? route?.params ?? {};
595
- const _asyncErrParams = makeThenableParams(_errParams);
596
- for (let i = layouts.length - 1; i >= 0; i--) {
597
- const LayoutComponent = layouts[i]?.default;
598
- if (LayoutComponent) {
599
- element = createElement(LayoutComponent, { children: element, params: _asyncErrParams });
600
- const _etp = _errTreePositions ? _errTreePositions[i] : 0;
601
- const _ecs = __resolveChildSegments(_errRouteSegs, _etp, _errParams);
602
- element = createElement(LayoutSegmentProvider, { childSegments: _ecs }, element);
667
+
668
+ // global-error.tsx provides its own <html> and <body> (it replaces the root
669
+ // layout). Skip layout wrapping when rendering it to avoid double <html> tags.
670
+ if (!_isGlobalError) {
671
+ const layouts = route?.layouts ?? rootLayouts;
672
+ if (isRscRequest) {
673
+ // For RSC requests (client-side navigation), wrap with the same component
674
+ // wrappers that buildPageElement() uses (LayoutSegmentProvider, GlobalErrorBoundary).
675
+ // This ensures React can reconcile the tree without destroying the DOM.
676
+ // Same rationale as renderHTTPAccessFallbackPage — see comment there.
677
+ const _errTreePositions = route?.layoutTreePositions;
678
+ const _errRouteSegs = route?.routeSegments || [];
679
+ const _errParams = matchedParams ?? route?.params ?? {};
680
+ const _asyncErrParams = makeThenableParams(_errParams);
681
+ for (let i = layouts.length - 1; i >= 0; i--) {
682
+ const LayoutComponent = layouts[i]?.default;
683
+ if (LayoutComponent) {
684
+ element = createElement(LayoutComponent, { children: element, params: _asyncErrParams });
685
+ const _etp = _errTreePositions ? _errTreePositions[i] : 0;
686
+ const _ecs = __resolveChildSegments(_errRouteSegs, _etp, _errParams);
687
+ element = createElement(LayoutSegmentProvider, { childSegments: _ecs }, element);
688
+ }
689
+ }
690
+ ${globalErrorVar ? `
691
+ const _ErrGlobalComponent = ${globalErrorVar}.default;
692
+ if (_ErrGlobalComponent) {
693
+ element = createElement(ErrorBoundary, {
694
+ fallback: _ErrGlobalComponent,
695
+ children: element,
696
+ });
697
+ }
698
+ ` : ""}
699
+ } else {
700
+ // For HTML (full page load) responses, wrap with layouts only.
701
+ const _errParamsHtml = matchedParams ?? route?.params ?? {};
702
+ const _asyncErrParamsHtml = makeThenableParams(_errParamsHtml);
703
+ for (let i = layouts.length - 1; i >= 0; i--) {
704
+ const LayoutComponent = layouts[i]?.default;
705
+ if (LayoutComponent) {
706
+ element = createElement(LayoutComponent, { children: element, params: _asyncErrParamsHtml });
707
+ }
603
708
  }
604
709
  }
605
- ${globalErrorVar ? `
606
- const _ErrGlobalComponent = ${globalErrorVar}.default;
607
- if (_ErrGlobalComponent) {
608
- element = createElement(ErrorBoundary, {
609
- fallback: _ErrGlobalComponent,
610
- children: element,
611
- });
612
- }
613
- ` : ""}
614
- const rscStream = renderToReadableStream(element, { onError: rscOnError });
615
- setHeadersContext(null);
616
- setNavigationContext(null);
710
+ }
711
+
712
+ const _pathname = new URL(request.url).pathname;
713
+ const onRenderError = createRscOnErrorHandler(
714
+ request,
715
+ _pathname,
716
+ route?.pattern ?? _pathname,
717
+ );
718
+
719
+ if (isRscRequest) {
720
+ const rscStream = renderToReadableStream(element, { onError: onRenderError });
721
+ // Do NOT clear context here — the RSC stream is consumed lazily by the client.
722
+ // Clearing context now would cause async server components (e.g. NextIntlClientProviderServer)
723
+ // that run during stream consumption to see null headers/navigation context and throw,
724
+ // resulting in missing provider context on the client (e.g. next-intl useTranslations fails
725
+ // with "context from NextIntlClientProvider was not found").
726
+ // Context is cleared naturally when the ALS scope from runWithHeadersContext unwinds.
617
727
  return new Response(rscStream, {
618
728
  status: 200,
619
729
  headers: { "Content-Type": "text/x-component; charset=utf-8", "Vary": "RSC, Accept" },
620
730
  });
621
731
  }
622
- // For HTML (full page load) responses, wrap with layouts only.
623
- const _errParamsHtml = matchedParams ?? route?.params ?? {};
624
- const _asyncErrParamsHtml = makeThenableParams(_errParamsHtml);
625
- for (let i = layouts.length - 1; i >= 0; i--) {
626
- const LayoutComponent = layouts[i]?.default;
627
- if (LayoutComponent) {
628
- element = createElement(LayoutComponent, { children: element, params: _asyncErrParamsHtml });
629
- }
630
- }
631
- const rscStream = renderToReadableStream(element, { onError: rscOnError });
732
+
733
+ // HTML (full page load) response render through RSC → SSR pipeline
734
+ const rscStream = renderToReadableStream(element, { onError: onRenderError });
632
735
  // Collect font data from RSC environment so error pages include font styles
633
736
  const fontData = {
634
737
  links: _getSSRFontLinks(),
@@ -730,23 +833,79 @@ async function buildPageElement(route, params, opts, searchParams) {
730
833
  return createElement("div", null, "Page has no default export");
731
834
  }
732
835
 
733
- // Resolve metadata and viewport from layouts and page
734
- const metadataList = [];
735
- const viewportList = [];
736
- for (const layoutMod of route.layouts) {
737
- if (layoutMod) {
738
- const meta = await resolveModuleMetadata(layoutMod, params);
739
- if (meta) metadataList.push(meta);
740
- const vp = await resolveModuleViewport(layoutMod, params);
741
- if (vp) viewportList.push(vp);
742
- }
836
+ // Resolve metadata and viewport from layouts and page.
837
+ //
838
+ // generateMetadata() accepts a "parent" (Promise of ResolvedMetadata) as its
839
+ // second argument (Next.js 13+). The parent resolves to the accumulated
840
+ // merged metadata of all ancestor segments, enabling patterns like:
841
+ //
842
+ // const previousImages = (await parent).openGraph?.images ?? []
843
+ // return { openGraph: { images: ['/new-image.jpg', ...previousImages] } }
844
+ //
845
+ // Next.js uses an eager-execution-with-serial-resolution approach:
846
+ // all generateMetadata() calls are kicked off concurrently, but each
847
+ // segment's "parent" promise resolves only after the preceding segment's
848
+ // metadata is resolved and merged. This preserves concurrency for I/O-bound
849
+ // work while guaranteeing that parent data is available when needed.
850
+ //
851
+ // We build a chain: layoutParentPromises[0] = Promise.resolve({}) (no parent
852
+ // for root layout), layoutParentPromises[i+1] resolves to merge(layouts[0..i]),
853
+ // and pageParentPromise resolves to merge(all layouts).
854
+ //
855
+ // IMPORTANT: Layout metadata errors are swallowed (.catch(() => null)) because
856
+ // a layout's generateMetadata() failing should not crash the page.
857
+ // Page metadata errors are NOT swallowed — if the page's generateMetadata()
858
+ // throws, the error propagates out of buildPageElement() so the caller can
859
+ // route it to the nearest error.tsx boundary (or global-error.tsx).
860
+ const layoutMods = route.layouts.filter(Boolean);
861
+
862
+ // Build the parent promise chain and kick off metadata resolution in one pass.
863
+ // Each layout module is called exactly once. layoutMetaPromises[i] is the
864
+ // promise for layout[i]'s own metadata result.
865
+ //
866
+ // All calls are kicked off immediately (concurrent I/O), but each layout's
867
+ // "parent" promise only resolves after the preceding layout's metadata is done.
868
+ const layoutMetaPromises = [];
869
+ let accumulatedMetaPromise = Promise.resolve({});
870
+ for (let i = 0; i < layoutMods.length; i++) {
871
+ const parentForThisLayout = accumulatedMetaPromise;
872
+ // Kick off this layout's metadata resolution now (concurrent with others).
873
+ const metaPromise = resolveModuleMetadata(layoutMods[i], params, undefined, parentForThisLayout)
874
+ .catch((err) => { console.error("[vinext] Layout generateMetadata() failed:", err); return null; });
875
+ layoutMetaPromises.push(metaPromise);
876
+ // Advance accumulator: resolves to merged(layouts[0..i]) once layout[i] is done.
877
+ accumulatedMetaPromise = metaPromise.then(async (result) =>
878
+ result ? mergeMetadata([await parentForThisLayout, result]) : await parentForThisLayout
879
+ );
743
880
  }
744
- if (route.page) {
745
- const pageMeta = await resolveModuleMetadata(route.page, params);
746
- if (pageMeta) metadataList.push(pageMeta);
747
- const pageVp = await resolveModuleViewport(route.page, params);
748
- if (pageVp) viewportList.push(pageVp);
881
+ // Page's parent is the fully-accumulated layout metadata.
882
+ const pageParentPromise = accumulatedMetaPromise;
883
+
884
+ // Convert URLSearchParams plain object so we can pass it to
885
+ // resolveModuleMetadata (which expects Record<string, string | string[]>).
886
+ // This same object is reused for pageProps.searchParams below.
887
+ const spObj = {};
888
+ let hasSearchParams = false;
889
+ if (searchParams && searchParams.forEach) {
890
+ searchParams.forEach(function(v, k) {
891
+ hasSearchParams = true;
892
+ if (k in spObj) {
893
+ spObj[k] = Array.isArray(spObj[k]) ? spObj[k].concat(v) : [spObj[k], v];
894
+ } else {
895
+ spObj[k] = v;
896
+ }
897
+ });
749
898
  }
899
+
900
+ const [layoutMetaResults, layoutVpResults, pageMeta, pageVp] = await Promise.all([
901
+ Promise.all(layoutMetaPromises),
902
+ Promise.all(layoutMods.map((mod) => resolveModuleViewport(mod, params).catch((err) => { console.error("[vinext] Layout generateViewport() failed:", err); return null; }))),
903
+ route.page ? resolveModuleMetadata(route.page, params, spObj, pageParentPromise) : Promise.resolve(null),
904
+ route.page ? resolveModuleViewport(route.page, params) : Promise.resolve(null),
905
+ ]);
906
+
907
+ const metadataList = [...layoutMetaResults.filter(Boolean), ...(pageMeta ? [pageMeta] : [])];
908
+ const viewportList = [...layoutVpResults.filter(Boolean), ...(pageVp ? [pageVp] : [])];
750
909
  const resolvedMetadata = metadataList.length > 0 ? mergeMetadata(metadataList) : null;
751
910
  const resolvedViewport = mergeViewport(viewportList);
752
911
 
@@ -757,17 +916,10 @@ async function buildPageElement(route, params, opts, searchParams) {
757
916
  const asyncParams = makeThenableParams(params);
758
917
  const pageProps = { params: asyncParams };
759
918
  if (searchParams) {
760
- const spObj = {};
761
- let hasSearchParams = false;
762
- if (searchParams.forEach) searchParams.forEach(function(v, k) {
763
- hasSearchParams = true;
764
- if (k in spObj) {
765
- // Multi-value: promote to array (Next.js returns string[] for duplicate keys)
766
- spObj[k] = Array.isArray(spObj[k]) ? spObj[k].concat(v) : [spObj[k], v];
767
- } else {
768
- spObj[k] = v;
769
- }
770
- });
919
+ // Always provide searchParams prop when the URL object is available, even
920
+ // when the query string is empty -- pages that do "await searchParams" need
921
+ // it to be a thenable rather than undefined.
922
+ pageProps.searchParams = makeThenableParams(spObj);
771
923
  // If the URL has query parameters, mark the page as dynamic.
772
924
  // In Next.js, only accessing the searchParams prop signals dynamic usage,
773
925
  // but a Proxy-based approach doesn't work here because React's RSC debug
@@ -777,7 +929,6 @@ async function buildPageElement(route, params, opts, searchParams) {
777
929
  // approximation: pages with query params in the URL are almost always
778
930
  // dynamic, and this avoids false positives from React internals.
779
931
  if (hasSearchParams) markDynamicUsage();
780
- pageProps.searchParams = makeThenableParams(spObj);
781
932
  }
782
933
  let element = createElement(PageComponent, pageProps);
783
934
 
@@ -946,7 +1097,14 @@ async function buildPageElement(route, params, opts, searchParams) {
946
1097
  }
947
1098
 
948
1099
  // Wrap with global error boundary if app/global-error.tsx exists.
949
- // This catches errors in the root layout itself.
1100
+ // This must be present in both HTML and RSC paths so the component tree
1101
+ // structure matches — otherwise React reconciliation on client-side navigation
1102
+ // would see a mismatched tree and destroy/recreate the DOM.
1103
+ //
1104
+ // For RSC requests (client-side nav), this provides error recovery on the client.
1105
+ // For HTML requests (initial page load), the ErrorBoundary catches during SSR
1106
+ // but produces double <html>/<body> (root layout + global-error). The request
1107
+ // handler detects this via the rscOnError flag and re-renders without layouts.
950
1108
  ${globalErrorVar ? `
951
1109
  const GlobalErrorComponent = ${globalErrorVar}.default;
952
1110
  if (GlobalErrorComponent) {
@@ -971,168 +1129,18 @@ const __allowedOrigins = ${JSON.stringify(allowedOrigins)};
971
1129
 
972
1130
  ${generateDevOriginCheckCode(config?.allowedDevOrigins)}
973
1131
 
974
- // ── CSRF origin validation for server actions ───────────────────────────
975
- // Matches Next.js behavior: compare the Origin header against the Host header.
976
- // If they don't match, the request is rejected with 403 unless the origin is
977
- // in the allowedOrigins list (from experimental.serverActions.allowedOrigins).
978
- function __isOriginAllowed(origin, allowed) {
979
- for (const pattern of allowed) {
980
- if (pattern.startsWith("*.")) {
981
- // Wildcard: *.example.com matches sub.example.com, a.b.example.com
982
- const suffix = pattern.slice(1); // ".example.com"
983
- if (origin === pattern.slice(2) || origin.endsWith(suffix)) return true;
984
- } else if (origin === pattern) {
985
- return true;
986
- }
987
- }
988
- return false;
989
- }
990
-
991
- function __validateCsrfOrigin(request) {
992
- const originHeader = request.headers.get("origin");
993
- // If there's no Origin header, allow the request — same-origin requests
994
- // from non-fetch navigations (e.g. SSR) may lack an Origin header.
995
- // The x-rsc-action custom header already provides protection against simple
996
- // form-based CSRF since custom headers can't be set by cross-origin forms.
997
- if (!originHeader || originHeader === "null") return null;
998
-
999
- let originHost;
1000
- try {
1001
- originHost = new URL(originHeader).host.toLowerCase();
1002
- } catch {
1003
- return new Response("Forbidden", { status: 403, headers: { "Content-Type": "text/plain" } });
1004
- }
1005
-
1006
- // Only use the Host header for origin comparison — never trust
1007
- // X-Forwarded-Host here, since it can be freely set by the client
1008
- // and would allow the check to be bypassed if it matched a spoofed
1009
- // Origin. The prod server's resolveHost() handles trusted proxy
1010
- // scenarios separately.
1011
- const hostHeader = (
1012
- request.headers.get("host") ||
1013
- ""
1014
- ).split(",")[0].trim().toLowerCase();
1015
-
1016
- if (!hostHeader) return null;
1017
-
1018
- // Same origin — allow
1019
- if (originHost === hostHeader) return null;
1020
-
1021
- // Check allowedOrigins from next.config.js
1022
- if (__allowedOrigins.length > 0 && __isOriginAllowed(originHost, __allowedOrigins)) return null;
1023
-
1024
- console.warn(
1025
- \`[vinext] CSRF origin mismatch: origin "\${originHost}" does not match host "\${hostHeader}". Blocking server action request.\`
1026
- );
1027
- return new Response("Forbidden", { status: 403, headers: { "Content-Type": "text/plain" } });
1028
- }
1029
-
1030
- // ── ReDoS-safe regex compilation ────────────────────────────────────────
1132
+ // ── ReDoS-safe regex compilation (still needed for middleware matching) ──
1031
1133
  ${generateSafeRegExpCode("modern")}
1032
1134
 
1033
1135
  // ── Path normalization ──────────────────────────────────────────────────
1034
1136
  ${generateNormalizePathCode("modern")}
1035
1137
 
1036
- // ── Config pattern matching (redirects, rewrites, headers) ──────────────
1037
- function __matchConfigPattern(pathname, pattern) {
1038
- if (pattern.includes("(") || pattern.includes("\\\\") || /:[\\w-]+[*+][^/]/.test(pattern) || /:[\\w-]+\\./.test(pattern)) {
1039
- try {
1040
- const paramNames = [];
1041
- const regexStr = pattern
1042
- .replace(/\\./g, "\\\\.")
1043
- .replace(/:([\\w-]+)\\*(?:\\(([^)]+)\\))?/g, (_, name, c) => { paramNames.push(name); return c ? "(" + c + ")" : "(.*)"; })
1044
- .replace(/:([\\w-]+)\\+(?:\\(([^)]+)\\))?/g, (_, name, c) => { paramNames.push(name); return c ? "(" + c + ")" : "(.+)"; })
1045
- .replace(/:([\\w-]+)\\(([^)]+)\\)/g, (_, name, c) => { paramNames.push(name); return "(" + c + ")"; })
1046
- .replace(/:([\\w-]+)/g, (_, name) => { paramNames.push(name); return "([^/]+)"; });
1047
- const re = __safeRegExp("^" + regexStr + "$");
1048
- if (!re) return null;
1049
- const match = re.exec(pathname);
1050
- if (!match) return null;
1051
- const params = Object.create(null);
1052
- for (let i = 0; i < paramNames.length; i++) params[paramNames[i]] = match[i + 1] || "";
1053
- return params;
1054
- } catch { /* fall through */ }
1055
- }
1056
- const catchAllMatch = pattern.match(/:([\\w-]+)(\\*|\\+)$/);
1057
- if (catchAllMatch) {
1058
- const prefix = pattern.slice(0, pattern.lastIndexOf(":"));
1059
- const paramName = catchAllMatch[1];
1060
- const isPlus = catchAllMatch[2] === "+";
1061
- if (!pathname.startsWith(prefix.replace(/\\/$/, ""))) return null;
1062
- const rest = pathname.slice(prefix.replace(/\\/$/, "").length);
1063
- if (isPlus && (!rest || rest === "/")) return null;
1064
- let restValue = rest.startsWith("/") ? rest.slice(1) : rest;
1065
- // NOTE: Do NOT decodeURIComponent here. The pathname is already decoded at
1066
- // the request entry point. Decoding again would produce incorrect param values.
1067
- return { [paramName]: restValue };
1068
- }
1069
- const parts = pattern.split("/");
1070
- const pathParts = pathname.split("/");
1071
- if (parts.length !== pathParts.length) return null;
1072
- const params = Object.create(null);
1073
- for (let i = 0; i < parts.length; i++) {
1074
- if (parts[i].startsWith(":")) params[parts[i].slice(1)] = pathParts[i];
1075
- else if (parts[i] !== pathParts[i]) return null;
1076
- }
1077
- return params;
1078
- }
1079
-
1080
- function __parseCookies(cookieHeader) {
1081
- if (!cookieHeader) return {};
1082
- const cookies = {};
1083
- for (const part of cookieHeader.split(";")) {
1084
- const eq = part.indexOf("=");
1085
- if (eq === -1) continue;
1086
- const key = part.slice(0, eq).trim();
1087
- const value = part.slice(eq + 1).trim();
1088
- if (key) cookies[key] = value;
1089
- }
1090
- return cookies;
1091
- }
1092
-
1093
- function __checkSingleCondition(condition, ctx) {
1094
- switch (condition.type) {
1095
- case "header": {
1096
- const v = ctx.headers.get(condition.key);
1097
- if (v === null) return false;
1098
- if (condition.value !== undefined) { const re = __safeRegExp(condition.value); return re ? re.test(v) : v === condition.value; }
1099
- return true;
1100
- }
1101
- case "cookie": {
1102
- const v = ctx.cookies[condition.key];
1103
- if (v === undefined) return false;
1104
- if (condition.value !== undefined) { const re = __safeRegExp(condition.value); return re ? re.test(v) : v === condition.value; }
1105
- return true;
1106
- }
1107
- case "query": {
1108
- const v = ctx.query.get(condition.key);
1109
- if (v === null) return false;
1110
- if (condition.value !== undefined) { const re = __safeRegExp(condition.value); return re ? re.test(v) : v === condition.value; }
1111
- return true;
1112
- }
1113
- case "host": {
1114
- if (condition.value !== undefined) { const re = __safeRegExp(condition.value); return re ? re.test(ctx.host) : ctx.host === condition.value; }
1115
- return ctx.host === condition.key;
1116
- }
1117
- default: return false;
1118
- }
1119
- }
1120
-
1121
- function __checkHasConditions(has, missing, ctx) {
1122
- if (has) { for (const c of has) { if (!__checkSingleCondition(c, ctx)) return false; } }
1123
- if (missing) { for (const c of missing) { if (__checkSingleCondition(c, ctx)) return false; } }
1124
- return true;
1125
- }
1126
-
1127
- function __buildRequestContext(request) {
1128
- const url = new URL(request.url);
1129
- return {
1130
- headers: request.headers,
1131
- cookies: __parseCookies(request.headers.get("cookie")),
1132
- query: url.searchParams,
1133
- host: request.headers.get("host") || url.host,
1134
- };
1135
- }
1138
+ // ── Config pattern matching, redirects, rewrites, headers, CSRF validation,
1139
+ // external URL proxy, cookie parsing, and request context are imported from
1140
+ // config-matchers.ts and request-pipeline.ts (see import statements above).
1141
+ // This eliminates ~250 lines of duplicated inline code and ensures the
1142
+ // single-pass tokenizer in config-matchers.ts is used consistently
1143
+ // (fixing the chained .replace() divergence flagged by CodeQL).
1136
1144
 
1137
1145
  /**
1138
1146
  * Build a request context from the live ALS HeadersContext, which reflects
@@ -1143,7 +1151,7 @@ function __buildRequestContext(request) {
1143
1151
  function __buildPostMwRequestContext(request) {
1144
1152
  const url = new URL(request.url);
1145
1153
  const ctx = getHeadersContext();
1146
- if (!ctx) return __buildRequestContext(request);
1154
+ if (!ctx) return requestContextFromRequest(request);
1147
1155
  // ctx.cookies is a Map<string, string> (HeadersContext), but RequestContext
1148
1156
  // requires a plain Record<string, string> for has/missing cookie evaluation
1149
1157
  // (config-matchers.ts uses obj[key] not Map.get()). Convert here.
@@ -1156,51 +1164,14 @@ function __buildPostMwRequestContext(request) {
1156
1164
  };
1157
1165
  }
1158
1166
 
1159
- function __sanitizeDestination(dest) {
1160
- if (dest.startsWith("http://") || dest.startsWith("https://")) return dest;
1161
- dest = dest.replace(/^[\\\\/]+/, "/");
1162
- return dest;
1163
- }
1164
-
1165
- function __applyConfigRedirects(pathname, ctx) {
1166
- for (const rule of __configRedirects) {
1167
- const params = __matchConfigPattern(pathname, rule.source);
1168
- if (params) {
1169
- if (ctx && (rule.has || rule.missing)) { if (!__checkHasConditions(rule.has, rule.missing, ctx)) continue; }
1170
- let dest = rule.destination;
1171
- for (const [key, value] of Object.entries(params)) { dest = dest.replace(":" + key + "*", value); dest = dest.replace(":" + key + "+", value); dest = dest.replace(":" + key, value); }
1172
- dest = __sanitizeDestination(dest);
1173
- return { destination: dest, permanent: rule.permanent };
1174
- }
1175
- }
1176
- return null;
1177
- }
1178
-
1179
- function __applyConfigRewrites(pathname, rules, ctx) {
1180
- for (const rule of rules) {
1181
- const params = __matchConfigPattern(pathname, rule.source);
1182
- if (params) {
1183
- if (ctx && (rule.has || rule.missing)) { if (!__checkHasConditions(rule.has, rule.missing, ctx)) continue; }
1184
- let dest = rule.destination;
1185
- for (const [key, value] of Object.entries(params)) { dest = dest.replace(":" + key + "*", value); dest = dest.replace(":" + key + "+", value); dest = dest.replace(":" + key, value); }
1186
- dest = __sanitizeDestination(dest);
1187
- return dest;
1188
- }
1189
- }
1190
- return null;
1191
- }
1192
-
1193
- function __isExternalUrl(url) {
1194
- return /^[a-z][a-z0-9+.-]*:/i.test(url) || url.startsWith("//");
1195
- }
1196
-
1197
1167
  /**
1198
- * Maximum server-action request body size (1 MB).
1199
- * Matches the Next.js default for serverActions.bodySizeLimit.
1168
+ * Maximum server-action request body size.
1169
+ * Configurable via experimental.serverActions.bodySizeLimit in next.config.
1170
+ * Defaults to 1MB, matching the Next.js default.
1200
1171
  * @see https://nextjs.org/docs/app/api-reference/config/next-config-js/serverActions#bodysizelimit
1201
1172
  * Prevents unbounded request body buffering.
1202
1173
  */
1203
- var __MAX_ACTION_BODY_SIZE = 1 * 1024 * 1024;
1174
+ var __MAX_ACTION_BODY_SIZE = ${JSON.stringify(bodySizeLimit)};
1204
1175
 
1205
1176
  /**
1206
1177
  * Read a request body as text with a size limit.
@@ -1259,71 +1230,11 @@ async function __readFormDataWithLimit(request, maxBytes) {
1259
1230
  return new Response(combined, { headers: { "Content-Type": contentType } }).formData();
1260
1231
  }
1261
1232
 
1262
- const __hopByHopHeaders = new Set(["connection","keep-alive","proxy-authenticate","proxy-authorization","te","trailers","transfer-encoding","upgrade"]);
1263
-
1264
- async function __proxyExternalRequest(request, externalUrl) {
1265
- const originalUrl = new URL(request.url);
1266
- const targetUrl = new URL(externalUrl);
1267
- for (const [key, value] of originalUrl.searchParams) {
1268
- if (!targetUrl.searchParams.has(key)) targetUrl.searchParams.set(key, value);
1269
- }
1270
- const headers = new Headers(request.headers);
1271
- headers.set("host", targetUrl.host);
1272
- headers.delete("connection");
1273
- for (const key of [...headers.keys()]) {
1274
- if (key.startsWith("x-middleware-")) headers.delete(key);
1275
- }
1276
- const method = request.method;
1277
- const hasBody = method !== "GET" && method !== "HEAD";
1278
- const init = { method, headers, redirect: "manual", signal: AbortSignal.timeout(30000) };
1279
- if (hasBody && request.body) { init.body = request.body; init.duplex = "half"; }
1280
- let upstream;
1281
- try { upstream = await fetch(targetUrl.href, init); }
1282
- catch (e) {
1283
- if (e && e.name === "TimeoutError") return new Response("Gateway Timeout", { status: 504 });
1284
- console.error("[vinext] External rewrite proxy error:", e); return new Response("Bad Gateway", { status: 502 });
1285
- }
1286
- const respHeaders = new Headers();
1287
- // Node.js fetch() auto-decompresses response bodies, while Workers fetch()
1288
- // preserves wire encoding. Only strip encoding/length on Node.js to avoid
1289
- // double-decompression errors without breaking Workers parity.
1290
- const __isNodeRuntime = typeof process !== "undefined" && !!(process.versions && process.versions.node);
1291
- upstream.headers.forEach(function(value, key) {
1292
- var lower = key.toLowerCase();
1293
- if (__hopByHopHeaders.has(lower)) return;
1294
- if (__isNodeRuntime && (lower === "content-encoding" || lower === "content-length")) return;
1295
- respHeaders.append(key, value);
1296
- });
1297
- return new Response(upstream.body, { status: upstream.status, statusText: upstream.statusText, headers: respHeaders });
1298
- }
1299
-
1300
- function __applyConfigHeaders(pathname, ctx) {
1301
- const result = [];
1302
- for (const rule of __configHeaders) {
1303
- const groups = [];
1304
- const withPlaceholders = rule.source.replace(/\\(([^)]+)\\)/g, (_, inner) => {
1305
- groups.push(inner);
1306
- return "___GROUP_" + (groups.length - 1) + "___";
1307
- });
1308
- const escaped = withPlaceholders
1309
- .replace(/\\./g, "\\\\.")
1310
- .replace(/\\+/g, "\\\\+")
1311
- .replace(/\\?/g, "\\\\?")
1312
- .replace(/\\*/g, ".*")
1313
- .replace(/:[\\w-]+/g, "[^/]+")
1314
- .replace(/___GROUP_(\\d+)___/g, (_, idx) => "(" + groups[Number(idx)] + ")");
1315
- const sourceRegex = __safeRegExp("^" + escaped + "$");
1316
- if (sourceRegex && sourceRegex.test(pathname)) {
1317
- if (ctx && (rule.has || rule.missing)) {
1318
- if (!__checkHasConditions(rule.has, rule.missing, ctx)) continue;
1319
- }
1320
- result.push(...rule.headers);
1321
- }
1322
- }
1323
- return result;
1324
- }
1325
-
1326
1233
  export default async function handler(request) {
1234
+ ${instrumentationPath ? `// Ensure instrumentation.register() has run before handling the first request.
1235
+ // This is a no-op after the first call (guarded by __instrumentationInitialized).
1236
+ await __ensureInstrumentation();
1237
+ ` : ""}
1327
1238
  // Wrap the entire request in nested AsyncLocalStorage.run() scopes to ensure
1328
1239
  // per-request isolation for all state modules. Each runWith*() creates an
1329
1240
  // ALS scope that propagates through all async continuations (including RSC
@@ -1335,7 +1246,7 @@ export default async function handler(request) {
1335
1246
  _runWithCacheState(() =>
1336
1247
  _runWithPrivateCache(() =>
1337
1248
  runWithFetchCache(async () => {
1338
- const __reqCtx = __buildRequestContext(request);
1249
+ const __reqCtx = requestContextFromRequest(request);
1339
1250
  // Per-request container for middleware state. Passed into
1340
1251
  // _handleRequest which fills in .headers and .status;
1341
1252
  // avoids module-level variables that race on Workers.
@@ -1350,7 +1261,7 @@ export default async function handler(request) {
1350
1261
  let pathname;
1351
1262
  try { pathname = __normalizePath(decodeURIComponent(url.pathname)); } catch { pathname = url.pathname; }
1352
1263
  ${bp ? `if (pathname.startsWith(${JSON.stringify(bp)})) pathname = pathname.slice(${JSON.stringify(bp)}.length) || "/";` : ""}
1353
- const extraHeaders = __applyConfigHeaders(pathname, __reqCtx);
1264
+ const extraHeaders = matchHeaders(pathname, __configHeaders, __reqCtx);
1354
1265
  for (const h of extraHeaders) {
1355
1266
  // Use append() for headers where multiple values must coexist
1356
1267
  // (Vary, Set-Cookie). Using set() on these would destroy
@@ -1384,22 +1295,18 @@ async function _handleRequest(request, __reqCtx, _mwCtx) {
1384
1295
  // Format: "handlerStart,compileMs,renderMs" - all as integers (ms). Dev-only.
1385
1296
  const url = new URL(request.url);
1386
1297
 
1387
- // ── Cross-origin request protection ─────────────────────────────────
1298
+ // ── Cross-origin request protection (dev only) ─────────────────────
1388
1299
  // Block requests from non-localhost origins to prevent data exfiltration.
1389
- const __originBlock = __validateDevRequestOrigin(request);
1390
- if (__originBlock) return __originBlock;
1391
-
1392
- // Guard against protocol-relative URL open redirects.
1393
- // Paths like //example.com/ would be redirected to //example.com by the
1394
- // trailing-slash normalizer, which browsers interpret as http://example.com.
1395
- // Backslashes are equivalent to forward slashes in the URL spec
1396
- // (e.g. /\\evil.com is treated as //evil.com by browsers and the URL constructor).
1397
- // Next.js returns 404 for these paths. Check the RAW pathname before
1398
- // normalization so the guard fires before normalizePath collapses //.
1399
- if (url.pathname.replaceAll("\\\\", "/").startsWith("//")) {
1400
- return new Response("404 Not Found", { status: 404 });
1300
+ // Skipped in production — Vite replaces NODE_ENV at build time.
1301
+ if (process.env.NODE_ENV !== "production") {
1302
+ const __originBlock = __validateDevRequestOrigin(request);
1303
+ if (__originBlock) return __originBlock;
1401
1304
  }
1402
1305
 
1306
+ // Guard against protocol-relative URL open redirects (see request-pipeline.ts).
1307
+ const __protoGuard = guardProtocolRelativeUrl(url.pathname);
1308
+ if (__protoGuard) return __protoGuard;
1309
+
1403
1310
  // Decode percent-encoding and normalize pathname to canonical form.
1404
1311
  // decodeURIComponent prevents /%61dmin from bypassing /admin matchers.
1405
1312
  // __normalizePath collapses //foo///bar → /foo/bar, resolves . and .. segments.
@@ -1411,20 +1318,12 @@ async function _handleRequest(request, __reqCtx, _mwCtx) {
1411
1318
 
1412
1319
  ${bp ? `
1413
1320
  // Strip basePath prefix
1414
- if (__basePath && pathname.startsWith(__basePath)) {
1415
- pathname = pathname.slice(__basePath.length) || "/";
1416
- }
1321
+ pathname = stripBasePath(pathname, __basePath);
1417
1322
  ` : ""}
1418
1323
 
1419
1324
  // Trailing slash normalization (redirect to canonical form)
1420
- if (pathname !== "/" && !pathname.startsWith("/api")) {
1421
- const hasTrailing = pathname.endsWith("/");
1422
- if (__trailingSlash && !hasTrailing && !pathname.endsWith(".rsc")) {
1423
- return Response.redirect(new URL(__basePath + pathname + "/" + url.search, request.url), 308);
1424
- } else if (!__trailingSlash && hasTrailing) {
1425
- return Response.redirect(new URL(__basePath + pathname.replace(/\\/+$/, "") + url.search, request.url), 308);
1426
- }
1427
- }
1325
+ const __tsRedirect = normalizeTrailingSlash(pathname, __basePath, __trailingSlash, url.search);
1326
+ if (__tsRedirect) return __tsRedirect;
1428
1327
 
1429
1328
  // ── Apply redirects from next.config.js ───────────────────────────────
1430
1329
  if (__configRedirects.length) {
@@ -1432,9 +1331,9 @@ async function _handleRequest(request, __reqCtx, _mwCtx) {
1432
1331
  // arrive as /some/path.rsc but redirect patterns are defined without it (e.g.
1433
1332
  // /some/path). Without this, soft-nav fetches bypass all config redirects.
1434
1333
  const __redirPathname = pathname.endsWith(".rsc") ? pathname.slice(0, -4) : pathname;
1435
- const __redir = __applyConfigRedirects(__redirPathname, __reqCtx);
1334
+ const __redir = matchRedirect(__redirPathname, __configRedirects, __reqCtx);
1436
1335
  if (__redir) {
1437
- const __redirDest = __sanitizeDestination(
1336
+ const __redirDest = sanitizeDestination(
1438
1337
  __basePath && !__redir.destination.startsWith(__basePath)
1439
1338
  ? __basePath + __redir.destination
1440
1339
  : __redir.destination
@@ -1533,30 +1432,26 @@ async function _handleRequest(request, __reqCtx, _mwCtx) {
1533
1432
  // internal routing signals and must never reach clients.
1534
1433
  if (_mwCtx.headers) {
1535
1434
  applyMiddlewareRequestHeaders(_mwCtx.headers);
1536
- for (const key of [..._mwCtx.headers.keys()]) {
1537
- if (key.startsWith("x-middleware-")) {
1538
- _mwCtx.headers.delete(key);
1539
- }
1540
- }
1435
+ processMiddlewareHeaders(_mwCtx.headers);
1541
1436
  }
1542
1437
  ` : ""}
1543
1438
 
1544
1439
  // Build post-middleware request context for afterFiles/fallback rewrites.
1545
1440
  // These run after middleware in the App Router execution order and should
1546
1441
  // evaluate has/missing conditions against middleware-modified headers.
1547
- // When no middleware is present, this falls back to __buildRequestContext.
1442
+ // When no middleware is present, this falls back to requestContextFromRequest.
1548
1443
  const __postMwReqCtx = __buildPostMwRequestContext(request);
1549
1444
 
1550
1445
  // ── Apply beforeFiles rewrites from next.config.js ────────────────────
1551
1446
  // In App Router execution order, beforeFiles runs after middleware so that
1552
1447
  // has/missing conditions can evaluate against middleware-modified headers.
1553
1448
  if (__configRewrites.beforeFiles && __configRewrites.beforeFiles.length) {
1554
- const __rewritten = __applyConfigRewrites(cleanPathname, __configRewrites.beforeFiles, __postMwReqCtx);
1449
+ const __rewritten = matchRewrite(cleanPathname, __configRewrites.beforeFiles, __postMwReqCtx);
1555
1450
  if (__rewritten) {
1556
- if (__isExternalUrl(__rewritten)) {
1451
+ if (isExternalUrl(__rewritten)) {
1557
1452
  setHeadersContext(null);
1558
1453
  setNavigationContext(null);
1559
- return __proxyExternalRequest(request, __rewritten);
1454
+ return proxyExternalRequest(request, __rewritten);
1560
1455
  }
1561
1456
  cleanPathname = __rewritten;
1562
1457
  }
@@ -1564,22 +1459,10 @@ async function _handleRequest(request, __reqCtx, _mwCtx) {
1564
1459
 
1565
1460
  // ── Image optimization passthrough (dev mode — no transformation) ───────
1566
1461
  if (cleanPathname === "/_vinext/image") {
1567
- const __rawImgUrl = url.searchParams.get("url");
1568
- // Normalize backslashes: browsers and the URL constructor treat
1569
- // /\\evil.com as protocol-relative (//evil.com), bypassing the // check.
1570
- const __imgUrl = __rawImgUrl?.replaceAll("\\\\", "/") ?? null;
1571
- // Allowlist: must start with "/" but not "//" — blocks absolute URLs,
1572
- // protocol-relative, backslash variants, and exotic schemes.
1573
- if (!__imgUrl || !__imgUrl.startsWith("/") || __imgUrl.startsWith("//")) {
1574
- return new Response(!__rawImgUrl ? "Missing url parameter" : "Only relative URLs allowed", { status: 400 });
1575
- }
1576
- // Validate the constructed URL's origin hasn't changed (defense in depth).
1577
- const __resolvedImg = new URL(__imgUrl, request.url);
1578
- if (__resolvedImg.origin !== url.origin) {
1579
- return new Response("Only relative URLs allowed", { status: 400 });
1580
- }
1462
+ const __imgResult = validateImageUrl(url.searchParams.get("url"), request.url);
1463
+ if (__imgResult instanceof Response) return __imgResult;
1581
1464
  // In dev, redirect to the original asset URL so Vite's static serving handles it.
1582
- return Response.redirect(__resolvedImg.href, 302);
1465
+ return Response.redirect(new URL(__imgResult, url.origin).href, 302);
1583
1466
  }
1584
1467
 
1585
1468
  // Handle metadata routes (sitemap.xml, robots.txt, manifest.webmanifest, etc.)
@@ -1635,7 +1518,7 @@ async function _handleRequest(request, __reqCtx, _mwCtx) {
1635
1518
  // ── CSRF protection ─────────────────────────────────────────────────
1636
1519
  // Verify that the Origin header matches the Host header to prevent
1637
1520
  // cross-site request forgery, matching Next.js server action behavior.
1638
- const csrfResponse = __validateCsrfOrigin(request);
1521
+ const csrfResponse = validateCsrfOrigin(request, __allowedOrigins);
1639
1522
  if (csrfResponse) return csrfResponse;
1640
1523
 
1641
1524
  // ── Body size limit ─────────────────────────────────────────────────
@@ -1744,16 +1627,23 @@ async function _handleRequest(request, __reqCtx, _mwCtx) {
1744
1627
  element = createElement("div", null, "Page not found");
1745
1628
  }
1746
1629
 
1630
+ const onRenderError = createRscOnErrorHandler(
1631
+ request,
1632
+ cleanPathname,
1633
+ match ? match.route.pattern : cleanPathname,
1634
+ );
1747
1635
  const rscStream = renderToReadableStream(
1748
1636
  { root: element, returnValue },
1749
- { temporaryReferences, onError: rscOnError },
1637
+ { temporaryReferences, onError: onRenderError },
1750
1638
  );
1751
1639
 
1752
- // Collect cookies set during the action
1640
+ // Collect cookies set during the action synchronously (before stream is consumed).
1641
+ // Do NOT clear headers/navigation context here — the RSC stream is consumed lazily
1642
+ // by the client, and async server components that run during consumption need the
1643
+ // context to still be live. The AsyncLocalStorage scope from runWithHeadersContext
1644
+ // handles cleanup naturally when all async continuations complete.
1753
1645
  const actionPendingCookies = getAndClearPendingCookies();
1754
1646
  const actionDraftCookie = getDraftModeCookieHeader();
1755
- setHeadersContext(null);
1756
- setNavigationContext(null);
1757
1647
 
1758
1648
  const actionHeaders = { "Content-Type": "text/x-component; charset=utf-8", "Vary": "RSC, Accept" };
1759
1649
  const actionResponse = new Response(rscStream, { headers: actionHeaders });
@@ -1787,12 +1677,12 @@ async function _handleRequest(request, __reqCtx, _mwCtx) {
1787
1677
 
1788
1678
  // ── Apply afterFiles rewrites from next.config.js ──────────────────────
1789
1679
  if (__configRewrites.afterFiles && __configRewrites.afterFiles.length) {
1790
- const __afterRewritten = __applyConfigRewrites(cleanPathname, __configRewrites.afterFiles, __postMwReqCtx);
1680
+ const __afterRewritten = matchRewrite(cleanPathname, __configRewrites.afterFiles, __postMwReqCtx);
1791
1681
  if (__afterRewritten) {
1792
- if (__isExternalUrl(__afterRewritten)) {
1682
+ if (isExternalUrl(__afterRewritten)) {
1793
1683
  setHeadersContext(null);
1794
1684
  setNavigationContext(null);
1795
- return __proxyExternalRequest(request, __afterRewritten);
1685
+ return proxyExternalRequest(request, __afterRewritten);
1796
1686
  }
1797
1687
  cleanPathname = __afterRewritten;
1798
1688
  }
@@ -1802,12 +1692,12 @@ async function _handleRequest(request, __reqCtx, _mwCtx) {
1802
1692
 
1803
1693
  // ── Fallback rewrites from next.config.js (if no route matched) ───────
1804
1694
  if (!match && __configRewrites.fallback && __configRewrites.fallback.length) {
1805
- const __fallbackRewritten = __applyConfigRewrites(cleanPathname, __configRewrites.fallback, __postMwReqCtx);
1695
+ const __fallbackRewritten = matchRewrite(cleanPathname, __configRewrites.fallback, __postMwReqCtx);
1806
1696
  if (__fallbackRewritten) {
1807
- if (__isExternalUrl(__fallbackRewritten)) {
1697
+ if (isExternalUrl(__fallbackRewritten)) {
1808
1698
  setHeadersContext(null);
1809
1699
  setNavigationContext(null);
1810
- return __proxyExternalRequest(request, __fallbackRewritten);
1700
+ return proxyExternalRequest(request, __fallbackRewritten);
1811
1701
  }
1812
1702
  cleanPathname = __fallbackRewritten;
1813
1703
  match = matchRoute(cleanPathname, routes);
@@ -2065,9 +1955,16 @@ async function _handleRequest(request, __reqCtx, _mwCtx) {
2065
1955
  interceptPage: intercept.page,
2066
1956
  interceptParams: intercept.matchedParams,
2067
1957
  }, url.searchParams);
2068
- const interceptStream = renderToReadableStream(interceptElement, { onError: rscOnError });
2069
- setHeadersContext(null);
2070
- setNavigationContext(null);
1958
+ const interceptOnError = createRscOnErrorHandler(
1959
+ request,
1960
+ cleanPathname,
1961
+ sourceRoute.pattern,
1962
+ );
1963
+ const interceptStream = renderToReadableStream(interceptElement, { onError: interceptOnError });
1964
+ // Do NOT clear headers/navigation context here — the RSC stream is consumed lazily
1965
+ // by the client, and async server components that run during consumption need the
1966
+ // context to still be live. The AsyncLocalStorage scope from runWithHeadersContext
1967
+ // handles cleanup naturally when all async continuations complete.
2071
1968
  return new Response(interceptStream, {
2072
1969
  headers: { "Content-Type": "text/x-component; charset=utf-8", "Vary": "RSC, Accept" },
2073
1970
  });
@@ -2254,8 +2151,19 @@ async function _handleRequest(request, __reqCtx, _mwCtx) {
2254
2151
  // Mark end of compile phase: route matching, middleware, tree building are done.
2255
2152
  if (process.env.NODE_ENV !== "production") __compileEnd = performance.now();
2256
2153
 
2257
- // Render to RSC stream
2258
- const rscStream = renderToReadableStream(element, { onError: rscOnError });
2154
+ // Render to RSC stream.
2155
+ // Track non-navigation RSC errors so we can detect when the in-tree global
2156
+ // ErrorBoundary catches during SSR (producing double <html>/<body>) and
2157
+ // re-render with renderErrorBoundaryPage (which skips layouts for global-error).
2158
+ let _rscErrorForRerender = null;
2159
+ const _baseOnError = createRscOnErrorHandler(request, cleanPathname, route.pattern);
2160
+ const onRenderError = function(error, requestInfo, errorContext) {
2161
+ if (!(error && typeof error === "object" && "digest" in error)) {
2162
+ _rscErrorForRerender = error;
2163
+ }
2164
+ return _baseOnError(error, requestInfo, errorContext);
2165
+ };
2166
+ const rscStream = renderToReadableStream(element, { onError: onRenderError });
2259
2167
 
2260
2168
  if (isRscRequest) {
2261
2169
  // Direct RSC stream response (for client-side navigation)
@@ -2359,6 +2267,20 @@ async function _handleRequest(request, __reqCtx, _mwCtx) {
2359
2267
  throw ssrErr;
2360
2268
  }
2361
2269
 
2270
+ // If an RSC error was caught by the in-tree global ErrorBoundary during SSR,
2271
+ // the HTML output has double <html>/<body> (root layout + global-error.tsx).
2272
+ // Discard it and re-render using renderErrorBoundaryPage which skips layouts
2273
+ // when the error falls through to global-error.tsx.
2274
+ ${globalErrorVar ? `
2275
+ if (_rscErrorForRerender && !isRscRequest) {
2276
+ const _hasLocalBoundary = !!(route?.error?.default) || !!(route?.errors && route.errors.some(function(e) { return e?.default; }));
2277
+ if (!_hasLocalBoundary) {
2278
+ const cleanResp = await renderErrorBoundaryPage(route, _rscErrorForRerender, false, request, params);
2279
+ if (cleanResp) return cleanResp;
2280
+ }
2281
+ }
2282
+ ` : ""}
2283
+
2362
2284
  // Check for draftMode Set-Cookie header (from draftMode().enable()/disable())
2363
2285
  const draftCookie = getDraftModeCookieHeader();
2364
2286
 
@@ -2482,771 +2404,4 @@ if (import.meta.hot) {
2482
2404
  }
2483
2405
  `;
2484
2406
  }
2485
- /**
2486
- * Generate the virtual SSR entry module.
2487
- *
2488
- * This runs in the `ssr` Vite environment. It receives an RSC stream,
2489
- * deserializes it to a React tree, and renders to HTML.
2490
- */
2491
- export function generateSsrEntry() {
2492
- return `
2493
- import { createFromReadableStream } from "@vitejs/plugin-rsc/ssr";
2494
- import { renderToReadableStream, renderToStaticMarkup } from "react-dom/server.edge";
2495
- import { setNavigationContext, ServerInsertedHTMLContext } from "next/navigation";
2496
- import { runWithNavigationContext as _runWithNavCtx } from "vinext/navigation-state";
2497
- import { safeJsonStringify } from "vinext/html";
2498
- import { createElement as _ssrCE } from "react";
2499
-
2500
- /**
2501
- * Collect all chunks from a ReadableStream into an array of text strings.
2502
- * Used to capture the RSC payload for embedding in HTML.
2503
- * The RSC flight protocol is text-based (line-delimited key:value pairs),
2504
- * so we decode to text strings instead of byte arrays — this is dramatically
2505
- * more compact when JSON-serialized into inline <script> tags.
2506
- */
2507
- async function collectStreamChunks(stream) {
2508
- const reader = stream.getReader();
2509
- const decoder = new TextDecoder();
2510
- const chunks = [];
2511
- while (true) {
2512
- const { done, value } = await reader.read();
2513
- if (done) break;
2514
- // Decode Uint8Array to text string for compact JSON serialization
2515
- chunks.push(decoder.decode(value, { stream: true }));
2516
- }
2517
- return chunks;
2518
- }
2519
-
2520
- // React 19 dev-mode workaround (see VinextFlightRoot in handleSsr):
2521
- //
2522
- // In dev, Flight error decoding in react-server-dom-webpack/client.edge
2523
- // can hit resolveErrorDev() which (via React's dev error stack capture)
2524
- // expects a non-null hooks dispatcher.
2525
- //
2526
- // Vinext previously called createFromReadableStream() outside of any React render.
2527
- // When an RSC stream contains an error, dev-mode decoding could crash with:
2528
- // - "Invalid hook call"
2529
- // - "Cannot read properties of null (reading 'useContext')"
2530
- //
2531
- // Fix: call createFromReadableStream() lazily inside a React component render.
2532
- // This mirrors Next.js behavior and ensures the dispatcher is set.
2533
-
2534
- /**
2535
- * Create a TransformStream that appends RSC chunks as inline <script> tags
2536
- * to the HTML stream. This allows progressive hydration — the browser receives
2537
- * RSC data incrementally as Suspense boundaries resolve, rather than waiting
2538
- * for the entire RSC payload before hydration can begin.
2539
- *
2540
- * Each chunk is written as:
2541
- * <script>self.__VINEXT_RSC_CHUNKS__=self.__VINEXT_RSC_CHUNKS__||[];self.__VINEXT_RSC_CHUNKS__.push("...")</script>
2542
- *
2543
- * Chunks are embedded as text strings (not byte arrays) since the RSC flight
2544
- * protocol is text-based. The browser entry encodes them back to Uint8Array.
2545
- * This is ~3x more compact than the previous byte-array format.
2546
- */
2547
- function createRscEmbedTransform(embedStream) {
2548
- const reader = embedStream.getReader();
2549
- const _decoder = new TextDecoder();
2550
- let done = false;
2551
- let pendingChunks = [];
2552
- let reading = false;
2553
-
2554
- // Fix invalid preload "as" values in RSC Flight hint lines before
2555
- // they reach the client. React Flight emits HL hints with
2556
- // as="stylesheet" for CSS, but the HTML spec requires as="style"
2557
- // for <link rel="preload">. The fixPreloadAs() below only fixes the
2558
- // server-rendered HTML stream; this fixes the raw Flight data that
2559
- // gets embedded as __VINEXT_RSC_CHUNKS__ and processed client-side.
2560
- function fixFlightHints(text) {
2561
- // Flight hint format: <id>:HL["url","stylesheet"] or with options
2562
- return text.replace(/(\\d+:HL\\[.*?),"stylesheet"(\\]|,)/g, '$1,"style"$2');
2563
- }
2564
-
2565
- // Start reading RSC chunks in the background, accumulating them as text strings.
2566
- // The RSC flight protocol is text-based, so decoding to strings and embedding
2567
- // as JSON strings is ~3x more compact than the byte-array format.
2568
- async function pumpReader() {
2569
- if (reading) return;
2570
- reading = true;
2571
- try {
2572
- while (true) {
2573
- const result = await reader.read();
2574
- if (result.done) {
2575
- done = true;
2576
- break;
2577
- }
2578
- const text = _decoder.decode(result.value, { stream: true });
2579
- pendingChunks.push(fixFlightHints(text));
2580
- }
2581
- } catch (err) {
2582
- if (process.env.NODE_ENV !== "production") {
2583
- console.warn("[vinext] RSC embed stream read error:", err);
2584
- }
2585
- done = true;
2586
- }
2587
- reading = false;
2588
- }
2589
-
2590
- // Fire off the background reader immediately
2591
- const pumpPromise = pumpReader();
2592
-
2593
- return {
2594
- /**
2595
- * Flush any accumulated RSC chunks as <script> tags.
2596
- * Called after each HTML chunk is enqueued.
2597
- */
2598
- flush() {
2599
- if (pendingChunks.length === 0) return "";
2600
- const chunks = pendingChunks;
2601
- pendingChunks = [];
2602
- let scripts = "";
2603
- for (const chunk of chunks) {
2604
- scripts += "<script>self.__VINEXT_RSC_CHUNKS__=self.__VINEXT_RSC_CHUNKS__||[];self.__VINEXT_RSC_CHUNKS__.push(" + safeJsonStringify(chunk) + ")</script>";
2605
- }
2606
- return scripts;
2607
- },
2608
-
2609
- /**
2610
- * Wait for the RSC stream to fully complete and return any final
2611
- * script tags plus the closing signal.
2612
- */
2613
- async finalize() {
2614
- await pumpPromise;
2615
- let scripts = this.flush();
2616
- // Signal that all RSC chunks have been sent.
2617
- // Params are already embedded in <head> — no need to include here.
2618
- scripts += "<script>self.__VINEXT_RSC_DONE__=true</script>";
2619
- return scripts;
2620
- },
2621
- };
2622
- }
2623
-
2624
- /**
2625
- * Render the RSC stream to HTML.
2626
- *
2627
- * @param rscStream - The RSC payload stream from the RSC environment
2628
- * @param navContext - Navigation context for client component SSR hooks.
2629
- * "use client" components like those using usePathname() need the current
2630
- * request URL during SSR, and they run in this SSR environment (separate
2631
- * from the RSC environment where the context was originally set).
2632
- * @param fontData - Font links and styles collected from the RSC environment.
2633
- * Fonts are loaded during RSC rendering (when layout calls Geist() etc.),
2634
- * and the data needs to be passed to SSR since they're separate module instances.
2635
- */
2636
- export async function handleSsr(rscStream, navContext, fontData) {
2637
- // Wrap in a navigation ALS scope for per-request isolation in the SSR
2638
- // environment. The SSR environment has separate module instances from RSC,
2639
- // so it needs its own ALS scope.
2640
- return _runWithNavCtx(async () => {
2641
- // Set navigation context so hooks like usePathname() work during SSR
2642
- // of "use client" components
2643
- if (navContext) {
2644
- setNavigationContext(navContext);
2645
- }
2646
-
2647
- // Clear any stale callbacks from previous requests
2648
- const { clearServerInsertedHTML, flushServerInsertedHTML, useServerInsertedHTML: _addInsertedHTML } = await import("next/navigation");
2649
- clearServerInsertedHTML();
2650
-
2651
- try {
2652
- // Tee the RSC stream - one for SSR rendering, one for embedding in HTML.
2653
- // This ensures the browser uses the SAME RSC payload for hydration that
2654
- // was used to generate the HTML, avoiding hydration mismatches (React #418).
2655
- const [ssrStream, embedStream] = rscStream.tee();
2656
-
2657
- // Create the progressive RSC embed helper — it reads the embed stream
2658
- // in the background and provides script tags to inject into the HTML stream.
2659
- const rscEmbed = createRscEmbedTransform(embedStream);
2660
-
2661
- // Deserialize RSC stream back to React VDOM.
2662
- // IMPORTANT: Do NOT await this — createFromReadableStream returns a thenable
2663
- // that React's renderToReadableStream can consume progressively. By passing
2664
- // the unresolved thenable, React will render Suspense fallbacks (loading.tsx)
2665
- // immediately in the HTML shell, then stream in resolved content as RSC
2666
- // chunks arrive. Awaiting here would block until all async server components
2667
- // complete, collapsing the streaming behavior.
2668
- // Lazily create the Flight root inside render so React's hook dispatcher is set
2669
- // (avoids React 19 dev-mode resolveErrorDev() crash). VinextFlightRoot returns
2670
- // a thenable (not a ReactNode), which React 19 consumes via its internal
2671
- // thenable-as-child suspend/resume behavior. This matches Next.js's approach.
2672
- let flightRoot;
2673
- function VinextFlightRoot() {
2674
- if (!flightRoot) {
2675
- flightRoot = createFromReadableStream(ssrStream);
2676
- }
2677
- return flightRoot;
2678
- }
2679
- const root = _ssrCE(VinextFlightRoot);
2680
-
2681
- // Wrap with ServerInsertedHTMLContext.Provider so libraries that use
2682
- // useContext(ServerInsertedHTMLContext) (Apollo Client, styled-components,
2683
- // etc.) get a working callback registration function during SSR.
2684
- // The provider value is useServerInsertedHTML — same function that direct
2685
- // callers use — so both paths push to the same ALS-backed callback array.
2686
- const ssrRoot = ServerInsertedHTMLContext
2687
- ? _ssrCE(ServerInsertedHTMLContext.Provider, { value: _addInsertedHTML }, root)
2688
- : root;
2689
-
2690
- // Get the bootstrap script content for the browser entry
2691
- const bootstrapScriptContent =
2692
- await import.meta.viteRsc.loadBootstrapScriptContent("index");
2693
-
2694
- // djb2 hash for digest generation in the SSR environment.
2695
- // Matches the RSC environment's __errorDigest function.
2696
- function ssrErrorDigest(str) {
2697
- let hash = 5381;
2698
- for (let i = str.length - 1; i >= 0; i--) {
2699
- hash = (hash * 33) ^ str.charCodeAt(i);
2700
- }
2701
- return (hash >>> 0).toString();
2702
- }
2703
-
2704
- // Render HTML (streaming SSR)
2705
- // useServerInsertedHTML callbacks are registered during this render.
2706
- // The onError callback preserves the digest for Next.js navigation errors
2707
- // (redirect, notFound, forbidden, unauthorized) thrown inside Suspense
2708
- // boundaries during RSC streaming. Without this, React's default onError
2709
- // returns undefined and the digest is lost in the $RX() call, preventing
2710
- // client-side error boundaries from identifying the error type.
2711
- // In production, non-navigation errors also get a digest hash so they
2712
- // can be correlated with server logs without leaking details to clients.
2713
- const htmlStream = await renderToReadableStream(ssrRoot, {
2714
- bootstrapScriptContent,
2715
- onError(error) {
2716
- if (error && typeof error === "object" && "digest" in error) {
2717
- return String(error.digest);
2718
- }
2719
- // In production, generate a digest hash for non-navigation errors
2720
- if (process.env.NODE_ENV === "production" && error) {
2721
- const msg = error instanceof Error ? error.message : String(error);
2722
- const stack = error instanceof Error ? (error.stack || "") : "";
2723
- return ssrErrorDigest(msg + stack);
2724
- }
2725
- return undefined;
2726
- },
2727
- });
2728
-
2729
-
2730
- // Flush useServerInsertedHTML callbacks (CSS-in-JS style injection)
2731
- const insertedElements = flushServerInsertedHTML();
2732
-
2733
- // Render the inserted elements to HTML strings
2734
- const { Fragment } = await import("react");
2735
- let insertedHTML = "";
2736
- for (const el of insertedElements) {
2737
- try {
2738
- insertedHTML += renderToStaticMarkup(_ssrCE(Fragment, null, el));
2739
- } catch {
2740
- // Skip elements that can't be rendered
2741
- }
2742
- }
2743
-
2744
- // Escape HTML attribute values (defense-in-depth for font URLs/types).
2745
- function _escAttr(s) { return s.replace(/&/g, "&amp;").replace(/"/g, "&quot;"); }
2746
-
2747
- // Build font HTML from data passed from RSC environment
2748
- // (Fonts are loaded during RSC rendering, and RSC/SSR are separate module instances)
2749
- let fontHTML = "";
2750
- if (fontData) {
2751
- if (fontData.links && fontData.links.length > 0) {
2752
- for (const url of fontData.links) {
2753
- fontHTML += '<link rel="stylesheet" href="' + _escAttr(url) + '" />\\n';
2754
- }
2755
- }
2756
- // Emit <link rel="preload"> for local font files
2757
- if (fontData.preloads && fontData.preloads.length > 0) {
2758
- for (const preload of fontData.preloads) {
2759
- fontHTML += '<link rel="preload" href="' + _escAttr(preload.href) + '" as="font" type="' + _escAttr(preload.type) + '" crossorigin />\\n';
2760
- }
2761
- }
2762
- if (fontData.styles && fontData.styles.length > 0) {
2763
- fontHTML += '<style data-vinext-fonts>' + fontData.styles.join("\\n") + '</style>\\n';
2764
- }
2765
- }
2766
-
2767
- // Extract client entry module URL from bootstrapScriptContent to emit
2768
- // a <link rel="modulepreload"> hint. The RSC plugin formats bootstrap
2769
- // content as: import("URL") — we extract the URL so the browser can
2770
- // speculatively fetch and parse the JS module while still processing
2771
- // the HTML body, instead of waiting until it reaches the inline script.
2772
- let modulePreloadHTML = "";
2773
- if (bootstrapScriptContent) {
2774
- const m = bootstrapScriptContent.match(/import\\("([^"]+)"\\)/);
2775
- if (m && m[1]) {
2776
- modulePreloadHTML = '<link rel="modulepreload" href="' + _escAttr(m[1]) + '" />\\n';
2777
- }
2778
- }
2779
-
2780
- // Head-injected HTML: server-inserted HTML, font HTML, route params,
2781
- // and modulepreload hints.
2782
- // RSC payload is now embedded progressively via script tags in the body stream.
2783
- // Params are embedded eagerly in <head> so they're available before client
2784
- // hydration starts, avoiding the need for polling on the client.
2785
- const paramsScript = '<script>self.__VINEXT_RSC_PARAMS__=' + safeJsonStringify(navContext?.params || {}) + '</script>';
2786
- const injectHTML = paramsScript + modulePreloadHTML + insertedHTML + fontHTML;
2787
-
2788
- // Inject the collected HTML before </head> and progressively embed RSC
2789
- // chunks as script tags throughout the HTML body stream.
2790
- const decoder = new TextDecoder();
2791
- const encoder = new TextEncoder();
2792
- let injected = false;
2793
-
2794
- // Fix invalid preload "as" values in server-rendered HTML.
2795
- // React Fizz emits <link rel="preload" as="stylesheet"> for CSS,
2796
- // but the HTML spec requires as="style" for <link rel="preload">.
2797
- // Note: fixFlightHints() in createRscEmbedTransform handles the
2798
- // complementary case — fixing the raw Flight stream data before
2799
- // it's embedded as __VINEXT_RSC_CHUNKS__ for client-side processing.
2800
- // See: https://html.spec.whatwg.org/multipage/links.html#link-type-preload
2801
- function fixPreloadAs(html) {
2802
- // Match <link ...rel="preload"... as="stylesheet"...> in any attribute order
2803
- return html.replace(/<link(?=[^>]*\\srel="preload")[^>]*>/g, function(tag) {
2804
- return tag.replace(' as="stylesheet"', ' as="style"');
2805
- });
2806
- }
2807
-
2808
- // Tick-buffered RSC script injection.
2809
- //
2810
- // React's renderToReadableStream (Fizz) flushes chunks synchronously
2811
- // within one microtask — all chunks from a single flushCompletedQueues
2812
- // call arrive in the same macrotask. We buffer HTML chunks as they
2813
- // arrive, then use setTimeout(0) to defer emitting them plus any
2814
- // accumulated RSC scripts to the next macrotask. This guarantees we
2815
- // never inject <script> tags between partial HTML chunks (which would
2816
- // corrupt split elements like "<linearGradi" + "ent>"), while still
2817
- // delivering RSC data progressively as Suspense boundaries resolve.
2818
- //
2819
- // Reference: rsc-html-stream by Devon Govett (credited by Next.js)
2820
- // https://github.com/devongovett/rsc-html-stream
2821
- let buffered = [];
2822
- let timeoutId = null;
2823
-
2824
- const transform = new TransformStream({
2825
- transform(chunk, controller) {
2826
- const text = decoder.decode(chunk, { stream: true });
2827
- const fixed = fixPreloadAs(text);
2828
- buffered.push(fixed);
2829
-
2830
- if (timeoutId !== null) return;
2831
-
2832
- timeoutId = setTimeout(() => {
2833
- // Flush all buffered HTML chunks from this React flush cycle
2834
- for (const buf of buffered) {
2835
- if (!injected) {
2836
- const headEnd = buf.indexOf("</head>");
2837
- if (headEnd !== -1) {
2838
- const before = buf.slice(0, headEnd);
2839
- const after = buf.slice(headEnd);
2840
- controller.enqueue(encoder.encode(before + injectHTML + after));
2841
- injected = true;
2842
- continue;
2843
- }
2844
- }
2845
- controller.enqueue(encoder.encode(buf));
2846
- }
2847
- buffered = [];
2848
-
2849
- // Now safe to inject any accumulated RSC scripts — we're between
2850
- // React flush cycles, so no partial HTML chunks can follow until
2851
- // the next macrotask.
2852
- const rscScripts = rscEmbed.flush();
2853
- if (rscScripts) {
2854
- controller.enqueue(encoder.encode(rscScripts));
2855
- }
2856
-
2857
- timeoutId = null;
2858
- }, 0);
2859
- },
2860
- async flush(controller) {
2861
- // Cancel any pending setTimeout callback — flush() drains
2862
- // everything itself, so the callback would be a no-op but
2863
- // cancelling makes the code obviously correct.
2864
- if (timeoutId !== null) {
2865
- clearTimeout(timeoutId);
2866
- timeoutId = null;
2867
- }
2868
-
2869
- // Flush any remaining buffered HTML chunks
2870
- for (const buf of buffered) {
2871
- if (!injected) {
2872
- const headEnd = buf.indexOf("</head>");
2873
- if (headEnd !== -1) {
2874
- const before = buf.slice(0, headEnd);
2875
- const after = buf.slice(headEnd);
2876
- controller.enqueue(encoder.encode(before + injectHTML + after));
2877
- injected = true;
2878
- continue;
2879
- }
2880
- }
2881
- controller.enqueue(encoder.encode(buf));
2882
- }
2883
- buffered = [];
2884
-
2885
- if (!injected && injectHTML) {
2886
- controller.enqueue(encoder.encode(injectHTML));
2887
- }
2888
- // Finalize: wait for the RSC stream to complete and emit remaining
2889
- // chunks plus the __VINEXT_RSC_DONE__ signal.
2890
- const finalScripts = await rscEmbed.finalize();
2891
- if (finalScripts) {
2892
- controller.enqueue(encoder.encode(finalScripts));
2893
- }
2894
- },
2895
- });
2896
-
2897
- return htmlStream.pipeThrough(transform);
2898
- } finally {
2899
- // Clean up so we don't leak context between requests
2900
- setNavigationContext(null);
2901
- clearServerInsertedHTML();
2902
- }
2903
- }); // end _runWithNavCtx
2904
- }
2905
-
2906
- export default {
2907
- async fetch(request) {
2908
- const url = new URL(request.url);
2909
- if (url.pathname.startsWith("//")) {
2910
- return new Response("404 Not Found", { status: 404 });
2911
- }
2912
- const rscModule = await import.meta.viteRsc.loadModule("rsc", "index");
2913
- const result = await rscModule.default(request);
2914
- if (result instanceof Response) {
2915
- return result;
2916
- }
2917
- if (result === null || result === undefined) {
2918
- return new Response("Not Found", { status: 404 });
2919
- }
2920
- return new Response(String(result), { status: 200 });
2921
- },
2922
- };
2923
- `;
2924
- }
2925
- /**
2926
- * Generate the virtual browser entry module.
2927
- *
2928
- * This runs in the client (browser). It hydrates the page from the
2929
- * embedded RSC payload and handles client-side navigation by re-fetching
2930
- * RSC streams.
2931
- */
2932
- export function generateBrowserEntry() {
2933
- return `
2934
- import {
2935
- createFromReadableStream,
2936
- createFromFetch,
2937
- setServerCallback,
2938
- encodeReply,
2939
- createTemporaryReferenceSet,
2940
- } from "@vitejs/plugin-rsc/browser";
2941
- import { hydrateRoot } from "react-dom/client";
2942
- import { flushSync } from "react-dom";
2943
- import { setClientParams, toRscUrl, getPrefetchCache, getPrefetchedUrls, PREFETCH_CACHE_TTL } from "next/navigation";
2944
-
2945
- let reactRoot;
2946
-
2947
- /**
2948
- * Convert the embedded RSC chunks back to a ReadableStream.
2949
- * Each chunk is a text string that needs to be encoded back to Uint8Array.
2950
- */
2951
- function chunksToReadableStream(chunks) {
2952
- const encoder = new TextEncoder();
2953
- return new ReadableStream({
2954
- start(controller) {
2955
- for (const chunk of chunks) {
2956
- controller.enqueue(encoder.encode(chunk));
2957
- }
2958
- controller.close();
2959
- }
2960
- });
2961
- }
2962
-
2963
- /**
2964
- * Create a ReadableStream from progressively-embedded RSC chunks.
2965
- * The server injects RSC data as <script> tags that push to
2966
- * self.__VINEXT_RSC_CHUNKS__ throughout the HTML stream, and sets
2967
- * self.__VINEXT_RSC_DONE__ = true when complete.
2968
- *
2969
- * Instead of polling with setTimeout, we monkey-patch the array's
2970
- * push() method so new chunks are delivered immediately when the
2971
- * server's <script> tags execute. This eliminates unnecessary
2972
- * wakeups and reduces latency — same pattern Next.js uses with
2973
- * __next_f. The stream closes on DOMContentLoaded (when all
2974
- * server-injected scripts have executed) or when __VINEXT_RSC_DONE__
2975
- * is set, whichever comes first.
2976
- */
2977
- function createProgressiveRscStream() {
2978
- const encoder = new TextEncoder();
2979
- return new ReadableStream({
2980
- start(controller) {
2981
- const chunks = self.__VINEXT_RSC_CHUNKS__ || [];
2982
-
2983
- // Deliver any chunks that arrived before this code ran
2984
- // (from <script> tags that executed before the browser entry loaded)
2985
- for (const chunk of chunks) {
2986
- controller.enqueue(encoder.encode(chunk));
2987
- }
2988
-
2989
- // If the stream is already complete, close immediately
2990
- if (self.__VINEXT_RSC_DONE__) {
2991
- controller.close();
2992
- return;
2993
- }
2994
-
2995
- // Monkey-patch push() so future chunks stream in immediately
2996
- // when the server's <script> tags execute
2997
- let closed = false;
2998
- function closeOnce() {
2999
- if (!closed) {
3000
- closed = true;
3001
- controller.close();
3002
- }
3003
- }
3004
-
3005
- const arr = self.__VINEXT_RSC_CHUNKS__ = self.__VINEXT_RSC_CHUNKS__ || [];
3006
- arr.push = function(chunk) {
3007
- Array.prototype.push.call(this, chunk);
3008
- if (!closed) {
3009
- controller.enqueue(encoder.encode(chunk));
3010
- if (self.__VINEXT_RSC_DONE__) {
3011
- closeOnce();
3012
- }
3013
- }
3014
- return this.length;
3015
- };
3016
-
3017
- // Safety net: if the server crashes mid-stream and __VINEXT_RSC_DONE__
3018
- // never arrives, close the stream when all server-injected scripts
3019
- // have executed (DOMContentLoaded). Without this, a truncated response
3020
- // leaves the ReadableStream open forever, hanging hydration.
3021
- if (typeof document !== "undefined") {
3022
- if (document.readyState === "loading") {
3023
- document.addEventListener("DOMContentLoaded", closeOnce);
3024
- } else {
3025
- // Document already loaded — close immediately if not already done
3026
- closeOnce();
3027
- }
3028
- }
3029
- }
3030
- });
3031
- }
3032
-
3033
- // Register the server action callback — React calls this internally
3034
- // when a "use server" function is invoked from client code.
3035
- setServerCallback(async (id, args) => {
3036
- const temporaryReferences = createTemporaryReferenceSet();
3037
- const body = await encodeReply(args, { temporaryReferences });
3038
-
3039
- const fetchResponse = await fetch(toRscUrl(window.location.pathname + window.location.search), {
3040
- method: "POST",
3041
- headers: { "x-rsc-action": id },
3042
- body,
3043
- });
3044
-
3045
- // Check for redirect signal from server action that called redirect()
3046
- const actionRedirect = fetchResponse.headers.get("x-action-redirect");
3047
- if (actionRedirect) {
3048
- // External URLs (different origin) need a hard redirect — client-side
3049
- // RSC navigation only works for same-origin paths.
3050
- try {
3051
- const redirectUrl = new URL(actionRedirect, window.location.origin);
3052
- if (redirectUrl.origin !== window.location.origin) {
3053
- window.location.href = actionRedirect;
3054
- return undefined;
3055
- }
3056
- } catch {
3057
- // If URL parsing fails, fall through to client-side navigation
3058
- }
3059
-
3060
- // Navigate to the redirect target using client-side navigation
3061
- const redirectType = fetchResponse.headers.get("x-action-redirect-type") || "replace";
3062
- if (redirectType === "push") {
3063
- window.history.pushState(null, "", actionRedirect);
3064
- } else {
3065
- window.history.replaceState(null, "", actionRedirect);
3066
- }
3067
- // Trigger RSC navigation to the redirect target
3068
- if (typeof window.__VINEXT_RSC_NAVIGATE__ === "function") {
3069
- window.__VINEXT_RSC_NAVIGATE__(actionRedirect);
3070
- }
3071
- return undefined;
3072
- }
3073
-
3074
- const result = await createFromFetch(Promise.resolve(fetchResponse), { temporaryReferences });
3075
-
3076
- // The RSC response for actions contains { root, returnValue }.
3077
- // Re-render the page with the updated tree.
3078
- if (result && typeof result === "object" && "root" in result) {
3079
- reactRoot.render(result.root);
3080
- // Return the action's return value to the caller
3081
- if (result.returnValue) {
3082
- if (!result.returnValue.ok) throw result.returnValue.data;
3083
- return result.returnValue.data;
3084
- }
3085
- return undefined;
3086
- }
3087
-
3088
- // Fallback: render the entire result as the tree
3089
- reactRoot.render(result);
3090
- return result;
3091
- });
3092
-
3093
- async function main() {
3094
- let rscStream;
3095
-
3096
- // Use embedded RSC data for initial hydration if available.
3097
- // This ensures we use the SAME RSC payload that generated the HTML,
3098
- // avoiding hydration mismatches (React error #418).
3099
- //
3100
- // The server embeds RSC chunks progressively as <script> tags that push
3101
- // to self.__VINEXT_RSC_CHUNKS__. When complete, self.__VINEXT_RSC_DONE__
3102
- // is set and self.__VINEXT_RSC_PARAMS__ contains route params.
3103
- // For backwards compat, also check the legacy self.__VINEXT_RSC__ format.
3104
- if (self.__VINEXT_RSC_CHUNKS__ || self.__VINEXT_RSC_DONE__ || self.__VINEXT_RSC__) {
3105
- if (self.__VINEXT_RSC__) {
3106
- // Legacy format: single object with all chunks
3107
- const embedData = self.__VINEXT_RSC__;
3108
- delete self.__VINEXT_RSC__;
3109
- if (embedData.params) {
3110
- setClientParams(embedData.params);
3111
- }
3112
- rscStream = chunksToReadableStream(embedData.rsc);
3113
- } else {
3114
- // Progressive format: chunks arrive incrementally via script tags.
3115
- // Params are embedded in <head> so they're always available by this point.
3116
- if (self.__VINEXT_RSC_PARAMS__) {
3117
- setClientParams(self.__VINEXT_RSC_PARAMS__);
3118
- }
3119
- rscStream = createProgressiveRscStream();
3120
- }
3121
- } else {
3122
- // Fallback: fetch fresh RSC (shouldn't happen on initial page load)
3123
- const rscResponse = await fetch(toRscUrl(window.location.pathname + window.location.search));
3124
-
3125
- // Hydrate useParams() with route params from the server before React hydration
3126
- const paramsHeader = rscResponse.headers.get("X-Vinext-Params");
3127
- if (paramsHeader) {
3128
- try { setClientParams(JSON.parse(paramsHeader)); } catch (_e) { /* ignore */ }
3129
- }
3130
-
3131
- rscStream = rscResponse.body;
3132
- }
3133
-
3134
- const root = await createFromReadableStream(rscStream);
3135
-
3136
- // Hydrate the document
3137
- // In development, suppress Vite's error overlay for errors caught by React error
3138
- // boundaries. Without this, React re-throws caught errors to the global handler,
3139
- // which triggers Vite's overlay even though the error was handled by an error.tsx.
3140
- // In production, preserve React's default onCaughtError (console.error) so
3141
- // boundary-caught errors remain visible to error monitoring.
3142
- reactRoot = hydrateRoot(document, root, import.meta.env.DEV ? {
3143
- onCaughtError: function() {},
3144
- } : undefined);
3145
-
3146
- // Store for client-side navigation
3147
- window.__VINEXT_RSC_ROOT__ = reactRoot;
3148
-
3149
- // Client-side navigation handler
3150
- // Checks the prefetch cache (populated by <Link> IntersectionObserver and
3151
- // router.prefetch()) before making a network request. This makes navigation
3152
- // near-instant for prefetched routes.
3153
- window.__VINEXT_RSC_NAVIGATE__ = async function navigateRsc(href, __redirectDepth) {
3154
- if ((__redirectDepth || 0) > 10) {
3155
- console.error("[vinext] Too many RSC redirects — aborting navigation to prevent infinite loop.");
3156
- window.location.href = href;
3157
- return;
3158
- }
3159
- try {
3160
- const url = new URL(href, window.location.origin);
3161
- const rscUrl = toRscUrl(url.pathname + url.search);
3162
-
3163
- // Check the in-memory prefetch cache first
3164
- let navResponse;
3165
- const prefetchCache = getPrefetchCache();
3166
- const cached = prefetchCache.get(rscUrl);
3167
- if (cached && (Date.now() - cached.timestamp) < PREFETCH_CACHE_TTL) {
3168
- navResponse = cached.response;
3169
- prefetchCache.delete(rscUrl); // Consume the cached entry (one-time use)
3170
- getPrefetchedUrls().delete(rscUrl); // Allow re-prefetch when link is visible again
3171
- } else if (cached) {
3172
- prefetchCache.delete(rscUrl); // Expired, clean up
3173
- getPrefetchedUrls().delete(rscUrl);
3174
- }
3175
-
3176
- // Fallback to network fetch if not in cache
3177
- if (!navResponse) {
3178
- navResponse = await fetch(rscUrl, {
3179
- headers: { Accept: "text/x-component" },
3180
- credentials: "include",
3181
- });
3182
- }
3183
-
3184
- // Detect if fetch followed a redirect: compare the final response URL to
3185
- // what we requested. If they differ, the server issued a 3xx — push the
3186
- // canonical destination URL into history before rendering.
3187
- const __finalUrl = new URL(navResponse.url);
3188
- const __requestedUrl = new URL(rscUrl, window.location.origin);
3189
- if (__finalUrl.pathname !== __requestedUrl.pathname) {
3190
- // Strip .rsc suffix from the final URL to get the page path for history.
3191
- // Use replaceState instead of pushState: the caller (navigateImpl) already
3192
- // pushed the pre-redirect URL; replacing it avoids a stale history entry.
3193
- const __destPath = __finalUrl.pathname.replace(/\\.rsc$/, "") + __finalUrl.search;
3194
- window.history.replaceState(null, "", __destPath);
3195
- return window.__VINEXT_RSC_NAVIGATE__(__destPath, (__redirectDepth || 0) + 1);
3196
- }
3197
-
3198
- // Update useParams() with route params from the server before re-rendering
3199
- const navParamsHeader = navResponse.headers.get("X-Vinext-Params");
3200
- if (navParamsHeader) {
3201
- try { setClientParams(JSON.parse(navParamsHeader)); } catch (_e) { /* ignore */ }
3202
- } else {
3203
- setClientParams({});
3204
- }
3205
-
3206
- const rscPayload = await createFromFetch(Promise.resolve(navResponse));
3207
- // Use flushSync to guarantee React commits the new tree to the DOM
3208
- // synchronously before this function returns. Callers scroll to top
3209
- // after awaiting, so the new content must be painted first.
3210
- flushSync(function () { reactRoot.render(rscPayload); });
3211
- } catch (err) {
3212
- console.error("[vinext] RSC navigation error:", err);
3213
- // Fallback to full page load
3214
- window.location.href = href;
3215
- }
3216
- };
3217
-
3218
- // Handle popstate (browser back/forward)
3219
- // Store the navigation promise on a well-known property so that
3220
- // restoreScrollPosition (in navigation.ts) can await it before scrolling.
3221
- // This prevents a flash where the old content is visible at the restored
3222
- // scroll position before the new RSC payload has rendered.
3223
- window.addEventListener("popstate", () => {
3224
- const p = window.__VINEXT_RSC_NAVIGATE__(window.location.href);
3225
- window.__VINEXT_RSC_PENDING__ = p;
3226
- p.finally(() => {
3227
- // Clear once settled so stale promises aren't awaited later
3228
- if (window.__VINEXT_RSC_PENDING__ === p) {
3229
- window.__VINEXT_RSC_PENDING__ = null;
3230
- }
3231
- });
3232
- });
3233
-
3234
- // HMR: re-render on server module updates
3235
- if (import.meta.hot) {
3236
- import.meta.hot.on("rsc:update", async () => {
3237
- try {
3238
- const rscPayload = await createFromFetch(
3239
- fetch(toRscUrl(window.location.pathname + window.location.search))
3240
- );
3241
- reactRoot.render(rscPayload);
3242
- } catch (err) {
3243
- console.error("[vinext] RSC HMR error:", err);
3244
- }
3245
- });
3246
- }
3247
- }
3248
-
3249
- main();
3250
- `;
3251
- }
3252
- //# sourceMappingURL=app-dev-server.js.map
2407
+ //# sourceMappingURL=app-rsc-entry.js.map