catalyst-core 1.0.2 → 1.0.3-canary.1
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.
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type React from "react";
|
|
1
2
|
import type { Location, NavigateFunction, Params, RouteMatch, RouteObject } from "react-router";
|
|
2
3
|
/**
|
|
3
4
|
* Internal context shared among the router components. `matchedRoutes` is the
|
|
@@ -26,4 +27,4 @@ export interface OneMgRouterContextValue {
|
|
|
26
27
|
/**
|
|
27
28
|
* @description This is internal context made for sharing data among router components
|
|
28
29
|
*/
|
|
29
|
-
export declare const OneMgRouterContext:
|
|
30
|
+
export declare const OneMgRouterContext: React.Context<OneMgRouterContextValue>;
|
|
@@ -17,7 +17,16 @@ import { createContext } from "react";
|
|
|
17
17
|
* internal context a subpath's own hook calls can't see.
|
|
18
18
|
*/
|
|
19
19
|
|
|
20
|
+
// One context per JS realm, not per module record. A production build can load
|
|
21
|
+
// this file twice: the app's server bundle inlines catalyst-core's main entry
|
|
22
|
+
// while `catalyst-core/webmcp` is loaded by Node at runtime (or the reverse),
|
|
23
|
+
// and each copy's own `createContext()` would be a different context. Provider
|
|
24
|
+
// and consumer then never meet and `useRouterState()` throws on every page.
|
|
25
|
+
// Keying the context on the global symbol registry makes every copy share it.
|
|
26
|
+
const CONTEXT_KEY = Symbol.for("catalyst-core.OneMgRouterContext");
|
|
27
|
+
const globalScope = globalThis;
|
|
28
|
+
|
|
20
29
|
/**
|
|
21
30
|
* @description This is internal context made for sharing data among router components
|
|
22
31
|
*/
|
|
23
|
-
export const OneMgRouterContext = createContext({});
|
|
32
|
+
export const OneMgRouterContext = globalScope[CONTEXT_KEY] ??= createContext({});
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { describe, expect, it } from "vitest"
|
|
1
|
+
import { describe, expect, it, vi } from "vitest"
|
|
2
2
|
import { OneMgRouterContext } from "./context.jsx"
|
|
3
3
|
|
|
4
4
|
describe("OneMgRouterContext", () => {
|
|
@@ -7,4 +7,15 @@ describe("OneMgRouterContext", () => {
|
|
|
7
7
|
expect(OneMgRouterContext.Consumer).toBeDefined()
|
|
8
8
|
expect(OneMgRouterContext._currentValue).toEqual({})
|
|
9
9
|
})
|
|
10
|
+
|
|
11
|
+
// Regression for #550: a production build can load this module twice (the
|
|
12
|
+
// app's server bundle inlines it, catalyst-core/webmcp loads it from
|
|
13
|
+
// disk). A per-module createContext() gave each copy its own context, so
|
|
14
|
+
// useRouterState() threw on every page under `catalyst serve`.
|
|
15
|
+
it("is the same context object across separate module instances", async () => {
|
|
16
|
+
const first = (await import("./context.jsx")).OneMgRouterContext
|
|
17
|
+
vi.resetModules()
|
|
18
|
+
const second = (await import("./context.jsx")).OneMgRouterContext
|
|
19
|
+
expect(second).toBe(first)
|
|
20
|
+
})
|
|
10
21
|
})
|