vinext 0.0.27 → 0.0.29

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 (151) hide show
  1. package/dist/build/report.d.ts +117 -0
  2. package/dist/build/report.d.ts.map +1 -0
  3. package/dist/build/report.js +303 -0
  4. package/dist/build/report.js.map +1 -0
  5. package/dist/build/static-export.d.ts +1 -1
  6. package/dist/build/static-export.d.ts.map +1 -1
  7. package/dist/build/static-export.js +2 -1
  8. package/dist/build/static-export.js.map +1 -1
  9. package/dist/cli.js +106 -9
  10. package/dist/cli.js.map +1 -1
  11. package/dist/cloudflare/kv-cache-handler.d.ts +28 -17
  12. package/dist/cloudflare/kv-cache-handler.d.ts.map +1 -1
  13. package/dist/cloudflare/kv-cache-handler.js +109 -42
  14. package/dist/cloudflare/kv-cache-handler.js.map +1 -1
  15. package/dist/cloudflare/tpr.d.ts +10 -0
  16. package/dist/cloudflare/tpr.d.ts.map +1 -1
  17. package/dist/cloudflare/tpr.js +36 -41
  18. package/dist/cloudflare/tpr.js.map +1 -1
  19. package/dist/config/config-matchers.d.ts +1 -0
  20. package/dist/config/config-matchers.d.ts.map +1 -1
  21. package/dist/config/config-matchers.js +51 -23
  22. package/dist/config/config-matchers.js.map +1 -1
  23. package/dist/config/next-config.d.ts.map +1 -1
  24. package/dist/config/next-config.js +16 -0
  25. package/dist/config/next-config.js.map +1 -1
  26. package/dist/deploy.d.ts +1 -1
  27. package/dist/deploy.d.ts.map +1 -1
  28. package/dist/deploy.js +48 -32
  29. package/dist/deploy.js.map +1 -1
  30. package/dist/entries/app-rsc-entry.d.ts +3 -1
  31. package/dist/entries/app-rsc-entry.d.ts.map +1 -1
  32. package/dist/entries/app-rsc-entry.js +514 -99
  33. package/dist/entries/app-rsc-entry.js.map +1 -1
  34. package/dist/entries/pages-server-entry.d.ts.map +1 -1
  35. package/dist/entries/pages-server-entry.js +154 -58
  36. package/dist/entries/pages-server-entry.js.map +1 -1
  37. package/dist/index.d.ts +40 -7
  38. package/dist/index.d.ts.map +1 -1
  39. package/dist/index.js +239 -79
  40. package/dist/index.js.map +1 -1
  41. package/dist/plugins/client-reference-dedup.d.ts +19 -0
  42. package/dist/plugins/client-reference-dedup.d.ts.map +1 -0
  43. package/dist/plugins/client-reference-dedup.js +96 -0
  44. package/dist/plugins/client-reference-dedup.js.map +1 -0
  45. package/dist/routing/app-router.d.ts +2 -0
  46. package/dist/routing/app-router.d.ts.map +1 -1
  47. package/dist/routing/app-router.js +145 -161
  48. package/dist/routing/app-router.js.map +1 -1
  49. package/dist/routing/pages-router.d.ts +1 -1
  50. package/dist/routing/pages-router.d.ts.map +1 -1
  51. package/dist/routing/pages-router.js +37 -65
  52. package/dist/routing/pages-router.js.map +1 -1
  53. package/dist/routing/route-trie.d.ts +57 -0
  54. package/dist/routing/route-trie.d.ts.map +1 -0
  55. package/dist/routing/route-trie.js +160 -0
  56. package/dist/routing/route-trie.js.map +1 -0
  57. package/dist/routing/route-validation.d.ts +8 -0
  58. package/dist/routing/route-validation.d.ts.map +1 -0
  59. package/dist/routing/route-validation.js +136 -0
  60. package/dist/routing/route-validation.js.map +1 -0
  61. package/dist/routing/utils.d.ts +19 -0
  62. package/dist/routing/utils.d.ts.map +1 -1
  63. package/dist/routing/utils.js +47 -0
  64. package/dist/routing/utils.js.map +1 -1
  65. package/dist/server/api-handler.d.ts.map +1 -1
  66. package/dist/server/api-handler.js +52 -20
  67. package/dist/server/api-handler.js.map +1 -1
  68. package/dist/server/dev-server.d.ts.map +1 -1
  69. package/dist/server/dev-server.js +67 -9
  70. package/dist/server/dev-server.js.map +1 -1
  71. package/dist/server/image-optimization.d.ts.map +1 -1
  72. package/dist/server/image-optimization.js +1 -1
  73. package/dist/server/image-optimization.js.map +1 -1
  74. package/dist/server/instrumentation.d.ts.map +1 -1
  75. package/dist/server/instrumentation.js +17 -8
  76. package/dist/server/instrumentation.js.map +1 -1
  77. package/dist/server/isr-cache.d.ts +5 -13
  78. package/dist/server/isr-cache.d.ts.map +1 -1
  79. package/dist/server/isr-cache.js +13 -12
  80. package/dist/server/isr-cache.js.map +1 -1
  81. package/dist/server/metadata-routes.d.ts +8 -2
  82. package/dist/server/metadata-routes.d.ts.map +1 -1
  83. package/dist/server/metadata-routes.js +73 -28
  84. package/dist/server/metadata-routes.js.map +1 -1
  85. package/dist/server/middleware-codegen.d.ts +11 -1
  86. package/dist/server/middleware-codegen.d.ts.map +1 -1
  87. package/dist/server/middleware-codegen.js +204 -12
  88. package/dist/server/middleware-codegen.js.map +1 -1
  89. package/dist/server/middleware.d.ts +9 -8
  90. package/dist/server/middleware.d.ts.map +1 -1
  91. package/dist/server/middleware.js +76 -14
  92. package/dist/server/middleware.js.map +1 -1
  93. package/dist/server/prod-server.d.ts +8 -2
  94. package/dist/server/prod-server.d.ts.map +1 -1
  95. package/dist/server/prod-server.js +144 -74
  96. package/dist/server/prod-server.js.map +1 -1
  97. package/dist/shims/cache.d.ts +2 -0
  98. package/dist/shims/cache.d.ts.map +1 -1
  99. package/dist/shims/cache.js +20 -8
  100. package/dist/shims/cache.js.map +1 -1
  101. package/dist/shims/fetch-cache.d.ts.map +1 -1
  102. package/dist/shims/fetch-cache.js +5 -2
  103. package/dist/shims/fetch-cache.js.map +1 -1
  104. package/dist/shims/form.d.ts.map +1 -1
  105. package/dist/shims/form.js +103 -8
  106. package/dist/shims/form.js.map +1 -1
  107. package/dist/shims/headers.d.ts +11 -3
  108. package/dist/shims/headers.d.ts.map +1 -1
  109. package/dist/shims/headers.js +182 -30
  110. package/dist/shims/headers.js.map +1 -1
  111. package/dist/shims/internal/parse-cookie-header.d.ts +12 -0
  112. package/dist/shims/internal/parse-cookie-header.d.ts.map +1 -0
  113. package/dist/shims/internal/parse-cookie-header.js +32 -0
  114. package/dist/shims/internal/parse-cookie-header.js.map +1 -0
  115. package/dist/shims/link.d.ts +2 -1
  116. package/dist/shims/link.d.ts.map +1 -1
  117. package/dist/shims/link.js +19 -45
  118. package/dist/shims/link.js.map +1 -1
  119. package/dist/shims/metadata.d.ts +56 -0
  120. package/dist/shims/metadata.d.ts.map +1 -1
  121. package/dist/shims/metadata.js +66 -0
  122. package/dist/shims/metadata.js.map +1 -1
  123. package/dist/shims/navigation.d.ts +5 -7
  124. package/dist/shims/navigation.d.ts.map +1 -1
  125. package/dist/shims/navigation.js +61 -39
  126. package/dist/shims/navigation.js.map +1 -1
  127. package/dist/shims/readonly-url-search-params.d.ts +11 -0
  128. package/dist/shims/readonly-url-search-params.d.ts.map +1 -0
  129. package/dist/shims/readonly-url-search-params.js +24 -0
  130. package/dist/shims/readonly-url-search-params.js.map +1 -0
  131. package/dist/shims/router.d.ts +4 -3
  132. package/dist/shims/router.d.ts.map +1 -1
  133. package/dist/shims/router.js +55 -48
  134. package/dist/shims/router.js.map +1 -1
  135. package/dist/shims/server.d.ts +1 -1
  136. package/dist/shims/server.d.ts.map +1 -1
  137. package/dist/shims/server.js +7 -13
  138. package/dist/shims/server.js.map +1 -1
  139. package/dist/shims/url-utils.d.ts +20 -6
  140. package/dist/shims/url-utils.d.ts.map +1 -1
  141. package/dist/shims/url-utils.js +79 -0
  142. package/dist/shims/url-utils.js.map +1 -1
  143. package/dist/utils/manifest-paths.d.ts +4 -0
  144. package/dist/utils/manifest-paths.d.ts.map +1 -0
  145. package/dist/utils/manifest-paths.js +20 -0
  146. package/dist/utils/manifest-paths.js.map +1 -0
  147. package/dist/utils/query.d.ts +9 -0
  148. package/dist/utils/query.d.ts.map +1 -1
  149. package/dist/utils/query.js +59 -9
  150. package/dist/utils/query.js.map +1 -1
  151. package/package.json +2 -2
