@bleedingdev/modern-js-plugin-tanstack 3.2.0-ultramodern.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/dist/cjs/cli/index.js +268 -0
- package/dist/cjs/cli/tanstackTypes.js +388 -0
- package/dist/cjs/cli.js +65 -0
- package/dist/cjs/runtime/DefaultNotFound.js +47 -0
- package/dist/cjs/runtime/basepathRewrite.js +62 -0
- package/dist/cjs/runtime/dataMutation.js +345 -0
- package/dist/cjs/runtime/hooks.js +57 -0
- package/dist/cjs/runtime/index.js +114 -0
- package/dist/cjs/runtime/lifecycle.js +125 -0
- package/dist/cjs/runtime/plugin.js +250 -0
- package/dist/cjs/runtime/plugin.node.js +304 -0
- package/dist/cjs/runtime/prefetchLink.js +55 -0
- package/dist/cjs/runtime/routeTree.js +492 -0
- package/dist/cjs/runtime/rsc/ClientSlot.js +53 -0
- package/dist/cjs/runtime/rsc/CompositeComponent.js +75 -0
- package/dist/cjs/runtime/rsc/ReplayableStream.js +141 -0
- package/dist/cjs/runtime/rsc/RscNodeRenderer.js +65 -0
- package/dist/cjs/runtime/rsc/SlotContext.js +54 -0
- package/dist/cjs/runtime/rsc/client.js +93 -0
- package/dist/cjs/runtime/rsc/createRscProxy.js +141 -0
- package/dist/cjs/runtime/rsc/index.js +42 -0
- package/dist/cjs/runtime/rsc/payloadRouter.js +211 -0
- package/dist/cjs/runtime/rsc/server.js +246 -0
- package/dist/cjs/runtime/rsc/slotUsageSanitizer.js +65 -0
- package/dist/cjs/runtime/rsc/symbols.js +72 -0
- package/dist/cjs/runtime/types.js +18 -0
- package/dist/cjs/runtime/utils.js +142 -0
- package/dist/cjs/runtime.js +58 -0
- package/dist/esm/cli/index.mjs +201 -0
- package/dist/esm/cli/tanstackTypes.mjs +341 -0
- package/dist/esm/cli.mjs +2 -0
- package/dist/esm/rslib-runtime.mjs +18 -0
- package/dist/esm/runtime/DefaultNotFound.mjs +13 -0
- package/dist/esm/runtime/basepathRewrite.mjs +28 -0
- package/dist/esm/runtime/dataMutation.mjs +305 -0
- package/dist/esm/runtime/hooks.mjs +8 -0
- package/dist/esm/runtime/index.mjs +6 -0
- package/dist/esm/runtime/lifecycle.mjs +82 -0
- package/dist/esm/runtime/plugin.mjs +214 -0
- package/dist/esm/runtime/plugin.node.mjs +268 -0
- package/dist/esm/runtime/prefetchLink.mjs +18 -0
- package/dist/esm/runtime/routeTree.mjs +452 -0
- package/dist/esm/runtime/rsc/ClientSlot.mjs +19 -0
- package/dist/esm/runtime/rsc/CompositeComponent.mjs +41 -0
- package/dist/esm/runtime/rsc/ReplayableStream.mjs +104 -0
- package/dist/esm/runtime/rsc/RscNodeRenderer.mjs +31 -0
- package/dist/esm/runtime/rsc/SlotContext.mjs +17 -0
- package/dist/esm/runtime/rsc/client.mjs +53 -0
- package/dist/esm/runtime/rsc/createRscProxy.mjs +107 -0
- package/dist/esm/runtime/rsc/index.mjs +1 -0
- package/dist/esm/runtime/rsc/payloadRouter.mjs +162 -0
- package/dist/esm/runtime/rsc/server.mjs +200 -0
- package/dist/esm/runtime/rsc/slotUsageSanitizer.mjs +31 -0
- package/dist/esm/runtime/rsc/symbols.mjs +17 -0
- package/dist/esm/runtime/types.mjs +0 -0
- package/dist/esm/runtime/utils.mjs +89 -0
- package/dist/esm/runtime.mjs +1 -0
- package/dist/esm-node/cli/index.mjs +205 -0
- package/dist/esm-node/cli/tanstackTypes.mjs +342 -0
- package/dist/esm-node/cli.mjs +3 -0
- package/dist/esm-node/rslib-runtime.mjs +19 -0
- package/dist/esm-node/runtime/DefaultNotFound.mjs +14 -0
- package/dist/esm-node/runtime/basepathRewrite.mjs +29 -0
- package/dist/esm-node/runtime/dataMutation.mjs +306 -0
- package/dist/esm-node/runtime/hooks.mjs +9 -0
- package/dist/esm-node/runtime/index.mjs +7 -0
- package/dist/esm-node/runtime/lifecycle.mjs +83 -0
- package/dist/esm-node/runtime/plugin.mjs +215 -0
- package/dist/esm-node/runtime/plugin.node.mjs +269 -0
- package/dist/esm-node/runtime/prefetchLink.mjs +19 -0
- package/dist/esm-node/runtime/routeTree.mjs +453 -0
- package/dist/esm-node/runtime/rsc/ClientSlot.mjs +20 -0
- package/dist/esm-node/runtime/rsc/CompositeComponent.mjs +42 -0
- package/dist/esm-node/runtime/rsc/ReplayableStream.mjs +105 -0
- package/dist/esm-node/runtime/rsc/RscNodeRenderer.mjs +32 -0
- package/dist/esm-node/runtime/rsc/SlotContext.mjs +18 -0
- package/dist/esm-node/runtime/rsc/client.mjs +54 -0
- package/dist/esm-node/runtime/rsc/createRscProxy.mjs +108 -0
- package/dist/esm-node/runtime/rsc/index.mjs +2 -0
- package/dist/esm-node/runtime/rsc/payloadRouter.mjs +163 -0
- package/dist/esm-node/runtime/rsc/server.mjs +201 -0
- package/dist/esm-node/runtime/rsc/slotUsageSanitizer.mjs +32 -0
- package/dist/esm-node/runtime/rsc/symbols.mjs +18 -0
- package/dist/esm-node/runtime/types.mjs +1 -0
- package/dist/esm-node/runtime/utils.mjs +90 -0
- package/dist/esm-node/runtime.mjs +2 -0
- package/dist/types/cli/index.d.ts +20 -0
- package/dist/types/cli/tanstackTypes.d.ts +11 -0
- package/dist/types/cli.d.ts +2 -0
- package/dist/types/runtime/DefaultNotFound.d.ts +2 -0
- package/dist/types/runtime/basepathRewrite.d.ts +8 -0
- package/dist/types/runtime/dataMutation.d.ts +29 -0
- package/dist/types/runtime/hooks.d.ts +18 -0
- package/dist/types/runtime/index.d.ts +9 -0
- package/dist/types/runtime/lifecycle.d.ts +22 -0
- package/dist/types/runtime/plugin.d.ts +17 -0
- package/dist/types/runtime/plugin.node.d.ts +17 -0
- package/dist/types/runtime/prefetchLink.d.ts +11 -0
- package/dist/types/runtime/routeTree.d.ts +11 -0
- package/dist/types/runtime/rsc/ClientSlot.d.ts +5 -0
- package/dist/types/runtime/rsc/CompositeComponent.d.ts +3 -0
- package/dist/types/runtime/rsc/ReplayableStream.d.ts +24 -0
- package/dist/types/runtime/rsc/RscNodeRenderer.d.ts +5 -0
- package/dist/types/runtime/rsc/SlotContext.d.ts +11 -0
- package/dist/types/runtime/rsc/client.d.ts +11 -0
- package/dist/types/runtime/rsc/createRscProxy.d.ts +7 -0
- package/dist/types/runtime/rsc/index.d.ts +2 -0
- package/dist/types/runtime/rsc/payloadRouter.d.ts +24 -0
- package/dist/types/runtime/rsc/server.d.ts +14 -0
- package/dist/types/runtime/rsc/slotUsageSanitizer.d.ts +2 -0
- package/dist/types/runtime/rsc/symbols.d.ts +46 -0
- package/dist/types/runtime/types.d.ts +68 -0
- package/dist/types/runtime/utils.d.ts +36 -0
- package/dist/types/runtime.d.ts +1 -0
- package/dist/types-direct/cli/index.d.ts +20 -0
- package/dist/types-direct/cli/tanstackTypes.d.ts +11 -0
- package/dist/types-direct/cli.d.ts +2 -0
- package/dist/types-direct/runtime/DefaultNotFound.d.ts +2 -0
- package/dist/types-direct/runtime/basepathRewrite.d.ts +8 -0
- package/dist/types-direct/runtime/dataMutation.d.ts +29 -0
- package/dist/types-direct/runtime/hooks.d.ts +18 -0
- package/dist/types-direct/runtime/index.d.ts +9 -0
- package/dist/types-direct/runtime/lifecycle.d.ts +22 -0
- package/dist/types-direct/runtime/plugin.d.ts +17 -0
- package/dist/types-direct/runtime/plugin.node.d.ts +17 -0
- package/dist/types-direct/runtime/prefetchLink.d.ts +11 -0
- package/dist/types-direct/runtime/routeTree.d.ts +11 -0
- package/dist/types-direct/runtime/rsc/ClientSlot.d.ts +5 -0
- package/dist/types-direct/runtime/rsc/CompositeComponent.d.ts +3 -0
- package/dist/types-direct/runtime/rsc/ReplayableStream.d.ts +24 -0
- package/dist/types-direct/runtime/rsc/RscNodeRenderer.d.ts +5 -0
- package/dist/types-direct/runtime/rsc/SlotContext.d.ts +11 -0
- package/dist/types-direct/runtime/rsc/client.d.ts +11 -0
- package/dist/types-direct/runtime/rsc/createRscProxy.d.ts +7 -0
- package/dist/types-direct/runtime/rsc/index.d.ts +2 -0
- package/dist/types-direct/runtime/rsc/payloadRouter.d.ts +24 -0
- package/dist/types-direct/runtime/rsc/server.d.ts +14 -0
- package/dist/types-direct/runtime/rsc/slotUsageSanitizer.d.ts +2 -0
- package/dist/types-direct/runtime/rsc/symbols.d.ts +46 -0
- package/dist/types-direct/runtime/types.d.ts +68 -0
- package/dist/types-direct/runtime/utils.d.ts +36 -0
- package/dist/types-direct/runtime.d.ts +1 -0
- package/package.json +126 -0
- package/rslib.config.mts +4 -0
- package/rstest.config.mts +43 -0
- package/src/cli/index.ts +388 -0
- package/src/cli/tanstackTypes.ts +503 -0
- package/src/cli.ts +2 -0
- package/src/runtime/DefaultNotFound.tsx +15 -0
- package/src/runtime/basepathRewrite.ts +59 -0
- package/src/runtime/dataMutation.tsx +517 -0
- package/src/runtime/hooks.ts +34 -0
- package/src/runtime/index.tsx +30 -0
- package/src/runtime/lifecycle.ts +150 -0
- package/src/runtime/plugin.node.tsx +534 -0
- package/src/runtime/plugin.tsx +395 -0
- package/src/runtime/prefetchLink.tsx +87 -0
- package/src/runtime/routeTree.ts +942 -0
- package/src/runtime/rsc/ClientSlot.tsx +25 -0
- package/src/runtime/rsc/CompositeComponent.tsx +65 -0
- package/src/runtime/rsc/ReplayableStream.ts +155 -0
- package/src/runtime/rsc/RscNodeRenderer.tsx +45 -0
- package/src/runtime/rsc/SlotContext.tsx +31 -0
- package/src/runtime/rsc/client.tsx +90 -0
- package/src/runtime/rsc/createRscProxy.tsx +189 -0
- package/src/runtime/rsc/index.ts +10 -0
- package/src/runtime/rsc/payloadRouter.ts +318 -0
- package/src/runtime/rsc/server.tsx +303 -0
- package/src/runtime/rsc/slotUsageSanitizer.ts +76 -0
- package/src/runtime/rsc/symbols.ts +106 -0
- package/src/runtime/ssr-shim.d.ts +12 -0
- package/src/runtime/types.ts +83 -0
- package/src/runtime/utils.tsx +161 -0
- package/src/runtime.ts +1 -0
- package/tests/router/cli.test.ts +386 -0
- package/tests/router/dataMutation.test.tsx +396 -0
- package/tests/router/prefetchLink.test.tsx +43 -0
- package/tests/router/routeTree.test.ts +502 -0
- package/tests/router/rsc.test.tsx +256 -0
- package/tests/router/tanstackTypes.test.ts +62 -0
- package/tsconfig.json +12 -0
- package/tsconfig.tsgo.json +6 -0
|
@@ -0,0 +1,502 @@
|
|
|
1
|
+
import type { RouteObject } from '@modern-js/runtime-utils/router';
|
|
2
|
+
import type { NestedRoute } from '@modern-js/types';
|
|
3
|
+
import { createMemoryHistory } from '@tanstack/history';
|
|
4
|
+
import { createRouter } from '@tanstack/react-router';
|
|
5
|
+
import {
|
|
6
|
+
createRouteTreeFromModernRoutes,
|
|
7
|
+
createRouteTreeFromRouteObjects,
|
|
8
|
+
} from '../../src/runtime/routeTree';
|
|
9
|
+
import { __setTanstackRscPayloadDecoderForTests } from '../../src/runtime/rsc/payloadRouter';
|
|
10
|
+
import { createRouteObjectsFromConfig } from '../../src/runtime/utils';
|
|
11
|
+
|
|
12
|
+
type LoaderArgs = {
|
|
13
|
+
params: Record<string, string>;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
type TestRouteObject = RouteObject & {
|
|
17
|
+
children?: TestRouteObject[];
|
|
18
|
+
config?: {
|
|
19
|
+
handle?: Record<string, unknown>;
|
|
20
|
+
};
|
|
21
|
+
hasAction?: boolean;
|
|
22
|
+
hasClientLoader?: boolean;
|
|
23
|
+
hasLoader?: boolean;
|
|
24
|
+
inValidSSRRoute?: boolean;
|
|
25
|
+
isClientComponent?: boolean;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
type TestNestedRoute = NestedRoute & {
|
|
29
|
+
children?: TestNestedRoute[];
|
|
30
|
+
hasAction?: boolean;
|
|
31
|
+
hasClientLoader?: boolean;
|
|
32
|
+
hasLoader?: boolean;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
type ShouldRevalidateArgs = {
|
|
36
|
+
nextUrl: URL;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
type ShouldReloadArgs = {
|
|
40
|
+
context: {
|
|
41
|
+
request: Request;
|
|
42
|
+
};
|
|
43
|
+
location: {
|
|
44
|
+
href: string;
|
|
45
|
+
};
|
|
46
|
+
params: Record<string, string>;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
type TestRoute = {
|
|
50
|
+
options: {
|
|
51
|
+
shouldReload?: (args: ShouldReloadArgs) => boolean | undefined;
|
|
52
|
+
ssr?: boolean;
|
|
53
|
+
staticData: Record<string, unknown>;
|
|
54
|
+
};
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
type TestRouter = {
|
|
58
|
+
load: () => Promise<void>;
|
|
59
|
+
looseRoutesById: Partial<Record<string, TestRoute>>;
|
|
60
|
+
state: {
|
|
61
|
+
matches: Array<{
|
|
62
|
+
loaderData?: unknown;
|
|
63
|
+
routeId: string;
|
|
64
|
+
}>;
|
|
65
|
+
};
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
type TestRouteTree = ReturnType<typeof createRouteTreeFromRouteObjects>;
|
|
69
|
+
|
|
70
|
+
async function loadRouteTree(
|
|
71
|
+
routeTree: TestRouteTree,
|
|
72
|
+
pathname: string,
|
|
73
|
+
): Promise<TestRouter> {
|
|
74
|
+
const router = createRouter({
|
|
75
|
+
routeTree,
|
|
76
|
+
history: createMemoryHistory({
|
|
77
|
+
initialEntries: [pathname],
|
|
78
|
+
}),
|
|
79
|
+
context: {
|
|
80
|
+
request: new Request(`http://localhost${pathname}`),
|
|
81
|
+
requestContext: {},
|
|
82
|
+
},
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
await router.load();
|
|
86
|
+
return router as unknown as TestRouter;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function getLooseRoute(router: TestRouter, id: string): TestRoute {
|
|
90
|
+
const route = router.looseRoutesById[id];
|
|
91
|
+
if (!route) {
|
|
92
|
+
throw new Error(`Expected TanStack route ${id} to exist`);
|
|
93
|
+
}
|
|
94
|
+
return route;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function getLooseRouteByModernRouteId(
|
|
98
|
+
router: TestRouter,
|
|
99
|
+
modernRouteId: string,
|
|
100
|
+
): TestRoute {
|
|
101
|
+
const route = Object.values(router.looseRoutesById).find(
|
|
102
|
+
route => route?.options.staticData?.modernRouteId === modernRouteId,
|
|
103
|
+
);
|
|
104
|
+
if (!route) {
|
|
105
|
+
throw new Error(`Expected Modern route ${modernRouteId} to exist`);
|
|
106
|
+
}
|
|
107
|
+
return route;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
describe('tanstack route tree from RouteObject[]', () => {
|
|
111
|
+
afterEach(() => {
|
|
112
|
+
__setTanstackRscPayloadDecoderForTests();
|
|
113
|
+
rstest.restoreAllMocks();
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
test('maps root loader and dynamic params', async () => {
|
|
117
|
+
const routes: RouteObject[] = [
|
|
118
|
+
{
|
|
119
|
+
id: 'root',
|
|
120
|
+
path: '/',
|
|
121
|
+
loader: () => ({ root: 'ok' }),
|
|
122
|
+
Component: () => null,
|
|
123
|
+
children: [
|
|
124
|
+
{
|
|
125
|
+
id: 'user',
|
|
126
|
+
path: 'user/:id',
|
|
127
|
+
loader: ({ params }: LoaderArgs) => ({ id: params.id }),
|
|
128
|
+
Component: () => null,
|
|
129
|
+
},
|
|
130
|
+
],
|
|
131
|
+
},
|
|
132
|
+
];
|
|
133
|
+
|
|
134
|
+
const routeTree = createRouteTreeFromRouteObjects(routes);
|
|
135
|
+
const router = await loadRouteTree(routeTree, '/user/123');
|
|
136
|
+
|
|
137
|
+
const rootMatch = router.state.matches.find(
|
|
138
|
+
match => match.routeId === '__root__',
|
|
139
|
+
);
|
|
140
|
+
const userMatch = router.state.matches.find(
|
|
141
|
+
match => match.routeId === '/user/$id',
|
|
142
|
+
);
|
|
143
|
+
|
|
144
|
+
expect(rootMatch?.loaderData).toEqual({ root: 'ok' });
|
|
145
|
+
expect(userMatch?.loaderData).toEqual({ id: '123' });
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
test('uses TanStack route ids when loading RSC payload route data', async () => {
|
|
149
|
+
const rootLoader = rstest.fn(() => ({ source: 'modern-root' }));
|
|
150
|
+
const userLoader = rstest.fn(() => ({ source: 'modern-user' }));
|
|
151
|
+
const routes: RouteObject[] = [
|
|
152
|
+
{
|
|
153
|
+
id: 'root',
|
|
154
|
+
path: '/',
|
|
155
|
+
loader: rootLoader,
|
|
156
|
+
Component: () => null,
|
|
157
|
+
children: [
|
|
158
|
+
{
|
|
159
|
+
id: 'user',
|
|
160
|
+
path: 'user/:id',
|
|
161
|
+
loader: userLoader,
|
|
162
|
+
Component: () => null,
|
|
163
|
+
},
|
|
164
|
+
],
|
|
165
|
+
},
|
|
166
|
+
];
|
|
167
|
+
const payload = {
|
|
168
|
+
type: 'render',
|
|
169
|
+
actionData: null,
|
|
170
|
+
errors: null,
|
|
171
|
+
loaderData: {
|
|
172
|
+
__root__: { source: 'rsc-root' },
|
|
173
|
+
'/user/$id': { source: 'rsc-user' },
|
|
174
|
+
},
|
|
175
|
+
location: { href: '/user/123' },
|
|
176
|
+
routes: [
|
|
177
|
+
{ id: '__root__', hasLoader: true },
|
|
178
|
+
{ id: '/user/$id', hasLoader: true },
|
|
179
|
+
],
|
|
180
|
+
};
|
|
181
|
+
const fetchMock = rstest.fn(() => Promise.resolve(new Response('payload')));
|
|
182
|
+
const decodeMock = rstest.fn(async () => payload);
|
|
183
|
+
rstest.stubGlobal('fetch', fetchMock);
|
|
184
|
+
rstest.stubGlobal('window', { origin: 'http://localhost' });
|
|
185
|
+
__setTanstackRscPayloadDecoderForTests(decodeMock);
|
|
186
|
+
|
|
187
|
+
const routeTree = createRouteTreeFromRouteObjects(routes, {
|
|
188
|
+
rscPayloadRouter: true,
|
|
189
|
+
});
|
|
190
|
+
const router = await loadRouteTree(routeTree, '/user/123');
|
|
191
|
+
|
|
192
|
+
const rootMatch = router.state.matches.find(
|
|
193
|
+
match => match.routeId === '__root__',
|
|
194
|
+
);
|
|
195
|
+
const userMatch = router.state.matches.find(
|
|
196
|
+
match => match.routeId === '/user/$id',
|
|
197
|
+
);
|
|
198
|
+
expect(rootMatch?.loaderData).toEqual({ source: 'rsc-root' });
|
|
199
|
+
expect(userMatch?.loaderData).toEqual({ source: 'rsc-user' });
|
|
200
|
+
expect(rootLoader).not.toHaveBeenCalled();
|
|
201
|
+
expect(userLoader).not.toHaveBeenCalled();
|
|
202
|
+
expect(fetchMock).toHaveBeenCalledTimes(1);
|
|
203
|
+
expect(decodeMock).toHaveBeenCalledTimes(1);
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
test('maps splat params', async () => {
|
|
207
|
+
let splatParamValue = '';
|
|
208
|
+
const routes: TestRouteObject[] = [
|
|
209
|
+
{
|
|
210
|
+
id: 'root',
|
|
211
|
+
path: '/',
|
|
212
|
+
Component: () => null,
|
|
213
|
+
children: [
|
|
214
|
+
{
|
|
215
|
+
id: 'files',
|
|
216
|
+
path: 'files/*',
|
|
217
|
+
loader: ({ params }: LoaderArgs) => {
|
|
218
|
+
splatParamValue = String(params['*'] || '');
|
|
219
|
+
return { value: params['*'] };
|
|
220
|
+
},
|
|
221
|
+
Component: () => null,
|
|
222
|
+
},
|
|
223
|
+
],
|
|
224
|
+
},
|
|
225
|
+
];
|
|
226
|
+
|
|
227
|
+
const routeTree = createRouteTreeFromRouteObjects(routes);
|
|
228
|
+
|
|
229
|
+
const splatRouter = await loadRouteTree(routeTree, '/files/a/b/c');
|
|
230
|
+
const filesMatch = splatRouter.state.matches.find(
|
|
231
|
+
match => match.routeId === '/files/$',
|
|
232
|
+
);
|
|
233
|
+
expect(filesMatch?.loaderData).toEqual({ value: 'a/b/c' });
|
|
234
|
+
expect(splatParamValue).toBe('a/b/c');
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
test('preserves route handle and maps shouldRevalidate to shouldReload', async () => {
|
|
238
|
+
const shouldRevalidate = rstest.fn(({ nextUrl }: ShouldRevalidateArgs) =>
|
|
239
|
+
nextUrl.pathname.endsWith('/456'),
|
|
240
|
+
);
|
|
241
|
+
const routes: RouteObject[] = [
|
|
242
|
+
{
|
|
243
|
+
id: 'root',
|
|
244
|
+
path: '/',
|
|
245
|
+
config: {
|
|
246
|
+
handle: {
|
|
247
|
+
shell: true,
|
|
248
|
+
},
|
|
249
|
+
},
|
|
250
|
+
Component: () => null,
|
|
251
|
+
children: [
|
|
252
|
+
{
|
|
253
|
+
id: 'user',
|
|
254
|
+
path: 'user/:id',
|
|
255
|
+
handle: { auth: true },
|
|
256
|
+
config: {
|
|
257
|
+
handle: {
|
|
258
|
+
role: 'admin',
|
|
259
|
+
},
|
|
260
|
+
},
|
|
261
|
+
shouldRevalidate,
|
|
262
|
+
loader: ({ params }: LoaderArgs) => ({ id: params.id }),
|
|
263
|
+
Component: () => null,
|
|
264
|
+
},
|
|
265
|
+
],
|
|
266
|
+
},
|
|
267
|
+
];
|
|
268
|
+
|
|
269
|
+
const routeTree = createRouteTreeFromRouteObjects(routes);
|
|
270
|
+
const router = await loadRouteTree(routeTree, '/user/123');
|
|
271
|
+
const userRoute = getLooseRoute(router, '/user/$id');
|
|
272
|
+
|
|
273
|
+
expect(routeTree.options.staticData.modernRouteHandle).toEqual({
|
|
274
|
+
shell: true,
|
|
275
|
+
});
|
|
276
|
+
expect(userRoute.options.staticData.modernRouteHandle).toEqual({
|
|
277
|
+
auth: true,
|
|
278
|
+
role: 'admin',
|
|
279
|
+
});
|
|
280
|
+
expect(userRoute.options.staticData.modernRouteShouldRevalidate).toBe(
|
|
281
|
+
shouldRevalidate,
|
|
282
|
+
);
|
|
283
|
+
expect(
|
|
284
|
+
userRoute.options.shouldReload?.({
|
|
285
|
+
location: { href: '/user/456' },
|
|
286
|
+
params: { id: '456' },
|
|
287
|
+
context: {
|
|
288
|
+
request: new Request('http://localhost/user/456'),
|
|
289
|
+
},
|
|
290
|
+
}),
|
|
291
|
+
).toBe(true);
|
|
292
|
+
expect(shouldRevalidate).toHaveBeenCalledWith(
|
|
293
|
+
expect.objectContaining({
|
|
294
|
+
currentParams: { id: '123' },
|
|
295
|
+
nextParams: { id: '456' },
|
|
296
|
+
}),
|
|
297
|
+
);
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
test('preserves client route metadata and disables invalid SSR routes', async () => {
|
|
301
|
+
const routes: TestRouteObject[] = [
|
|
302
|
+
{
|
|
303
|
+
id: 'root',
|
|
304
|
+
path: '/',
|
|
305
|
+
Component: () => null,
|
|
306
|
+
children: [
|
|
307
|
+
{
|
|
308
|
+
id: 'client',
|
|
309
|
+
path: 'client',
|
|
310
|
+
hasAction: true,
|
|
311
|
+
hasClientLoader: true,
|
|
312
|
+
hasLoader: true,
|
|
313
|
+
inValidSSRRoute: true,
|
|
314
|
+
isClientComponent: true,
|
|
315
|
+
loader: () => ({ ok: true }),
|
|
316
|
+
Component: () => null,
|
|
317
|
+
},
|
|
318
|
+
],
|
|
319
|
+
},
|
|
320
|
+
];
|
|
321
|
+
|
|
322
|
+
const routeTree = createRouteTreeFromRouteObjects(routes);
|
|
323
|
+
const router = await loadRouteTree(routeTree, '/client');
|
|
324
|
+
const clientRoute = getLooseRoute(router, '/client');
|
|
325
|
+
|
|
326
|
+
expect(clientRoute.options.ssr).toBe(false);
|
|
327
|
+
expect(clientRoute.options.staticData).toMatchObject({
|
|
328
|
+
modernRouteHasAction: true,
|
|
329
|
+
modernRouteHasClientLoader: true,
|
|
330
|
+
modernRouteHasLoader: true,
|
|
331
|
+
modernRouteIsClientComponent: true,
|
|
332
|
+
});
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
test('normalizes Modern deferred loader data for TanStack SSR', async () => {
|
|
336
|
+
const routes: TestRouteObject[] = [
|
|
337
|
+
{
|
|
338
|
+
id: 'root',
|
|
339
|
+
path: '/',
|
|
340
|
+
Component: () => null,
|
|
341
|
+
children: [
|
|
342
|
+
{
|
|
343
|
+
id: 'deferred',
|
|
344
|
+
path: 'deferred',
|
|
345
|
+
loader: () => ({
|
|
346
|
+
__modern_deferred: true,
|
|
347
|
+
data: {
|
|
348
|
+
immediate: 'ok',
|
|
349
|
+
later: Promise.resolve('done'),
|
|
350
|
+
},
|
|
351
|
+
}),
|
|
352
|
+
Component: () => null,
|
|
353
|
+
},
|
|
354
|
+
],
|
|
355
|
+
},
|
|
356
|
+
];
|
|
357
|
+
|
|
358
|
+
const routeTree = createRouteTreeFromRouteObjects(routes);
|
|
359
|
+
const router = await loadRouteTree(routeTree, '/deferred');
|
|
360
|
+
const deferredMatch = router.state.matches.find(
|
|
361
|
+
match => match.routeId === '/deferred',
|
|
362
|
+
);
|
|
363
|
+
const loaderData = deferredMatch?.loaderData as
|
|
364
|
+
| { immediate: string; later: Promise<string> }
|
|
365
|
+
| undefined;
|
|
366
|
+
|
|
367
|
+
expect(loaderData?.immediate).toBe('ok');
|
|
368
|
+
await expect(loaderData?.later).resolves.toBe('done');
|
|
369
|
+
});
|
|
370
|
+
|
|
371
|
+
test('merges Modern generated route handle into TanStack static data', () => {
|
|
372
|
+
const modernRoutes: NestedRoute[] = [
|
|
373
|
+
{
|
|
374
|
+
type: 'nested',
|
|
375
|
+
origin: 'config',
|
|
376
|
+
id: 'root',
|
|
377
|
+
isRoot: true,
|
|
378
|
+
config: {
|
|
379
|
+
handle: {
|
|
380
|
+
shell: true,
|
|
381
|
+
},
|
|
382
|
+
},
|
|
383
|
+
children: [
|
|
384
|
+
{
|
|
385
|
+
type: 'nested',
|
|
386
|
+
origin: 'config',
|
|
387
|
+
id: 'dashboard',
|
|
388
|
+
path: 'dashboard',
|
|
389
|
+
handle: {
|
|
390
|
+
section: 'analytics',
|
|
391
|
+
},
|
|
392
|
+
config: {
|
|
393
|
+
handle: {
|
|
394
|
+
role: 'admin',
|
|
395
|
+
},
|
|
396
|
+
},
|
|
397
|
+
},
|
|
398
|
+
],
|
|
399
|
+
},
|
|
400
|
+
];
|
|
401
|
+
const routeTree = createRouteTreeFromModernRoutes(modernRoutes);
|
|
402
|
+
const router = createRouter({
|
|
403
|
+
routeTree,
|
|
404
|
+
history: createMemoryHistory({
|
|
405
|
+
initialEntries: ['/dashboard'],
|
|
406
|
+
}),
|
|
407
|
+
context: {},
|
|
408
|
+
}) as unknown as TestRouter;
|
|
409
|
+
const dashboardRoute = getLooseRoute(router, '/dashboard');
|
|
410
|
+
|
|
411
|
+
expect(routeTree.options.staticData.modernRouteHandle).toEqual({
|
|
412
|
+
shell: true,
|
|
413
|
+
});
|
|
414
|
+
expect(dashboardRoute.options.staticData.modernRouteHandle).toEqual({
|
|
415
|
+
section: 'analytics',
|
|
416
|
+
role: 'admin',
|
|
417
|
+
});
|
|
418
|
+
});
|
|
419
|
+
|
|
420
|
+
test('preserves Modern generated client route metadata', () => {
|
|
421
|
+
const modernRoutes: TestNestedRoute[] = [
|
|
422
|
+
{
|
|
423
|
+
type: 'nested',
|
|
424
|
+
origin: 'config',
|
|
425
|
+
id: 'root',
|
|
426
|
+
isRoot: true,
|
|
427
|
+
children: [
|
|
428
|
+
{
|
|
429
|
+
type: 'nested',
|
|
430
|
+
origin: 'config',
|
|
431
|
+
id: 'client',
|
|
432
|
+
path: 'client',
|
|
433
|
+
clientData: './client.data',
|
|
434
|
+
hasAction: true,
|
|
435
|
+
hasClientLoader: true,
|
|
436
|
+
hasLoader: true,
|
|
437
|
+
inValidSSRRoute: true,
|
|
438
|
+
isClientComponent: true,
|
|
439
|
+
},
|
|
440
|
+
],
|
|
441
|
+
},
|
|
442
|
+
];
|
|
443
|
+
const routeTree = createRouteTreeFromModernRoutes(modernRoutes);
|
|
444
|
+
const router = createRouter({
|
|
445
|
+
routeTree,
|
|
446
|
+
history: createMemoryHistory({
|
|
447
|
+
initialEntries: ['/client'],
|
|
448
|
+
}),
|
|
449
|
+
context: {},
|
|
450
|
+
}) as unknown as TestRouter;
|
|
451
|
+
const clientRoute = getLooseRouteByModernRouteId(router, 'client');
|
|
452
|
+
|
|
453
|
+
expect(clientRoute.options.ssr).toBe(false);
|
|
454
|
+
expect(clientRoute.options.staticData).toMatchObject({
|
|
455
|
+
modernRouteHasAction: true,
|
|
456
|
+
modernRouteHasClientLoader: true,
|
|
457
|
+
modernRouteHasLoader: true,
|
|
458
|
+
modernRouteIsClientComponent: true,
|
|
459
|
+
});
|
|
460
|
+
});
|
|
461
|
+
|
|
462
|
+
test('preserves generated client metadata through RouteObject conversion', () => {
|
|
463
|
+
const modernRoutes: TestNestedRoute[] = [
|
|
464
|
+
{
|
|
465
|
+
type: 'nested',
|
|
466
|
+
origin: 'config',
|
|
467
|
+
id: 'root',
|
|
468
|
+
isRoot: true,
|
|
469
|
+
children: [
|
|
470
|
+
{
|
|
471
|
+
type: 'nested',
|
|
472
|
+
origin: 'config',
|
|
473
|
+
id: 'client',
|
|
474
|
+
path: 'client',
|
|
475
|
+
clientData: './client.data',
|
|
476
|
+
data: './data',
|
|
477
|
+
inValidSSRRoute: true,
|
|
478
|
+
isClientComponent: true,
|
|
479
|
+
},
|
|
480
|
+
],
|
|
481
|
+
},
|
|
482
|
+
];
|
|
483
|
+
const routeObjects = createRouteObjectsFromConfig({
|
|
484
|
+
routesConfig: { routes: modernRoutes },
|
|
485
|
+
});
|
|
486
|
+
const routeTree = createRouteTreeFromRouteObjects(routeObjects || []);
|
|
487
|
+
const router = createRouter({
|
|
488
|
+
routeTree,
|
|
489
|
+
history: createMemoryHistory({
|
|
490
|
+
initialEntries: ['/client'],
|
|
491
|
+
}),
|
|
492
|
+
context: {},
|
|
493
|
+
}) as unknown as TestRouter;
|
|
494
|
+
const clientRoute = getLooseRouteByModernRouteId(router, 'client');
|
|
495
|
+
|
|
496
|
+
expect(clientRoute.options.ssr).toBe(false);
|
|
497
|
+
expect(clientRoute.options.staticData).toMatchObject({
|
|
498
|
+
modernRouteHasClientLoader: true,
|
|
499
|
+
modernRouteIsClientComponent: true,
|
|
500
|
+
});
|
|
501
|
+
});
|
|
502
|
+
});
|