@ivogt/rsc-router 0.0.0-experimental.2 → 0.0.0-experimental.3

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 (2) hide show
  1. package/package.json +1 -1
  2. package/src/vite/index.ts +44 -4
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ivogt/rsc-router",
3
- "version": "0.0.0-experimental.2",
3
+ "version": "0.0.0-experimental.3",
4
4
  "type": "module",
5
5
  "description": "Type-safe RSC router with partial rendering support",
6
6
  "author": "Ivo Todorov",
package/src/vite/index.ts CHANGED
@@ -21,6 +21,31 @@ export { exposeLocationStateId } from "./expose-location-state-id.ts";
21
21
 
22
22
  // Virtual module type declarations in ./version.d.ts
23
23
 
24
+ /**
25
+ * Modules that must be excluded from Vite's dependency optimization.
26
+ *
27
+ * When rsc-router is installed from npm, Vite's dep optimizer bundles these modules
28
+ * into separate chunks. However, @vitejs/plugin-rsc creates virtual proxy modules
29
+ * for client components that import from the original source paths.
30
+ *
31
+ * This creates two different module instances:
32
+ * 1. Bundled version in /node_modules/.vite/deps/
33
+ * 2. Original source via plugin-rsc proxy
34
+ *
35
+ * When both instances create React Contexts (e.g., OutletContext), React sees them
36
+ * as different contexts, causing useContext to return undefined even when a Provider
37
+ * exists in the tree.
38
+ *
39
+ * By excluding these modules, we ensure a single module instance is used everywhere.
40
+ */
41
+ const RSC_ROUTER_EXCLUDE_DEPS = [
42
+ "@ivogt/rsc-router",
43
+ "@ivogt/rsc-router/browser",
44
+ "@ivogt/rsc-router/client",
45
+ "@ivogt/rsc-router/internal/deps/browser",
46
+ "@ivogt/rsc-router/internal/deps/html-stream-client",
47
+ ];
48
+
24
49
  /**
25
50
  * Plugin to transform CJS react-server-dom vendor file to ESM.
26
51
  * The @vitejs/plugin-rsc package ships client.browser.js as CommonJS
@@ -455,6 +480,11 @@ export async function rscRouter(
455
480
  config() {
456
481
  // Configure environments for cloudflare deployment
457
482
  return {
483
+ // Exclude rsc-router modules from optimization to prevent module duplication
484
+ // This ensures the same Context instance is used by both browser entry and RSC proxy modules
485
+ optimizeDeps: {
486
+ exclude: RSC_ROUTER_EXCLUDE_DEPS,
487
+ },
458
488
  resolve: {
459
489
  alias: {
460
490
  // Map rsc-router/* to @ivogt/rsc-router/* for virtual entries
@@ -486,8 +516,10 @@ export async function rscRouter(
486
516
  },
487
517
  },
488
518
  // Pre-bundle rsc-html-stream to prevent discovery during first request
519
+ // Exclude rsc-router modules to ensure same Context instance
489
520
  optimizeDeps: {
490
521
  include: ["rsc-html-stream/client"],
522
+ exclude: RSC_ROUTER_EXCLUDE_DEPS,
491
523
  },
492
524
  },
493
525
  ssr: {
@@ -569,6 +601,11 @@ export async function rscRouter(
569
601
  const useVirtualRSC = finalEntries.rsc === VIRTUAL_IDS.rsc;
570
602
 
571
603
  return {
604
+ // Exclude rsc-router modules from optimization to prevent module duplication
605
+ // This ensures the same Context instance is used by both browser entry and RSC proxy modules
606
+ optimizeDeps: {
607
+ exclude: RSC_ROUTER_EXCLUDE_DEPS,
608
+ },
572
609
  resolve: {
573
610
  alias: {
574
611
  // Map rsc-router/* to @ivogt/rsc-router/* for virtual entries
@@ -599,12 +636,14 @@ export async function rscRouter(
599
636
  },
600
637
  },
601
638
  },
602
- ...(useVirtualClient && {
603
- optimizeDeps: {
639
+ // Always exclude rsc-router modules, conditionally add virtual entry
640
+ optimizeDeps: {
641
+ exclude: RSC_ROUTER_EXCLUDE_DEPS,
642
+ ...(useVirtualClient && {
604
643
  // Tell Vite to scan the virtual entry for dependencies
605
644
  entries: [VIRTUAL_IDS.browser],
606
- },
607
- }),
645
+ }),
646
+ },
608
647
  },
609
648
  ...(useVirtualSSR && {
610
649
  ssr: {
@@ -612,6 +651,7 @@ export async function rscRouter(
612
651
  entries: [VIRTUAL_IDS.ssr],
613
652
  // Pre-bundle React for SSR to ensure single instance
614
653
  include: ["react", "react-dom/server.edge", "react/jsx-runtime"],
654
+ exclude: RSC_ROUTER_EXCLUDE_DEPS,
615
655
  },
616
656
  },
617
657
  }),