package/dist/index.js CHANGED
@@ -10,6 +10,7 @@ import { createDirectRunner } from "./server/dev-module-runner.js";
10
10
  import { generateRscEntry } from "./entries/app-rsc-entry.js";
11
11
  import { generateSsrEntry } from "./entries/app-ssr-entry.js";
12
12
  import { generateBrowserEntry } from "./entries/app-browser-entry.js";
13
+ import { normalizePathnameForRouteMatchStrict } from "./routing/utils.js";
13
14
  import { loadNextConfig, resolveNextConfig, } from "./config/next-config.js";
14
15
  import { findMiddlewareFile, runMiddleware } from "./server/middleware.js";
15
16
  import { logRequest, now } from "./server/request-log.js";
@@ -21,8 +22,10 @@ import { isExternalUrl, proxyExternalRequest, matchHeaders, matchRedirect, match
21
22
  import { scanMetadataFiles } from "./server/metadata-routes.js";
22
23
  import { buildRequestHeadersFromMiddlewareResponse } from "./server/middleware-request-headers.js";
23
24
  import { detectPackageManager } from "./utils/project.js";
25
+ import { manifestFileWithBase, manifestFilesWithBase, normalizeManifestFile, } from "./utils/manifest-paths.js";
24
26
  import { hasBasePath } from "./utils/base-path.js";
25
27
  import { asyncHooksStubPlugin } from "./plugins/async-hooks-stub.js";
28
+ import { clientReferenceDedupPlugin } from "./plugins/client-reference-dedup.js";
26
29
  import { hasWranglerConfig, formatMissingCloudflarePluginError } from "./deploy.js";
27
30
  import tsconfigPaths from "vite-tsconfig-paths";
28
31
  import react from "@vitejs/plugin-react";
@@ -225,6 +228,12 @@ const POSTCSS_CONFIG_FILES = [
225
228
  ".postcssrc.yaml",
226
229
  ".postcssrc.yml",
227
230
  ];
231
+ /**
232
+ * Module-level cache for resolvePostcssStringPlugins — avoids re-scanning per Vite environment.
233
+ * Stores the Promise itself so concurrent calls (RSC/SSR/Client config() hooks firing in
234
+ * parallel) all await the same in-flight scan rather than each starting their own.
235
+ */
236
+ const _postcssCache = new Map();
228
237
  /**
229
238
  * Resolve PostCSS string plugin names in a project's PostCSS config.
230
239
  *
@@ -237,7 +246,14 @@ const POSTCSS_CONFIG_FILES = [
237
246
  * Returns the resolved PostCSS config object to inject into Vite's
238
247
  * `css.postcss`, or `undefined` if no resolution is needed.
239
248
  */
240
- async function resolvePostcssStringPlugins(projectRoot) {
249
+ function resolvePostcssStringPlugins(projectRoot) {
250
+ if (_postcssCache.has(projectRoot))
251
+ return _postcssCache.get(projectRoot);
252
+ const promise = _resolvePostcssStringPluginsUncached(projectRoot);
253
+ _postcssCache.set(projectRoot, promise);
254
+ return promise;
255
+ }
256
+ async function _resolvePostcssStringPluginsUncached(projectRoot) {
241
257
  // Find the PostCSS config file
242
258
  let configPath = null;
243
259
  for (const name of POSTCSS_CONFIG_FILES) {
@@ -247,8 +263,9 @@ async function resolvePostcssStringPlugins(projectRoot) {
247
263
  break;
248
264
  }
249
265
  }
250
- if (!configPath)
266
+ if (!configPath) {
251
267
  return undefined;
268
+ }
252
269
  // Load the config file
253
270
  let config;
254
271
  try {
@@ -275,11 +292,13 @@ async function resolvePostcssStringPlugins(projectRoot) {
275
292
  }
276
293
  // Only process array-form plugins that contain string entries
277
294
  // (either bare strings or tuple form ["plugin-name", { options }])
278
- if (!config || !Array.isArray(config.plugins))
295
+ if (!config || !Array.isArray(config.plugins)) {
279
296
  return undefined;
297
+ }
280
298
  const hasStringPlugins = config.plugins.some((p) => typeof p === "string" || (Array.isArray(p) && typeof p[0] === "string"));
281
- if (!hasStringPlugins)
299
+ if (!hasStringPlugins) {
282
300
  return undefined;
301
+ }
283
302
  // Resolve string plugin names to actual plugin functions
284
303
  const req = createRequire(path.join(projectRoot, "package.json"));
285
304
  const resolved = await Promise.all(config.plugins.filter(Boolean).map(async (plugin) => {
@@ -502,7 +521,59 @@ function computeLazyChunks(buildManifest) {
502
521
  }
503
522
  return lazyChunks;
504
523
  }
524
+ function normalizeManifestModuleId(moduleId, root) {
525
+ const normalizedId = moduleId.replace(/\\/g, "/");
526
+ const isWindowsAbsolute = /^[a-zA-Z]:[\\/]/.test(moduleId) || moduleId.startsWith("\\\\");
527
+ if (isWindowsAbsolute) {
528
+ const relativeId = path.win32.relative(root, moduleId).replace(/\\/g, "/");
529
+ if (!relativeId || relativeId.startsWith("../"))
530
+ return normalizedId;
531
+ return relativeId;
532
+ }
533
+ if (!path.isAbsolute(moduleId))
534
+ return normalizedId;
535
+ const relativeId = path.relative(root, moduleId).replace(/\\/g, "/");
536
+ if (!relativeId || relativeId.startsWith("../"))
537
+ return normalizedId;
538
+ return relativeId;
539
+ }
540
+ function augmentSsrManifestFromBundle(ssrManifest, bundle, root, base = "/") {
541
+ const nextManifest = Object.fromEntries(Object.entries(ssrManifest).map(([key, files]) => [
542
+ key,
543
+ new Set(files.map((file) => normalizeManifestFile(file))),
544
+ ]));
545
+ for (const item of Object.values(bundle)) {
546
+ if (item.type !== "chunk")
547
+ continue;
548
+ const chunk = item;
549
+ const files = new Set();
550
+ files.add(manifestFileWithBase(chunk.fileName, base));
551
+ for (const importedFile of chunk.imports ?? []) {
552
+ files.add(manifestFileWithBase(importedFile, base));
553
+ }
554
+ for (const cssFile of chunk.viteMetadata?.importedCss ?? []) {
555
+ files.add(manifestFileWithBase(cssFile, base));
556
+ }
557
+ for (const assetFile of chunk.viteMetadata?.importedAssets ?? []) {
558
+ files.add(manifestFileWithBase(assetFile, base));
559
+ }
560
+ for (const moduleId of Object.keys(chunk.modules ?? {})) {
561
+ const key = normalizeManifestModuleId(moduleId, root);
562
+ if (key.startsWith("node_modules/") || key.includes("/node_modules/"))
563
+ continue;
564
+ if (key.startsWith("\0"))
565
+ continue;
566
+ if (!nextManifest[key])
567
+ nextManifest[key] = new Set();
568
+ for (const file of files) {
569
+ nextManifest[key].add(file);
570
+ }
571
+ }
572
+ }
573
+ return Object.fromEntries(Object.entries(nextManifest).map(([key, files]) => [key, [...files]]));
574
+ }
505
575
  export default function vinext(options = {}) {
576
+ const viteMajorVersion = getViteMajorVersion();
506
577
  let root;
507
578
  let pagesDir;
508
579
  let appDir;
@@ -518,6 +589,11 @@ export default function vinext(options = {}) {
518
589
  const shimsDir = path.resolve(__dirname, "shims");
519
590
  // Shim alias map — populated in config(), used by resolveId() for .js variants
520
591
  let nextShimMap = {};
592
+ // Build-only cache for og-inline-fetch-assets to avoid repeated file reads
593
+ // during a single production build. Dev mode skips the cache so asset edits
594
+ // are picked up without restarting the Vite server.
595
+ const _ogInlineCache = new Map();
596
+ let _ogInlineIsBuild = false;
521
597
  /**
522
598
  * Generate the virtual SSR server entry module.
523
599
  * This is the entry point for `vite build --ssr`.
@@ -597,7 +673,8 @@ export default function vinext(options = {}) {
597
673
  const plugins = [
598
674
  // Resolve tsconfig paths/baseUrl aliases so real-world Next.js repos
599
675
  // that use @/*, #/*, or baseUrl imports work out of the box.
600
- tsconfigPaths(),
676
+ // Vite 8+ supports this natively via resolve.tsconfigPaths.
677
+ ...(viteMajorVersion >= 8 ? [] : [tsconfigPaths()]),
601
678
  // React Fast Refresh + JSX transform for client components.
602
679
  reactPlugin,
603
680
  // Transform CJS require()/module.exports to ESM before other plugins
@@ -608,6 +685,8 @@ export default function vinext(options = {}) {
608
685
  enforce: "pre",
609
686
  async config(config, env) {
610
687
  root = config.root ?? process.cwd();
688
+ const userResolve = config.resolve;
689
+ const shouldEnableNativeTsconfigPaths = viteMajorVersion >= 8 && userResolve?.tsconfigPaths === undefined;
611
690
  // Load .env files into process.env before anything else.
612
691
  // Next.js loads .env files before evaluating next.config.js, so
613
692
  // env vars are available in config, server-side code, and as
@@ -711,34 +790,58 @@ export default function vinext(options = {}) {
711
790
  defines["process.env.__VINEXT_DRAFT_SECRET"] = JSON.stringify(crypto.randomUUID());
712
791
  // Build ID — resolved from next.config generateBuildId() or random UUID.
713
792
  // Exposed so server entries and the next/server shim can inject it.
793
+ // Also used to namespace ISR cache keys so old cached entries from a
794
+ // previous deploy are never served by the new one.
714
795
  defines["process.env.__VINEXT_BUILD_ID"] = JSON.stringify(nextConfig.buildId);
715
- // Build the shim alias map used by both resolve.alias and resolveId
716
- // (resolveId handles .js extension variants for libraries like nuqs)
796
+ // Build the shim alias map. Exact `.js` variants are included for the
797
+ // public Next entrypoints that are file-backed in `next/package.json`.
798
+ // Some libraries (for example `nuqs`) import `next/navigation.js`
799
+ // directly; aliasing the `.js` form ensures optimizeDeps pre-bundles
800
+ // vinext's shim instead of real Next.
717
801
  nextShimMap = {
718
802
  ...nextConfig.aliases,
719
803
  "next/link": path.join(shimsDir, "link"),
804
+ "next/link.js": path.join(shimsDir, "link"),
720
805
  "next/head": path.join(shimsDir, "head"),
806
+ "next/head.js": path.join(shimsDir, "head"),
721
807
  "next/router": path.join(shimsDir, "router"),
808
+ "next/router.js": path.join(shimsDir, "router"),
722
809
  "next/compat/router": path.join(shimsDir, "compat-router"),
810
+ "next/compat/router.js": path.join(shimsDir, "compat-router"),
723
811
  "next/image": path.join(shimsDir, "image"),
812
+ "next/image.js": path.join(shimsDir, "image"),
724
813
  "next/legacy/image": path.join(shimsDir, "legacy-image"),
814
+ "next/legacy/image.js": path.join(shimsDir, "legacy-image"),
725
815
  "next/dynamic": path.join(shimsDir, "dynamic"),
816
+ "next/dynamic.js": path.join(shimsDir, "dynamic"),
726
817
  "next/app": path.join(shimsDir, "app"),
818
+ "next/app.js": path.join(shimsDir, "app"),
727
819
  "next/document": path.join(shimsDir, "document"),
820
+ "next/document.js": path.join(shimsDir, "document"),
728
821
  "next/config": path.join(shimsDir, "config"),
729
822
  "next/script": path.join(shimsDir, "script"),
823
+ "next/script.js": path.join(shimsDir, "script"),
730
824
  "next/server": path.join(shimsDir, "server"),
825
+ "next/server.js": path.join(shimsDir, "server"),
731
826
  "next/navigation": path.join(shimsDir, "navigation"),
827
+ "next/navigation.js": path.join(shimsDir, "navigation"),
732
828
  "next/headers": path.join(shimsDir, "headers"),
829
+ "next/headers.js": path.join(shimsDir, "headers"),
733
830
  "next/font/google": path.join(shimsDir, "font-google"),
734
831
  "next/font/local": path.join(shimsDir, "font-local"),
735
832
  "next/cache": path.join(shimsDir, "cache"),
833
+ "next/cache.js": path.join(shimsDir, "cache"),
736
834
  "next/form": path.join(shimsDir, "form"),
835
+ "next/form.js": path.join(shimsDir, "form"),
737
836
  "next/og": path.join(shimsDir, "og"),
837
+ "next/og.js": path.join(shimsDir, "og"),
738
838
  "next/web-vitals": path.join(shimsDir, "web-vitals"),
839
+ "next/web-vitals.js": path.join(shimsDir, "web-vitals"),
739
840
  "next/amp": path.join(shimsDir, "amp"),
740
841
  "next/error": path.join(shimsDir, "error"),
842
+ "next/error.js": path.join(shimsDir, "error"),
741
843
  "next/constants": path.join(shimsDir, "constants"),
844
+ "next/constants.js": path.join(shimsDir, "constants"),
742
845
  // Internal next/dist/* paths used by popular libraries
743
846
  // (next-intl, @clerk/nextjs, @sentry/nextjs, next-nprogress-bar, etc.)
744
847
  "next/dist/shared/lib/app-router-context.shared-runtime": path.join(shimsDir, "internal", "app-router-context"),
@@ -931,6 +1034,7 @@ export default function vinext(options = {}) {
931
1034
  // causing cryptic "Invalid hook call" errors. This is a no-op
932
1035
  // when only one copy exists.
933
1036
  dedupe: ["react", "react-dom", "react/jsx-runtime", "react/jsx-dev-runtime"],
1037
+ ...(shouldEnableNativeTsconfigPaths ? { tsconfigPaths: true } : {}),
934
1038
  },
935
1039
  // Exclude vinext from dependency optimization so esbuild doesn't
936
1040
  // scan dist files containing virtual module imports (virtual:vinext-*)
@@ -943,7 +1047,7 @@ export default function vinext(options = {}) {
943
1047
  },
944
1048
  // Enable JSX in .tsx/.jsx files
945
1049
  // Vite 7 uses `esbuild` for transforms, Vite 8+ uses `oxc`
946
- ...(getViteMajorVersion() >= 8
1050
+ ...(viteMajorVersion >= 8
947
1051
  ? { oxc: { jsx: { runtime: "automatic" } } }
948
1052
  : { esbuild: { jsx: "automatic" } }),
949
1053
  // Define env vars for client bundle
@@ -1044,6 +1148,10 @@ export default function vinext(options = {}) {
1044
1148
  consumer: "client",
1045
1149
  optimizeDeps: {
1046
1150
  exclude: ["vinext"],
1151
+ // Crawl app/ source files up front so client-only deps imported
1152
+ // by user components are discovered during startup instead of
1153
+ // triggering a late re-optimisation + full page reload.
1154
+ entries: appEntries,
1047
1155
  // React packages aren't crawled from app/ source files,
1048
1156
  // so must be pre-included to avoid late discovery (#25).
1049
1157
  include: [
@@ -1138,18 +1246,6 @@ export default function vinext(options = {}) {
1138
1246
  // ID (with \0 prefix). We need to re-resolve it so the client
1139
1247
  // environment's import-analysis can find it.
1140
1248
  const cleanId = id.startsWith("\0") ? id.slice(1) : id;
1141
- // Handle next/* imports with .js extension (e.g. "next/navigation.js")
1142
- // Libraries like nuqs import "next/navigation.js" which doesn't match
1143
- // our resolve.alias for "next/navigation". Strip the .js and resolve
1144
- // through our shim map, appending .js to the resolved path.
1145
- if (cleanId.startsWith("next/") && cleanId.endsWith(".js")) {
1146
- const withoutExt = cleanId.slice(0, -3);
1147
- if (nextShimMap[withoutExt]) {
1148
- const shimPath = nextShimMap[withoutExt];
1149
- // Alias values don't include .js — append it for resolveId
1150
- return shimPath.endsWith(".js") ? shimPath : shimPath + ".js";
1151
- }
1152
- }
1153
1249
  // Pages Router virtual modules
1154
1250
  if (cleanId === VIRTUAL_SERVER_ENTRY)
1155
1251
  return RESOLVED_SERVER_ENTRY;
@@ -1205,6 +1301,7 @@ export default function vinext(options = {}) {
1205
1301
  allowedOrigins: nextConfig?.serverActionsAllowedOrigins,
1206
1302
  allowedDevOrigins: nextConfig?.allowedDevOrigins,
1207
1303
  bodySizeLimit: nextConfig?.serverActionsBodySizeLimit,
1304
+ i18n: nextConfig?.i18n,
1208
1305
  }, instrumentationPath);
1209
1306
  }
1210
1307
  if (id === RESOLVED_APP_SSR_ENTRY && hasAppDir) {
@@ -1217,6 +1314,8 @@ export default function vinext(options = {}) {
1217
1314
  },
1218
1315
  // Stub node:async_hooks in client builds — see src/plugins/async-hooks-stub.ts
1219
1316
  asyncHooksStubPlugin,
1317
+ // Dedup client references from RSC proxy modules — see src/plugins/client-reference-dedup.ts
1318
+ clientReferenceDedupPlugin(),
1220
1319
  // Proxy plugin for @mdx-js/rollup. The real MDX plugin is created lazily
1221
1320
  // during vinext:config's config() (when MDX files are detected), but
1222
1321
  // plugins returned from config() hooks run too late in the pipeline —
@@ -1637,7 +1736,7 @@ export default function vinext(options = {}) {
1637
1736
  // decodeURIComponent prevents /%61dmin bypassing /admin matchers.
1638
1737
  // normalizePath collapses // and resolves . / .. segments.
1639
1738
  try {
1640
- pathname = normalizePath(decodeURIComponent(pathname));
1739
+ pathname = normalizePath(normalizePathnameForRouteMatchStrict(pathname));
1641
1740
  }
1642
1741
  catch {
1643
1742
  // Malformed percent-encoding (e.g. /%E0%A4%A) — return 400 instead of crashing.
@@ -1685,6 +1784,30 @@ export default function vinext(options = {}) {
1685
1784
  return;
1686
1785
  }
1687
1786
  }
1787
+ // When @cloudflare/vite-plugin is present, delegate the entire
1788
+ // Pages Router request pipeline to the Worker/miniflare side.
1789
+ // That keeps middleware, headers, redirects, rewrites, API
1790
+ // routes, and rendering in one place instead of mutating the
1791
+ // host request and forwarding post-middleware state downstream.
1792
+ if (hasCloudflarePlugin)
1793
+ return next();
1794
+ // Snapshot of req.headers before middleware runs. Used for both
1795
+ // preMiddlewareReqCtx and the middleware Request itself. Intentionally
1796
+ // captured once here — applyRequestHeadersToNodeRequest() mutates
1797
+ // req.headers later, but by then this Headers object is no longer read.
1798
+ const nodeRequestHeaders = new Headers(Object.fromEntries(Object.entries(req.headers)
1799
+ .filter(([, v]) => v !== undefined)
1800
+ .map(([k, v]) => [k, Array.isArray(v) ? v.join(", ") : String(v)])));
1801
+ const requestOrigin = `http://${req.headers.host || "localhost"}`;
1802
+ const preMiddlewareReqUrl = new URL(url, requestOrigin);
1803
+ const preMiddlewareReqCtx = requestContextFromRequest(new Request(preMiddlewareReqUrl, { headers: nodeRequestHeaders }));
1804
+ // Config redirects run before middleware, but still match against
1805
+ // the original normalized pathname and request headers/cookies.
1806
+ if (nextConfig?.redirects.length) {
1807
+ const redirected = applyRedirects(pathname, res, nextConfig.redirects, preMiddlewareReqCtx, nextConfig.basePath ?? "");
1808
+ if (redirected)
1809
+ return;
1810
+ }
1688
1811
  const applyRequestHeadersToNodeRequest = (nextRequestHeaders) => {
1689
1812
  for (const key of Object.keys(req.headers)) {
1690
1813
  delete req.headers[key];
@@ -1708,11 +1831,9 @@ export default function vinext(options = {}) {
1708
1831
  const origin = `${mwProto}://${req.headers.host || "localhost"}`;
1709
1832
  const middlewareRequest = new Request(new URL(url, origin), {
1710
1833
  method: req.method,
1711
- headers: Object.fromEntries(Object.entries(req.headers)
1712
- .filter(([, v]) => v !== undefined)
1713
- .map(([k, v]) => [k, Array.isArray(v) ? v.join(", ") : String(v)])),
1834
+ headers: nodeRequestHeaders,
1714
1835
  });
1715
- const result = await runMiddleware(getPagesRunner(), middlewarePath, middlewareRequest);
1836
+ const result = await runMiddleware(getPagesRunner(), middlewarePath, middlewareRequest, nextConfig?.i18n);
1716
1837
  if (!result.continue) {
1717
1838
  if (result.redirectUrl) {
1718
1839
  const redirectHeaders = {
@@ -1741,7 +1862,7 @@ export default function vinext(options = {}) {
1741
1862
  for (const [key, value] of result.response.headers) {
1742
1863
  res.appendHeader(key, value);
1743
1864
  }
1744
- const body = await result.response.text();
1865
+ const body = Buffer.from(await result.response.arrayBuffer());
1745
1866
  res.end(body);
1746
1867
  return;
1747
1868
  }
@@ -1787,46 +1908,19 @@ export default function vinext(options = {}) {
1787
1908
  req.__vinextRewriteStatus = result.rewriteStatus;
1788
1909
  }
1789
1910
  }
1790
- // ── Cloudflare Workers dev mode ────────────────────────────
1791
- // When @cloudflare/vite-plugin is present, ALL rendering runs
1792
- // inside the miniflare Worker subprocess — both App Router (via
1793
- // virtual:vinext-rsc-entry) and Pages Router (via
1794
- // virtual:vinext-server-entry → renderPage/handleApiRoute).
1795
- //
1796
- // The Worker entry already handles config redirects, rewrites,
1797
- // headers, and all routing internally. Running them here too
1798
- // would duplicate that logic and produce incorrect behaviour
1799
- // (double redirects, headers set on the wrong object, etc.).
1800
- //
1801
- // Middleware.ts is the only thing that belongs in the host connect
1802
- // handler — it has already run above. Any terminal middleware
1803
- // result (redirect, block response) has already been sent.
1804
- // Any rewrite has been written back to req.url above so the
1805
- // Cloudflare plugin's handler sees the correct path.
1806
- //
1807
- // Call next() to hand off to the Cloudflare plugin's connect
1808
- // handler, which dispatches the request to miniflare.
1809
- if (hasCloudflarePlugin)
1810
- return next();
1811
1911
  // Build request context once for has/missing condition checks
1812
- // across headers, redirects, and rewrites.
1912
+ // for config rules that execute after middleware (rewrites).
1813
1913
  // Convert Node.js IncomingMessage headers to a Web Request for
1814
1914
  // requestContextFromRequest(), which uses the standard Web API.
1815
- const reqUrl = new URL(url, `http://${req.headers.host || "localhost"}`);
1816
- const reqCtxHeaders = middlewareRequestHeaders ??
1817
- new Headers(Object.fromEntries(Object.entries(req.headers)
1818
- .filter(([, v]) => v !== undefined)
1819
- .map(([k, v]) => [k, Array.isArray(v) ? v.join(", ") : String(v)])));
1915
+ const reqUrl = new URL(url, requestOrigin);
1916
+ const reqCtxHeaders = middlewareRequestHeaders ?? nodeRequestHeaders;
1820
1917
  const reqCtx = requestContextFromRequest(new Request(reqUrl, { headers: reqCtxHeaders }));
1821
1918
  // Apply custom headers from next.config.js
1919
+ // Header matching still uses the original normalized pathname and
1920
+ // pre-middleware request state; middleware response headers win
1921
+ // later because they are already on the outgoing response.
1822
1922
  if (nextConfig?.headers.length) {
1823
- applyHeaders(pathname, res, nextConfig.headers, reqCtx);
1824
- }
1825
- // Apply redirects from next.config.js
1826
- if (nextConfig?.redirects.length) {
1827
- const redirected = applyRedirects(pathname, res, nextConfig.redirects, reqCtx, nextConfig.basePath ?? "");
1828
- if (redirected)
1829
- return;
1923
+ applyHeaders(pathname, res, nextConfig.headers, preMiddlewareReqCtx);
1830
1924
  }
1831
1925
  // Apply rewrites from next.config.js (beforeFiles)
1832
1926
  let resolvedUrl = url;
@@ -2409,11 +2503,20 @@ export default function vinext(options = {}) {
2409
2503
  {
2410
2504
  name: "vinext:og-inline-fetch-assets",
2411
2505
  enforce: "pre",
2412
- transform(code, id) {
2506
+ configResolved(config) {
2507
+ _ogInlineIsBuild = config.command === "build";
2508
+ },
2509
+ buildStart() {
2510
+ if (_ogInlineIsBuild) {
2511
+ _ogInlineCache.clear();
2512
+ }
2513
+ },
2514
+ async transform(code, id) {
2413
2515
  // Quick bail-out: only process modules that use new URL(..., import.meta.url)
2414
2516
  if (!code.includes("import.meta.url")) {
2415
2517
  return null;
2416
2518
  }
2519
+ const useCache = _ogInlineIsBuild;
2417
2520
  const moduleDir = path.dirname(id);
2418
2521
  let newCode = code;
2419
2522
  let didReplace = false;
@@ -2425,13 +2528,19 @@ export default function vinext(options = {}) {
2425
2528
  const fullMatch = match[0];
2426
2529
  const relPath = match[2]; // e.g. "./noto-sans-v27-latin-regular.ttf"
2427
2530
  const absPath = path.resolve(moduleDir, relPath);
2428
- let fileBase64;
2429
- try {
2430
- fileBase64 = fs.readFileSync(absPath).toString("base64");
2431
- }
2432
- catch {
2433
- // File not found on disk — skip (may be a runtime-only asset)
2434
- continue;
2531
+ let fileBase64 = useCache ? _ogInlineCache.get(absPath) : undefined;
2532
+ if (fileBase64 === undefined) {
2533
+ try {
2534
+ const buf = await fs.promises.readFile(absPath);
2535
+ fileBase64 = buf.toString("base64");
2536
+ if (useCache) {
2537
+ _ogInlineCache.set(absPath, fileBase64);
2538
+ }
2539
+ }
2540
+ catch {
2541
+ // File not found on disk — skip (may be a runtime-only asset)
2542
+ continue;
2543
+ }
2435
2544
  }
2436
2545
  // Replace fetch(...).then(...) with an inline IIFE that returns Promise<ArrayBuffer>.
2437
2546
  const inlined = [
@@ -2456,13 +2565,19 @@ export default function vinext(options = {}) {
2456
2565
  const fullMatch = match[0];
2457
2566
  const relPath = match[2]; // e.g. "./noto-sans-v27-latin-regular.ttf"
2458
2567
  const absPath = path.resolve(moduleDir, relPath);
2459
- let fileBase64;
2460
- try {
2461
- fileBase64 = fs.readFileSync(absPath).toString("base64");
2462
- }
2463
- catch {
2464
- // File not found on disk — skip
2465
- continue;
2568
+ let fileBase64 = useCache ? _ogInlineCache.get(absPath) : undefined;
2569
+ if (fileBase64 === undefined) {
2570
+ try {
2571
+ const buf = await fs.promises.readFile(absPath);
2572
+ fileBase64 = buf.toString("base64");
2573
+ if (useCache) {
2574
+ _ogInlineCache.set(absPath, fileBase64);
2575
+ }
2576
+ }
2577
+ catch {
2578
+ // File not found on disk — skip
2579
+ continue;
2580
+ }
2466
2581
  }
2467
2582
  // Replace readFileSync(...) with Buffer.from("<base64>", "base64").
2468
2583
  // Buffer is always available in Node.js and in the vinext SSR/RSC environments.
@@ -2561,6 +2676,38 @@ export default function vinext(options = {}) {
2561
2676
  },
2562
2677
  },
2563
2678
  },
2679
+ // Vite can emit empty SSR manifest entries for modules that Rollup inlines
2680
+ // into another chunk. Pages Router looks up assets by page module path at
2681
+ // runtime, so rebuild those mappings from the emitted client bundle.
2682
+ {
2683
+ name: "vinext:ssr-manifest-backfill",
2684
+ apply: "build",
2685
+ enforce: "post",
2686
+ writeBundle: {
2687
+ sequential: true,
2688
+ order: "post",
2689
+ handler(options, bundle) {
2690
+ const outDir = options.dir;
2691
+ if (!outDir)
2692
+ return;
2693
+ const viteDir = path.join(outDir, ".vite");
2694
+ const ssrManifestPath = path.join(viteDir, "ssr-manifest.json");
2695
+ if (!fs.existsSync(ssrManifestPath))
2696
+ return;
2697
+ try {
2698
+ const ssrManifest = JSON.parse(fs.readFileSync(ssrManifestPath, "utf-8"));
2699
+ const buildRoot = this.environment?.config.root ?? process.cwd();
2700
+ const buildBase = this.environment?.config.base ?? "/";
2701
+ const augmentedManifest = augmentSsrManifestFromBundle(ssrManifest, bundle, buildRoot, buildBase);
2702
+ fs.writeFileSync(ssrManifestPath, JSON.stringify(augmentedManifest, null, 2));
2703
+ }
2704
+ catch (err) {
2705
+ // Leave Vite's manifest untouched if parsing fails.
2706
+ console.warn("[vinext] Failed to augment SSR manifest:", err);
2707
+ }
2708
+ },
2709
+ },
2710
+ },
2564
2711
  // Cloudflare Workers production build integration:
2565
2712
  // After all environments are built, compute lazy chunks from the client
2566
2713
  // build manifest and inject globals into the worker entry.
@@ -2592,6 +2739,7 @@ export default function vinext(options = {}) {
2592
2739
  if (!fs.existsSync(distDir))
2593
2740
  return;
2594
2741
  const clientDir = path.resolve(buildRoot, "dist", "client");
2742
+ const clientBase = envConfig.base ?? "/";
2595
2743
  // Read build manifest and compute lazy chunks (only reachable via
2596
2744
  // dynamic imports). This runs for BOTH App Router and Pages Router.
2597
2745
  // clientEntryFile is only used by the Pages Router path below —
@@ -2604,11 +2752,11 @@ export default function vinext(options = {}) {
2604
2752
  const buildManifest = JSON.parse(fs.readFileSync(buildManifestPath, "utf-8"));
2605
2753
  for (const [, value] of Object.entries(buildManifest)) {
2606
2754
  if (value && value.isEntry && value.file) {
2607
- clientEntryFile = value.file;
2755
+ clientEntryFile = manifestFileWithBase(value.file, clientBase);
2608
2756
  break;
2609
2757
  }
2610
2758
  }
2611
- const lazy = computeLazyChunks(buildManifest);
2759
+ const lazy = manifestFilesWithBase(computeLazyChunks(buildManifest), clientBase);
2612
2760
  if (lazy.length > 0)
2613
2761
  lazyChunksData = lazy;
2614
2762
  }
@@ -2675,7 +2823,7 @@ export default function vinext(options = {}) {
2675
2823
  const entry = files.find((f) => (f.includes("vinext-client-entry") || f.includes("vinext-app-browser-entry")) &&
2676
2824
  f.endsWith(".js"));
2677
2825
  if (entry)
2678
- clientEntryFile = "assets/" + entry;
2826
+ clientEntryFile = manifestFileWithBase("assets/" + entry, clientBase);
2679
2827
  }
2680
2828
  }
2681
2829
  // Prepend globals to worker entry
@@ -3018,15 +3166,23 @@ function findFileWithExts(dir, name, matcher) {
3018
3166
  }
3019
3167
  return null;
3020
3168
  }
3169
+ /** Module-level cache for hasMdxFiles — avoids re-scanning per Vite environment. */
3170
+ const _mdxScanCache = new Map();
3021
3171
  /**
3022
3172
  * Check if the project has .mdx files in app/ or pages/ directories.
3023
3173
  */
3024
3174
  function hasMdxFiles(root, appDir, pagesDir) {
3175
+ const cacheKey = `${root}\0${appDir ?? ""}\0${pagesDir ?? ""}`;
3176
+ if (_mdxScanCache.has(cacheKey))
3177
+ return _mdxScanCache.get(cacheKey);
3025
3178
  const dirs = [appDir, pagesDir].filter(Boolean);
3026
3179
  for (const dir of dirs) {
3027
- if (fs.existsSync(dir) && scanDirForMdx(dir))
3180
+ if (fs.existsSync(dir) && scanDirForMdx(dir)) {
3181
+ _mdxScanCache.set(cacheKey, true);
3028
3182
  return true;
3183
+ }
3029
3184
  }
3185
+ _mdxScanCache.set(cacheKey, false);
3030
3186
  return false;
3031
3187
  }
3032
3188
  function scanDirForMdx(dir) {
@@ -3054,7 +3210,11 @@ function scanDirForMdx(dir) {
3054
3210
  export { staticExportPages, staticExportApp } from "./build/static-export.js";
3055
3211
  // Exported for CLI and testing
3056
3212
  export { clientManualChunks, clientOutputConfig, clientTreeshakeConfig, computeLazyChunks };
3213
+ export { augmentSsrManifestFromBundle as _augmentSsrManifestFromBundle };
3057
3214
  export { resolvePostcssStringPlugins as _resolvePostcssStringPlugins };
3215
+ export { _postcssCache };
3216
+ export { hasMdxFiles as _hasMdxFiles };
3217
+ export { _mdxScanCache };
3058
3218
  export { parseStaticObjectLiteral as _parseStaticObjectLiteral };
3059
3219
  export { stripServerExports as _stripServerExports };
3060
3220
  export { asyncHooksStubPlugin as _asyncHooksStubPlugin };