@teyik0/furin 0.5.0-alpha.2 → 0.7.0-alpha.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.
- package/dist/adapter/runtime-build.d.ts +3 -1
- package/dist/build/client.d.ts +4 -1
- package/dist/build/entry-template.d.ts +5 -0
- package/dist/build/entry-template.ts +9 -0
- package/dist/build/hydrate.d.ts +1 -1
- package/dist/build/index.js +9589 -6685
- package/dist/build/preload-manifest.d.ts +17 -0
- package/dist/build/request-keys.d.ts +4 -0
- package/dist/build/types.d.ts +6 -0
- package/dist/cli/index.js +9539 -6644
- package/dist/client/browser-events-runtime.d.ts +2 -1
- package/dist/client/client-module.d.ts +26 -0
- package/dist/client/define-route.d.ts +9 -1
- package/dist/client/dev-error-overlay.ts +1 -0
- package/dist/client/document.d.ts +3 -0
- package/dist/client/hmr.d.ts +1 -1
- package/dist/client/internal/runtime-types.d.ts +2 -0
- package/dist/client/link.js +1011 -157
- package/dist/client/mutation.d.ts +38 -0
- package/dist/client/query-store.d.ts +85 -0
- package/dist/client/query.d.ts +14 -0
- package/dist/client/route-api.d.ts +11 -0
- package/dist/client/router/context.d.ts +3 -2
- package/dist/client/router/index.d.ts +1 -1
- package/dist/client/router/link-utils.d.ts +8 -1
- package/dist/client/router/navigation.d.ts +2 -9
- package/dist/client/router/optimistic.d.ts +41 -0
- package/dist/client/router/search/index.d.ts +3 -3
- package/dist/client/router/search-store.d.ts +1 -0
- package/dist/client/router/sync-catch-up.d.ts +3 -0
- package/dist/client/router/types.d.ts +49 -7
- package/dist/client/sync.d.ts +39 -22
- package/dist/client.d.ts +11 -33
- package/dist/client.js +1222 -57
- package/dist/config.d.ts +7 -0
- package/dist/config.js +1 -0
- package/dist/define-route.d.ts +231 -123
- package/dist/env.d.ts +2 -2
- package/dist/furin.d.ts +4 -2
- package/dist/furin.js +9717 -6796
- package/dist/plugin/hmr-dependencies.d.ts +2 -0
- package/dist/plugin/index.js +811 -145
- package/dist/plugin/transform-client-module.d.ts +16 -0
- package/dist/plugin/transform-client.d.ts +2 -0
- package/dist/plugin/transform-dev-page.d.ts +7 -0
- package/dist/plugin/transform-isomorphic.d.ts +1 -1
- package/dist/routes.d.ts +6 -0
- package/dist/rsc-client.js +87 -16
- package/dist/rsc.js +87 -16
- package/dist/server/auto-invalidate/registry.d.ts +5 -2
- package/dist/server/auto-invalidate/types.d.ts +2 -0
- package/dist/server/browser-events/plugin.d.ts +3 -0
- package/dist/server/cache/isr.d.ts +2 -4
- package/dist/server/cache/route-cache.d.ts +5 -0
- package/dist/server/dev/graph.d.ts +1 -0
- package/dist/server/dev/runtime.production.d.ts +1 -0
- package/dist/server/dev-page-plugin.d.ts +6 -19
- package/dist/server/internal.d.ts +5 -0
- package/dist/server/internal.js +105 -57
- package/dist/server/production-server.js +104 -56
- package/dist/server/render/assemble.d.ts +6 -6
- package/dist/server/render/document.d.ts +1 -1
- package/dist/server/render/element.d.ts +1 -1
- package/dist/server/render/loaders.d.ts +15 -5
- package/dist/server/render/mixed-cache.d.ts +6 -0
- package/dist/server/render/react-stream.d.ts +3 -0
- package/dist/server/render/ssr.d.ts +5 -4
- package/dist/server/render/template.d.ts +4 -0
- package/dist/server/router/defined-route.d.ts +2 -0
- package/dist/server/router/discovery.d.ts +1 -0
- package/dist/server/router/hmr.d.ts +1 -1
- package/dist/server/router/patterns.d.ts +3 -0
- package/dist/server/router/plugin.d.ts +3 -0
- package/dist/server/router/types.d.ts +4 -0
- package/dist/server/security/csp.d.ts +12 -0
- package/dist/server/sync/adapter.d.ts +25 -4
- package/dist/server/sync/atomic.d.ts +25 -0
- package/dist/server/sync/drizzle/index.d.ts +45 -0
- package/dist/server/sync/drizzle/index.js +540 -0
- package/dist/server/sync/execution-error.d.ts +2 -0
- package/dist/server/sync/index.d.ts +2 -1
- package/dist/server/sync/index.js +580 -124
- package/dist/server/sync/mutation-handler.d.ts +4 -0
- package/dist/server/sync/plugin.d.ts +34 -11
- package/dist/server/sync/postgres/execute.d.ts +3 -0
- package/dist/server/sync/postgres/index.d.ts +12 -1
- package/dist/server/sync/postgres/index.js +42 -13
- package/dist/server/sync/postgres/sql.d.ts +8 -0
- package/dist/server/sync/prisma/index.d.ts +24 -0
- package/dist/server/sync/prisma/index.js +340 -0
- package/dist/server/sync/queries.d.ts +21 -0
- package/dist/server/sync/query-context.d.ts +5 -0
- package/dist/server/sync/redis/index.js +3 -3
- package/dist/server/sync/redis/scripts.d.ts +1 -1
- package/dist/server/sync/response.d.ts +1 -0
- package/dist/server/sync/sqlite/index.d.ts +3 -0
- package/dist/server/sync/sqlite/index.js +10 -4
- package/dist/server/sync/stream.d.ts +2 -5
- package/dist/server/sync/validation.d.ts +4 -0
- package/dist/shared/defer.d.ts +31 -0
- package/dist/shared/error.d.ts +2 -2
- package/dist/shared/page-key.d.ts +7 -0
- package/dist/shared/query-bindings.d.ts +12 -0
- package/dist/shared/route-frame.d.ts +1 -0
- package/dist/shared/sync-query.d.ts +40 -0
- package/package.json +40 -16
- package/src/adapter/README.md +3 -0
- package/src/adapter/bun.ts +3 -0
- package/src/adapter/package.ts +16 -2
- package/src/adapter/runtime-build.ts +99 -14
- package/src/adapter/static.ts +17 -6
- package/src/adapter/vercel.ts +33 -23
- package/src/build/client.ts +23 -5
- package/src/build/entry-template.ts +9 -0
- package/src/build/hydrate.ts +18 -3
- package/src/build/preload-manifest.ts +100 -0
- package/src/build/production-instrumentation.ts +6 -3
- package/src/build/request-keys.ts +93 -0
- package/src/build/ssg-cache.ts +9 -4
- package/src/build/types.ts +4 -0
- package/src/cli/index.ts +1 -0
- package/src/client/boundaries.tsx +1 -1
- package/src/client/browser-events-runtime.ts +73 -16
- package/src/client/client-module.ts +57 -0
- package/src/client/define-route.ts +11 -2
- package/src/client/dev-error-overlay.ts +1 -0
- package/src/client/document.tsx +47 -20
- package/src/client/hmr.ts +4 -2
- package/src/client/internal/runtime-types.ts +2 -0
- package/src/client/link.tsx +8 -8
- package/src/client/mutation.ts +136 -0
- package/src/client/query-store.ts +418 -0
- package/src/client/query.tsx +67 -0
- package/src/client/route-api.tsx +75 -0
- package/src/client/router/boundary-tree.tsx +10 -6
- package/src/client/router/context.ts +57 -4
- package/src/client/router/index.ts +3 -0
- package/src/client/router/link-utils.ts +29 -1
- package/src/client/router/navigation.ts +3 -50
- package/src/client/router/optimistic.ts +233 -0
- package/src/client/router/provider.tsx +404 -96
- package/src/client/router/search/index.ts +37 -17
- package/src/client/router/search-store.ts +4 -0
- package/src/client/router/sync-catch-up.ts +7 -0
- package/src/client/router/types.ts +71 -9
- package/src/client/sync.ts +438 -65
- package/src/client.ts +20 -60
- package/src/config.ts +5 -0
- package/src/define-route.ts +442 -155
- package/src/env.d.ts +2 -2
- package/src/furin.ts +213 -38
- package/src/plugin/hmr-dependencies.ts +58 -0
- package/src/plugin/index.ts +38 -2
- package/src/plugin/route-config-autofix.ts +2 -2
- package/src/plugin/routes.ts +19 -8
- package/src/plugin/transform-client-module.ts +123 -0
- package/src/plugin/transform-client.ts +293 -13
- package/src/plugin/transform-dev-page.ts +231 -0
- package/src/plugin/transform-isomorphic.ts +11 -5
- package/src/routes.ts +8 -0
- package/src/rsc/shared.tsx +104 -18
- package/src/rsc/version.ts +5 -0
- package/src/server/auto-invalidate/registry.ts +56 -24
- package/src/server/auto-invalidate/runtime.ts +17 -0
- package/src/server/auto-invalidate/types.ts +6 -0
- package/src/server/browser-events/plugin.ts +112 -1
- package/src/server/cache/dev-loader.ts +1 -1
- package/src/server/cache/invalidation.ts +2 -0
- package/src/server/cache/isr-ssg.ts +1 -1
- package/src/server/cache/isr.ts +6 -34
- package/src/server/cache/route-cache.ts +40 -4
- package/src/server/cache/ssg.ts +1 -1
- package/src/server/dev/diagnostics.ts +4 -2
- package/src/server/dev/error.ts +2 -1
- package/src/server/dev/graph.ts +70 -7
- package/src/server/dev/runtime.production.ts +4 -0
- package/src/server/dev/symbolicate.ts +2 -0
- package/src/server/dev-page-plugin.ts +160 -63
- package/src/server/evlog.ts +1 -1
- package/src/server/internal.ts +5 -0
- package/src/server/render/assemble.ts +15 -11
- package/src/server/render/document.tsx +13 -2
- package/src/server/render/element.tsx +11 -6
- package/src/server/render/isr.ts +71 -33
- package/src/server/render/loaders.ts +328 -70
- package/src/server/render/mixed-cache.ts +327 -0
- package/src/server/render/not-found.ts +14 -11
- package/src/server/render/ppr-document.ts +47 -11
- package/src/server/render/ppr-route.ts +54 -20
- package/src/server/render/react-stream.ts +17 -0
- package/src/server/render/route-frame-transport.ts +3 -0
- package/src/server/render/ssg.ts +53 -19
- package/src/server/render/ssr.ts +105 -36
- package/src/server/render/static-params.ts +1 -1
- package/src/server/render/template.ts +23 -0
- package/src/server/router/defined-route.ts +3 -0
- package/src/server/router/discovery.ts +14 -0
- package/src/server/router/hmr.ts +109 -18
- package/src/server/router/patterns.ts +27 -0
- package/src/server/router/plugin.ts +94 -19
- package/src/server/router/source-version.ts +20 -2
- package/src/server/router/types.ts +4 -0
- package/src/server/security/csp.ts +44 -0
- package/src/server/sync/adapter.ts +41 -4
- package/src/server/sync/atomic.ts +303 -0
- package/src/server/sync/drizzle/index.ts +145 -0
- package/src/server/sync/execution-error.ts +1 -0
- package/src/server/sync/fingerprint.ts +2 -6
- package/src/server/sync/index.ts +6 -0
- package/src/server/sync/mutation-handler.ts +69 -0
- package/src/server/sync/plugin.ts +177 -60
- package/src/server/sync/postgres/execute.ts +20 -0
- package/src/server/sync/postgres/index.ts +60 -16
- package/src/server/sync/postgres/sql.ts +20 -0
- package/src/server/sync/prisma/index.ts +70 -0
- package/src/server/sync/queries.ts +75 -0
- package/src/server/sync/query-context.ts +85 -0
- package/src/server/sync/redis/index.ts +7 -3
- package/src/server/sync/redis/scripts.ts +1 -1
- package/src/server/sync/response.ts +21 -0
- package/src/server/sync/sqlite/index.ts +19 -8
- package/src/server/sync/stream.ts +14 -22
- package/src/server/sync/validation.ts +34 -0
- package/src/shared/defer.ts +48 -0
- package/src/shared/error.ts +2 -2
- package/src/shared/page-key.ts +36 -0
- package/src/shared/query-bindings.ts +138 -0
- package/src/shared/route-frame.ts +13 -1
- package/src/shared/route-map.ts +12 -2
- package/src/shared/sync-query.ts +138 -0
|
@@ -1,11 +1,24 @@
|
|
|
1
1
|
import { log } from "evlog";
|
|
2
2
|
import type React from "react";
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
createElement,
|
|
5
|
+
useCallback,
|
|
6
|
+
useEffect,
|
|
7
|
+
useLayoutEffect,
|
|
8
|
+
useMemo,
|
|
9
|
+
useRef,
|
|
10
|
+
useState,
|
|
11
|
+
useSyncExternalStore,
|
|
12
|
+
} from "react";
|
|
4
13
|
import type { HeadOptions } from "../../client.ts";
|
|
5
14
|
import { parseDeferredNdjson } from "../../shared/deferred-ndjson.ts";
|
|
15
|
+
import { type DeferredQueryValue, projectDeferredQueryData } from "../../shared/query-bindings.ts";
|
|
6
16
|
import type { SearchParamsInput } from "../../shared/search-params.ts";
|
|
17
|
+
import { type QuerySeed, queryTag } from "../../shared/sync-query.ts";
|
|
7
18
|
import { subscribeBrowserEvent } from "../browser-events.ts";
|
|
8
19
|
import { DocumentProvider, useDocumentState } from "../document.tsx";
|
|
20
|
+
import { QueryStoreContext } from "../query.tsx";
|
|
21
|
+
import { QueryStore } from "../query-store.ts";
|
|
9
22
|
import { isAbortError } from "./abort.ts";
|
|
10
23
|
import { buildPageElement, buildRouterTree } from "./boundary-tree.tsx";
|
|
11
24
|
import {
|
|
@@ -27,6 +40,7 @@ import {
|
|
|
27
40
|
stripHashFromHref,
|
|
28
41
|
toLogical,
|
|
29
42
|
} from "./link-utils.ts";
|
|
43
|
+
import { createOptimisticRuntime, registerOptimisticRuntime, SYNC_REQUEST } from "./optimistic.ts";
|
|
30
44
|
import { createSearchStore, SearchStoreContext } from "./search-store.ts";
|
|
31
45
|
import {
|
|
32
46
|
buildDataEndpoint,
|
|
@@ -50,6 +64,10 @@ import type {
|
|
|
50
64
|
|
|
51
65
|
const EMPTY_SEARCH: SearchParamsInput = {};
|
|
52
66
|
|
|
67
|
+
function isRedirectState(state: RouterState | null): state is RouterState & { finalHref: string } {
|
|
68
|
+
return Boolean(state?.finalHref && !state.match && !state.notFound);
|
|
69
|
+
}
|
|
70
|
+
|
|
53
71
|
function getHistoryStateObject(): object {
|
|
54
72
|
const value = history.state;
|
|
55
73
|
return value !== null && typeof value === "object" ? value : {};
|
|
@@ -119,10 +137,24 @@ export function RouterProvider({
|
|
|
119
137
|
syncPath,
|
|
120
138
|
}: RouterProviderProps): React.ReactElement {
|
|
121
139
|
const initialDocumentState = useDocumentState();
|
|
140
|
+
const [queries] = useState(() => {
|
|
141
|
+
const store = new QueryStore(
|
|
142
|
+
typeof window === "undefined" ? undefined : window.location.origin
|
|
143
|
+
);
|
|
144
|
+
store.hydrate(
|
|
145
|
+
(initialData.__furinQueries as QuerySeed[] | undefined) ?? [],
|
|
146
|
+
typeof window === "undefined" ? undefined : window.location.origin
|
|
147
|
+
);
|
|
148
|
+
return store;
|
|
149
|
+
});
|
|
122
150
|
// Initial state. When `initialMatch` is `null`, `initialNotFound` MUST be set —
|
|
123
151
|
// the provider boots into the inline not-found UI.
|
|
124
152
|
const [state, setState] = useState<RouterState>(() => ({
|
|
125
|
-
data:
|
|
153
|
+
data: Object.fromEntries(
|
|
154
|
+
Object.entries(initialData).filter(([key]) => key !== "__furinQueries")
|
|
155
|
+
),
|
|
156
|
+
querySeeds: initialData.__furinQueries as QuerySeed[] | undefined,
|
|
157
|
+
hydrateQueries: queries.captureHydration(),
|
|
126
158
|
error: initialError
|
|
127
159
|
? {
|
|
128
160
|
...initialError,
|
|
@@ -134,6 +166,46 @@ export function RouterProvider({
|
|
|
134
166
|
notFound: initialNotFound,
|
|
135
167
|
}));
|
|
136
168
|
const [boundaryResetVersion, setBoundaryResetVersion] = useState(0);
|
|
169
|
+
const currentState = useRef(state);
|
|
170
|
+
useLayoutEffect(() => {
|
|
171
|
+
currentState.current = state;
|
|
172
|
+
}, [state]);
|
|
173
|
+
useLayoutEffect(() => {
|
|
174
|
+
state.hydrateQueries?.(
|
|
175
|
+
state.querySeeds ?? [],
|
|
176
|
+
typeof window === "undefined" ? undefined : window.location.origin
|
|
177
|
+
);
|
|
178
|
+
}, [state.hydrateQueries, state.querySeeds]);
|
|
179
|
+
const resolvedQueries = useRef(new WeakMap<Promise<unknown>, DeferredQueryValue>());
|
|
180
|
+
const [deferredRevision, setDeferredRevision] = useState(0);
|
|
181
|
+
useEffect(() => {
|
|
182
|
+
const snapshot = currentState.current;
|
|
183
|
+
const hydrated = new Set(snapshot.querySeeds?.map((seed) => seed.url));
|
|
184
|
+
let disposed = false;
|
|
185
|
+
for (const promise of Object.values(state.data)) {
|
|
186
|
+
if (!(promise instanceof Promise)) {
|
|
187
|
+
continue;
|
|
188
|
+
}
|
|
189
|
+
promise.then(
|
|
190
|
+
(value: unknown) => {
|
|
191
|
+
if (disposed) {
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
resolvedQueries.current.set(promise, { value });
|
|
195
|
+
const seeds = (snapshot.querySeeds ?? []).filter((seed) => !hydrated.has(seed.url));
|
|
196
|
+
snapshot.hydrateQueries?.(seeds, window.location.origin);
|
|
197
|
+
for (const seed of seeds) {
|
|
198
|
+
hydrated.add(seed.url);
|
|
199
|
+
}
|
|
200
|
+
setDeferredRevision((revision) => revision + 1);
|
|
201
|
+
},
|
|
202
|
+
() => undefined
|
|
203
|
+
);
|
|
204
|
+
}
|
|
205
|
+
return () => {
|
|
206
|
+
disposed = true;
|
|
207
|
+
};
|
|
208
|
+
}, [queries, state.data]);
|
|
137
209
|
const [isNavigating, setIsNavigating] = useState(false);
|
|
138
210
|
// currentHref stores the LOGICAL path (basePath stripped) so Link active-state
|
|
139
211
|
// detection works with route patterns that never include the basePath prefix.
|
|
@@ -143,6 +215,33 @@ export function RouterProvider({
|
|
|
143
215
|
}
|
|
144
216
|
return normalizeHref(toLogical(window.location.pathname, basePath)) + window.location.search;
|
|
145
217
|
});
|
|
218
|
+
const optimisticSnapshot = useRef({
|
|
219
|
+
href: currentHref,
|
|
220
|
+
loaded: state.match !== null,
|
|
221
|
+
pattern: state.match?.pattern,
|
|
222
|
+
});
|
|
223
|
+
useLayoutEffect(() => {
|
|
224
|
+
optimisticSnapshot.current = {
|
|
225
|
+
href: currentHref,
|
|
226
|
+
loaded: state.match !== null && !(state.error || state.notFound),
|
|
227
|
+
pattern: state.match?.pattern,
|
|
228
|
+
};
|
|
229
|
+
}, [currentHref, state.match, state.error, state.notFound]);
|
|
230
|
+
const syncResponse = useRef<
|
|
231
|
+
((response: Response | undefined, optimistic: boolean) => void) | undefined
|
|
232
|
+
>(undefined);
|
|
233
|
+
const [optimisticRuntime] = useState(() =>
|
|
234
|
+
createOptimisticRuntime({
|
|
235
|
+
basePath,
|
|
236
|
+
queries,
|
|
237
|
+
snapshot: () => optimisticSnapshot.current,
|
|
238
|
+
onResponse: (response, optimistic) => syncResponse.current?.(response, optimistic),
|
|
239
|
+
})
|
|
240
|
+
);
|
|
241
|
+
useSyncExternalStore(optimisticRuntime.subscribe, optimisticRuntime.revision, () => 0);
|
|
242
|
+
const queryRevision = useSyncExternalStore(queries.subscribeAll, queries.revision, () => 0);
|
|
243
|
+
useEffect(() => registerOptimisticRuntime(optimisticRuntime), [optimisticRuntime]);
|
|
244
|
+
const snapshotVersions = useRef(new WeakMap<RouterState, number>());
|
|
146
245
|
const prefetchCache = useRef(new Map<string, CacheEntry>());
|
|
147
246
|
/** Monotonic counter to discard stale navigations (race condition guard). */
|
|
148
247
|
const navVersion = useRef(0);
|
|
@@ -165,6 +264,7 @@ export function RouterProvider({
|
|
|
165
264
|
const pendingScrollRef = useRef<
|
|
166
265
|
{ type: "restore"; key: string } | { type: "reset"; href: string } | null
|
|
167
266
|
>(null);
|
|
267
|
+
const renderedHistoryKey = useRef<string | undefined>(undefined);
|
|
168
268
|
/**
|
|
169
269
|
* Tracks the last successfully rendered match for use in the no-route 404 path.
|
|
170
270
|
* `null` on the initial render when the URL didn't match any route and the
|
|
@@ -205,7 +305,6 @@ export function RouterProvider({
|
|
|
205
305
|
async (physicalHref: string, signal: AbortSignal | undefined): Promise<RouterState | null> => {
|
|
206
306
|
const res = await fetch(physicalHref, { signal });
|
|
207
307
|
if (isStaleDeployResponse(res)) {
|
|
208
|
-
window.location.href = physicalHref;
|
|
209
308
|
return null;
|
|
210
309
|
}
|
|
211
310
|
const { data, finalHref, title } = await parsePageResponse(res, basePath);
|
|
@@ -233,6 +332,7 @@ export function RouterProvider({
|
|
|
233
332
|
hmrRefresh: boolean
|
|
234
333
|
// biome-ignore lint/complexity/noExcessiveCognitiveComplexity: SPA navigation orchestrator — response shapes (redirect, stale-deploy, deferred) + abort-signal wiring require this depth
|
|
235
334
|
): Promise<RouterState | null> => {
|
|
335
|
+
const hydrateQueries = queries.captureHydration();
|
|
236
336
|
// Normalize trailing slashes so "/docs/routing/" matches the route
|
|
237
337
|
// pattern "/docs/routing" — static hosts (GitHub Pages, Netlify) often
|
|
238
338
|
// redirect to or serve URLs with a trailing slash.
|
|
@@ -255,6 +355,7 @@ export function RouterProvider({
|
|
|
255
355
|
const [res, loadedMod] = await Promise.all([
|
|
256
356
|
fetch(dataEndpoint, {
|
|
257
357
|
headers: hmrRefresh ? { "x-furin-hmr-refresh": "1" } : undefined,
|
|
358
|
+
redirect: "manual",
|
|
258
359
|
signal,
|
|
259
360
|
}),
|
|
260
361
|
loadedModule,
|
|
@@ -262,7 +363,17 @@ export function RouterProvider({
|
|
|
262
363
|
|
|
263
364
|
// Stale-deploy detection: force a full page reload to pick up the new bundle.
|
|
264
365
|
if (isStaleDeployResponse(res)) {
|
|
265
|
-
|
|
366
|
+
return null;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
const contentType = res.headers.get("content-type") ?? "";
|
|
370
|
+
if (
|
|
371
|
+
!(
|
|
372
|
+
staticMode ||
|
|
373
|
+
contentType.includes("application/x-ndjson") ||
|
|
374
|
+
contentType.includes("application/x-furin-route")
|
|
375
|
+
)
|
|
376
|
+
) {
|
|
266
377
|
return null;
|
|
267
378
|
}
|
|
268
379
|
|
|
@@ -297,6 +408,7 @@ export function RouterProvider({
|
|
|
297
408
|
__furinRedirect,
|
|
298
409
|
__furinError,
|
|
299
410
|
__furinTitle,
|
|
411
|
+
__furinQueries,
|
|
300
412
|
...cleanSyncData
|
|
301
413
|
} = syncData as {
|
|
302
414
|
__furinStatus?: number;
|
|
@@ -305,11 +417,34 @@ export function RouterProvider({
|
|
|
305
417
|
__furinError?: { digest: string; message: string; status: number };
|
|
306
418
|
__furinHead?: HeadOptions;
|
|
307
419
|
__furinTitle?: string;
|
|
420
|
+
__furinQueries?: QuerySeed[];
|
|
308
421
|
[key: string]: unknown;
|
|
309
422
|
};
|
|
310
423
|
|
|
311
|
-
|
|
312
|
-
|
|
424
|
+
if (typeof __furinRedirect === "string") {
|
|
425
|
+
const target = new URL(__furinRedirect, window.location.origin);
|
|
426
|
+
const isLogicalPath =
|
|
427
|
+
__furinRedirect.startsWith("/") && !__furinRedirect.startsWith("//");
|
|
428
|
+
const withinMount =
|
|
429
|
+
isLogicalPath ||
|
|
430
|
+
basePath === "" ||
|
|
431
|
+
target.pathname === basePath ||
|
|
432
|
+
target.pathname.startsWith(`${basePath}/`);
|
|
433
|
+
const targetPath = isLogicalPath ? target.pathname : toLogical(target.pathname, basePath);
|
|
434
|
+
if (
|
|
435
|
+
target.origin !== window.location.origin ||
|
|
436
|
+
!withinMount ||
|
|
437
|
+
!routes.some((route) => route.regex.test(targetPath))
|
|
438
|
+
) {
|
|
439
|
+
return null;
|
|
440
|
+
}
|
|
441
|
+
return {
|
|
442
|
+
data: {},
|
|
443
|
+
finalHref: targetPath + target.search + target.hash,
|
|
444
|
+
match: null,
|
|
445
|
+
title: "",
|
|
446
|
+
};
|
|
447
|
+
}
|
|
313
448
|
|
|
314
449
|
// Merge sync data + deferred Promises.
|
|
315
450
|
const data = { ...cleanSyncData, ...deferredPromises };
|
|
@@ -333,7 +468,6 @@ export function RouterProvider({
|
|
|
333
468
|
return {
|
|
334
469
|
data,
|
|
335
470
|
error: __furinError,
|
|
336
|
-
finalHref,
|
|
337
471
|
head: __furinHead,
|
|
338
472
|
match: loadedMatch,
|
|
339
473
|
title,
|
|
@@ -353,7 +487,6 @@ export function RouterProvider({
|
|
|
353
487
|
: {};
|
|
354
488
|
return {
|
|
355
489
|
data,
|
|
356
|
-
finalHref,
|
|
357
490
|
head: __furinHead,
|
|
358
491
|
match: loadedMatch,
|
|
359
492
|
notFound,
|
|
@@ -361,12 +494,14 @@ export function RouterProvider({
|
|
|
361
494
|
};
|
|
362
495
|
}
|
|
363
496
|
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
497
|
+
return {
|
|
498
|
+
data,
|
|
499
|
+
head: __furinHead,
|
|
500
|
+
match: loadedMatch,
|
|
501
|
+
querySeeds: __furinQueries,
|
|
502
|
+
hydrateQueries,
|
|
503
|
+
title,
|
|
504
|
+
};
|
|
370
505
|
} catch (err: unknown) {
|
|
371
506
|
if (!isAbortError(err)) {
|
|
372
507
|
log.error({
|
|
@@ -378,29 +513,42 @@ export function RouterProvider({
|
|
|
378
513
|
return null;
|
|
379
514
|
}
|
|
380
515
|
},
|
|
381
|
-
[routes, basePath, resolveNoMatchState]
|
|
516
|
+
[routes, basePath, resolveNoMatchState, queries]
|
|
382
517
|
);
|
|
383
518
|
|
|
384
|
-
const
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
519
|
+
const fetchConfirmedPageState = useCallback(
|
|
520
|
+
async (href: string, signal: AbortSignal | undefined, hmrRefresh: boolean) => {
|
|
521
|
+
for (;;) {
|
|
522
|
+
try {
|
|
523
|
+
// biome-ignore lint/performance/noAwaitInLoops: a snapshot must be read after local writes settle.
|
|
524
|
+
await optimisticRuntime.wait(href, signal);
|
|
525
|
+
} catch (error) {
|
|
526
|
+
if (signal?.aborted) {
|
|
527
|
+
return null;
|
|
528
|
+
}
|
|
529
|
+
throw error;
|
|
530
|
+
}
|
|
531
|
+
const version = optimisticRuntime.revision(href);
|
|
532
|
+
const snapshot = await fetchPageState(href, signal, hmrRefresh);
|
|
533
|
+
if (optimisticRuntime.publishable(href, version)) {
|
|
534
|
+
if (snapshot) {
|
|
535
|
+
snapshotVersions.current.set(snapshot, version);
|
|
536
|
+
}
|
|
537
|
+
return snapshot;
|
|
391
538
|
}
|
|
392
539
|
}
|
|
393
|
-
|
|
394
|
-
|
|
540
|
+
},
|
|
541
|
+
[fetchPageState, optimisticRuntime]
|
|
542
|
+
);
|
|
395
543
|
|
|
396
|
-
|
|
397
|
-
const
|
|
398
|
-
normalizedPath === "/" || normalizedPath.endsWith("/")
|
|
399
|
-
? normalizedPath
|
|
400
|
-
: `${normalizedPath}/`;
|
|
544
|
+
const invalidatePrefetch = useCallback((path: string, type: "page" | "layout") => {
|
|
545
|
+
const normalizedPath = normalizeHref(new URL(path, "http://furin.local").pathname);
|
|
401
546
|
for (const key of [...prefetchCache.current.keys()]) {
|
|
402
|
-
|
|
403
|
-
|
|
547
|
+
if (
|
|
548
|
+
shouldAutoRefreshPath(normalizeHref(stripHashFromHref(key)), [
|
|
549
|
+
{ path: normalizedPath, type },
|
|
550
|
+
])
|
|
551
|
+
) {
|
|
404
552
|
prefetchCache.current.delete(key);
|
|
405
553
|
}
|
|
406
554
|
}
|
|
@@ -434,14 +582,53 @@ export function RouterProvider({
|
|
|
434
582
|
signal: AbortSignal | undefined
|
|
435
583
|
): Promise<RouterState | null> {
|
|
436
584
|
const cached = prefetchCache.current.get(redirectLogical);
|
|
437
|
-
const
|
|
438
|
-
cached && !shouldRefetch(cached)
|
|
585
|
+
const useCached =
|
|
586
|
+
cached !== undefined && !shouldRefetch(cached) && !optimisticRuntime.has(redirectLogical);
|
|
587
|
+
let redirectState =
|
|
588
|
+
useCached && cached
|
|
439
589
|
? await cached.promise
|
|
440
|
-
: await
|
|
590
|
+
: await fetchConfirmedPageState(redirectLogical, signal, false);
|
|
441
591
|
if (navVersion.current !== myVersion) {
|
|
442
592
|
return null;
|
|
443
593
|
}
|
|
444
|
-
|
|
594
|
+
if (
|
|
595
|
+
useCached &&
|
|
596
|
+
(isRedirectState(redirectState) ||
|
|
597
|
+
(redirectState &&
|
|
598
|
+
!optimisticRuntime.publishable(
|
|
599
|
+
redirectLogical,
|
|
600
|
+
snapshotVersions.current.get(redirectState) ??
|
|
601
|
+
optimisticRuntime.revision(redirectLogical)
|
|
602
|
+
)))
|
|
603
|
+
) {
|
|
604
|
+
redirectState = await fetchConfirmedPageState(redirectLogical, signal, false);
|
|
605
|
+
}
|
|
606
|
+
return navVersion.current === myVersion ? redirectState : null;
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
async function followRedirects(
|
|
610
|
+
initialState: RouterState,
|
|
611
|
+
initialHref: string,
|
|
612
|
+
myVersion: number,
|
|
613
|
+
signal: AbortSignal | undefined
|
|
614
|
+
): Promise<{ href: string; state: RouterState } | null> {
|
|
615
|
+
let redirectState = initialState;
|
|
616
|
+
let href = initialHref;
|
|
617
|
+
const visited = new Set([href]);
|
|
618
|
+
for (let count = 0; isRedirectState(redirectState); count += 1) {
|
|
619
|
+
href = normalizeHref(redirectState.finalHref);
|
|
620
|
+
if (count >= 10 || visited.has(href)) {
|
|
621
|
+
return null;
|
|
622
|
+
}
|
|
623
|
+
visited.add(href);
|
|
624
|
+
// biome-ignore lint/performance/noAwaitInLoops: each redirect target depends on the previous response.
|
|
625
|
+
const next = await resolveRedirectState(href, myVersion, signal);
|
|
626
|
+
if (!next) {
|
|
627
|
+
return null;
|
|
628
|
+
}
|
|
629
|
+
redirectState = next;
|
|
630
|
+
}
|
|
631
|
+
return { href, state: redirectState };
|
|
445
632
|
}
|
|
446
633
|
|
|
447
634
|
const navigate = useCallback(
|
|
@@ -459,6 +646,7 @@ export function RouterProvider({
|
|
|
459
646
|
| undefined
|
|
460
647
|
) {
|
|
461
648
|
const logicalHref = normalizeHref(rawLogicalHref);
|
|
649
|
+
const beforeCommit = opts?.beforeCommit;
|
|
462
650
|
const finishUserNavigation =
|
|
463
651
|
opts?.hmrRefresh === true ? undefined : beginUserNavigation(pendingUserNavigation);
|
|
464
652
|
navVersion.current += 1;
|
|
@@ -470,14 +658,31 @@ export function RouterProvider({
|
|
|
470
658
|
setIsNavigating(true);
|
|
471
659
|
try {
|
|
472
660
|
const cached = prefetchCache.current.get(logicalHref);
|
|
661
|
+
const useCached =
|
|
662
|
+
cached !== undefined && !shouldRefetch(cached) && !optimisticRuntime.has(logicalHref);
|
|
473
663
|
let newState =
|
|
474
|
-
|
|
664
|
+
useCached && cached
|
|
475
665
|
? await cached.promise
|
|
476
|
-
: await
|
|
666
|
+
: await fetchConfirmedPageState(logicalHref, navSignal, opts?.hmrRefresh === true);
|
|
667
|
+
const cachedRedirect =
|
|
668
|
+
useCached &&
|
|
669
|
+
(isRedirectState(newState) ||
|
|
670
|
+
(newState &&
|
|
671
|
+
!optimisticRuntime.publishable(
|
|
672
|
+
logicalHref,
|
|
673
|
+
snapshotVersions.current.get(newState) ?? optimisticRuntime.revision(logicalHref)
|
|
674
|
+
)));
|
|
675
|
+
if (cachedRedirect) {
|
|
676
|
+
newState = await fetchConfirmedPageState(
|
|
677
|
+
logicalHref,
|
|
678
|
+
navSignal,
|
|
679
|
+
opts?.hmrRefresh === true
|
|
680
|
+
);
|
|
681
|
+
}
|
|
477
682
|
if (navVersion.current !== myVersion || opts?.shouldCommit?.() === false) {
|
|
478
683
|
return;
|
|
479
684
|
}
|
|
480
|
-
if (newState && (!
|
|
685
|
+
if (newState && (!useCached || cachedRedirect)) {
|
|
481
686
|
setPrefetchCacheEntry(
|
|
482
687
|
prefetchCache.current,
|
|
483
688
|
logicalHref,
|
|
@@ -499,34 +704,39 @@ export function RouterProvider({
|
|
|
499
704
|
return;
|
|
500
705
|
}
|
|
501
706
|
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
...getHistoryStateObject(),
|
|
510
|
-
_furinKey: getHistoryKey(history.state) ?? generateHistoryKey(),
|
|
511
|
-
},
|
|
512
|
-
"",
|
|
513
|
-
redirectPhysical
|
|
514
|
-
);
|
|
515
|
-
setCurrentHref(redirectLogical);
|
|
516
|
-
const redirectState = await resolveRedirectState(redirectLogical, myVersion, navSignal);
|
|
517
|
-
if (!redirectState) {
|
|
518
|
-
if (navVersion.current === myVersion) {
|
|
519
|
-
window.location.href = redirectPhysical;
|
|
520
|
-
}
|
|
521
|
-
return;
|
|
707
|
+
const resolved = await followRedirects(newState, logicalHref, myVersion, navSignal);
|
|
708
|
+
if (navVersion.current !== myVersion) {
|
|
709
|
+
return;
|
|
710
|
+
}
|
|
711
|
+
if (!resolved) {
|
|
712
|
+
if (navVersion.current === myVersion) {
|
|
713
|
+
window.location.href = basePath + logicalHref;
|
|
522
714
|
}
|
|
523
|
-
|
|
715
|
+
return;
|
|
524
716
|
}
|
|
717
|
+
newState = resolved.state;
|
|
525
718
|
|
|
526
719
|
if (opts?.shouldCommit?.() === false) {
|
|
527
720
|
return;
|
|
528
721
|
}
|
|
529
|
-
|
|
722
|
+
const publicationHref = newState.finalHref ?? resolved.href;
|
|
723
|
+
if (
|
|
724
|
+
!optimisticRuntime.publishable(
|
|
725
|
+
publicationHref,
|
|
726
|
+
snapshotVersions.current.get(newState) ?? optimisticRuntime.revision(publicationHref)
|
|
727
|
+
)
|
|
728
|
+
) {
|
|
729
|
+
return;
|
|
730
|
+
}
|
|
731
|
+
if (!(newState.error || newState.notFound)) {
|
|
732
|
+
optimisticRuntime.commit(publicationHref);
|
|
733
|
+
}
|
|
734
|
+
beforeCommit?.();
|
|
735
|
+
const effectiveLogical = newState.finalHref ?? resolved.href;
|
|
736
|
+
const physicalEffective = basePath + effectiveLogical;
|
|
737
|
+
if (opts?.resetScroll ?? true) {
|
|
738
|
+
pendingScrollRef.current = { href: physicalEffective, type: "reset" };
|
|
739
|
+
}
|
|
530
740
|
currentMatchRef.current = newState.match;
|
|
531
741
|
if (!newState.error) {
|
|
532
742
|
setBoundaryResetVersion((version) => version + 1);
|
|
@@ -535,8 +745,6 @@ export function RouterProvider({
|
|
|
535
745
|
if (newState.title) {
|
|
536
746
|
document.title = newState.title;
|
|
537
747
|
}
|
|
538
|
-
const effectiveLogical = newState.finalHref ?? redirectLogical ?? logicalHref;
|
|
539
|
-
const physicalEffective = basePath + effectiveLogical;
|
|
540
748
|
if (opts?.replace) {
|
|
541
749
|
window.history.replaceState(
|
|
542
750
|
{
|
|
@@ -547,18 +755,16 @@ export function RouterProvider({
|
|
|
547
755
|
physicalEffective
|
|
548
756
|
);
|
|
549
757
|
} else {
|
|
550
|
-
const currentKey =
|
|
758
|
+
const currentKey = renderedHistoryKey.current;
|
|
551
759
|
if (currentKey) {
|
|
552
760
|
saveScrollPosition(currentKey);
|
|
553
761
|
}
|
|
554
762
|
window.history.pushState({ _furinKey: generateHistoryKey() }, "", physicalEffective);
|
|
555
763
|
}
|
|
764
|
+
renderedHistoryKey.current = getHistoryKey(history.state);
|
|
556
765
|
const effectiveUrl = new URL(physicalEffective, window.location.origin);
|
|
557
766
|
const logicalPath = normalizeHref(toLogical(effectiveUrl.pathname, basePath));
|
|
558
767
|
setCurrentHref(logicalPath + effectiveUrl.search);
|
|
559
|
-
if (opts?.resetScroll ?? true) {
|
|
560
|
-
pendingScrollRef.current = { href: physicalEffective, type: "reset" };
|
|
561
|
-
}
|
|
562
768
|
} finally {
|
|
563
769
|
finishUserNavigation?.();
|
|
564
770
|
if (navVersion.current === myVersion) {
|
|
@@ -566,12 +772,19 @@ export function RouterProvider({
|
|
|
566
772
|
}
|
|
567
773
|
}
|
|
568
774
|
},
|
|
569
|
-
[
|
|
775
|
+
[
|
|
776
|
+
basePath,
|
|
777
|
+
fetchConfirmedPageState,
|
|
778
|
+
defaultPreloadStaleTime,
|
|
779
|
+
prefetchCacheSize,
|
|
780
|
+
optimisticRuntime,
|
|
781
|
+
]
|
|
570
782
|
);
|
|
571
783
|
|
|
572
784
|
const [searchStore] = useState(() =>
|
|
573
785
|
createSearchStore({
|
|
574
786
|
currentHref,
|
|
787
|
+
currentPattern: state.match?.pattern,
|
|
575
788
|
navigate,
|
|
576
789
|
search: resolvedSearch,
|
|
577
790
|
searchRoutes: routes,
|
|
@@ -580,11 +793,12 @@ export function RouterProvider({
|
|
|
580
793
|
const searchSnapshot = useMemo(
|
|
581
794
|
() => ({
|
|
582
795
|
currentHref,
|
|
796
|
+
currentPattern: state.match?.pattern,
|
|
583
797
|
navigate,
|
|
584
798
|
search: resolvedSearch,
|
|
585
799
|
searchRoutes: routes,
|
|
586
800
|
}),
|
|
587
|
-
[currentHref, navigate, resolvedSearch, routes]
|
|
801
|
+
[currentHref, state.match?.pattern, navigate, resolvedSearch, routes]
|
|
588
802
|
);
|
|
589
803
|
|
|
590
804
|
useLayoutEffect(() => {
|
|
@@ -622,11 +836,39 @@ export function RouterProvider({
|
|
|
622
836
|
onError: (error) => {
|
|
623
837
|
log.warn({ action: "sync_refresh_failed", error: String(error) });
|
|
624
838
|
},
|
|
625
|
-
refresh: () =>
|
|
839
|
+
refresh: async () => {
|
|
840
|
+
await waitForUserNavigation(pendingUserNavigation);
|
|
841
|
+
await refresh(undefined);
|
|
842
|
+
},
|
|
626
843
|
}),
|
|
627
844
|
[refresh]
|
|
628
845
|
);
|
|
629
846
|
|
|
847
|
+
useLayoutEffect(() => {
|
|
848
|
+
syncResponse.current = (response, optimistic) => {
|
|
849
|
+
const invalidated: Array<{ path: string; type: "page" | "layout" }> = [];
|
|
850
|
+
if (
|
|
851
|
+
response &&
|
|
852
|
+
(response.url === "" || new URL(response.url).origin === window.location.origin)
|
|
853
|
+
) {
|
|
854
|
+
applyRevalidateHeader(response.headers, (path, type) => {
|
|
855
|
+
const resolvedType = type ?? "page";
|
|
856
|
+
invalidatePrefetch(path, resolvedType);
|
|
857
|
+
invalidated.push({ path, type: resolvedType });
|
|
858
|
+
});
|
|
859
|
+
}
|
|
860
|
+
if (
|
|
861
|
+
optimistic ||
|
|
862
|
+
(autoRefresh && shouldAutoRefreshPath(optimisticSnapshot.current.href, invalidated))
|
|
863
|
+
) {
|
|
864
|
+
invalidationRefresh.run();
|
|
865
|
+
}
|
|
866
|
+
};
|
|
867
|
+
return () => {
|
|
868
|
+
syncResponse.current = undefined;
|
|
869
|
+
};
|
|
870
|
+
}, [autoRefresh, invalidatePrefetch, invalidationRefresh]);
|
|
871
|
+
|
|
630
872
|
// Expose a transactional refresh to the HMR handler in _hydrate.tsx. The
|
|
631
873
|
// callback swaps the hot component only after fresh data is ready and just
|
|
632
874
|
// before setState publishes the complete router snapshot.
|
|
@@ -639,7 +881,6 @@ export function RouterProvider({
|
|
|
639
881
|
) => {
|
|
640
882
|
hmrVersion.current += 1;
|
|
641
883
|
const myHmrVersion = hmrVersion.current;
|
|
642
|
-
// biome-ignore lint/suspicious/noUnnecessaryConditions: the ref persists invalidation from an earlier HMR callback
|
|
643
884
|
const shouldRefreshData = hmrState.current.dataInvalidated || dataChanged !== false;
|
|
644
885
|
if (!shouldRefreshData) {
|
|
645
886
|
beforeCommit?.();
|
|
@@ -673,6 +914,9 @@ export function RouterProvider({
|
|
|
673
914
|
|
|
674
915
|
const handlePopState = useCallback(() => {
|
|
675
916
|
const finishUserNavigation = beginUserNavigation(pendingUserNavigation);
|
|
917
|
+
if (renderedHistoryKey.current) {
|
|
918
|
+
saveScrollPosition(renderedHistoryKey.current);
|
|
919
|
+
}
|
|
676
920
|
const destKey = getHistoryKey(history.state);
|
|
677
921
|
const logicalPath = normalizeHref(toLogical(window.location.pathname, basePath));
|
|
678
922
|
const logicalHref = logicalPath + window.location.search;
|
|
@@ -689,10 +933,20 @@ export function RouterProvider({
|
|
|
689
933
|
try {
|
|
690
934
|
const cached = prefetchCache.current.get(logicalHref);
|
|
691
935
|
let newState: RouterState | null;
|
|
692
|
-
if (cached && !shouldRefetch(cached)) {
|
|
936
|
+
if (cached && !shouldRefetch(cached) && !optimisticRuntime.has(logicalHref)) {
|
|
693
937
|
newState = await cached.promise;
|
|
938
|
+
if (
|
|
939
|
+
isRedirectState(newState) ||
|
|
940
|
+
(newState &&
|
|
941
|
+
!optimisticRuntime.publishable(
|
|
942
|
+
logicalHref,
|
|
943
|
+
snapshotVersions.current.get(newState) ?? optimisticRuntime.revision(logicalHref)
|
|
944
|
+
))
|
|
945
|
+
) {
|
|
946
|
+
newState = await fetchConfirmedPageState(logicalHref, navSignal, false);
|
|
947
|
+
}
|
|
694
948
|
} else {
|
|
695
|
-
newState = await
|
|
949
|
+
newState = await fetchConfirmedPageState(logicalHref, navSignal, false);
|
|
696
950
|
}
|
|
697
951
|
if (navVersion.current !== myVersion) {
|
|
698
952
|
return;
|
|
@@ -707,20 +961,28 @@ export function RouterProvider({
|
|
|
707
961
|
return;
|
|
708
962
|
}
|
|
709
963
|
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
if (!redirectState) {
|
|
718
|
-
if (navVersion.current === myVersion) {
|
|
719
|
-
window.location.reload();
|
|
720
|
-
}
|
|
721
|
-
return;
|
|
964
|
+
const resolved = await followRedirects(newState, logicalHref, myVersion, navSignal);
|
|
965
|
+
if (navVersion.current !== myVersion) {
|
|
966
|
+
return;
|
|
967
|
+
}
|
|
968
|
+
if (!resolved) {
|
|
969
|
+
if (navVersion.current === myVersion) {
|
|
970
|
+
window.location.reload();
|
|
722
971
|
}
|
|
723
|
-
|
|
972
|
+
return;
|
|
973
|
+
}
|
|
974
|
+
newState = resolved.state;
|
|
975
|
+
const publicationHref = newState.finalHref ?? resolved.href;
|
|
976
|
+
if (
|
|
977
|
+
!optimisticRuntime.publishable(
|
|
978
|
+
publicationHref,
|
|
979
|
+
snapshotVersions.current.get(newState) ?? optimisticRuntime.revision(publicationHref)
|
|
980
|
+
)
|
|
981
|
+
) {
|
|
982
|
+
return;
|
|
983
|
+
}
|
|
984
|
+
if (!(newState.error || newState.notFound)) {
|
|
985
|
+
optimisticRuntime.commit(publicationHref);
|
|
724
986
|
}
|
|
725
987
|
|
|
726
988
|
currentMatchRef.current = newState.match;
|
|
@@ -731,10 +993,11 @@ export function RouterProvider({
|
|
|
731
993
|
if (newState.title) {
|
|
732
994
|
document.title = newState.title;
|
|
733
995
|
}
|
|
734
|
-
const effectiveLogical = newState.finalHref ??
|
|
735
|
-
if (
|
|
996
|
+
const effectiveLogical = newState.finalHref ?? resolved.href;
|
|
997
|
+
if (effectiveLogical !== logicalHref) {
|
|
736
998
|
window.history.replaceState(history.state, "", basePath + effectiveLogical);
|
|
737
999
|
}
|
|
1000
|
+
renderedHistoryKey.current = destKey;
|
|
738
1001
|
setCurrentHref(normalizeHref(effectiveLogical));
|
|
739
1002
|
if (destKey) {
|
|
740
1003
|
pendingScrollRef.current = { key: destKey, type: "restore" };
|
|
@@ -748,7 +1011,7 @@ export function RouterProvider({
|
|
|
748
1011
|
}
|
|
749
1012
|
}
|
|
750
1013
|
})();
|
|
751
|
-
}, [
|
|
1014
|
+
}, [fetchConfirmedPageState, basePath, optimisticRuntime]);
|
|
752
1015
|
|
|
753
1016
|
// Disable native scroll restoration and assign a key to the initial history entry.
|
|
754
1017
|
useEffect(() => {
|
|
@@ -756,12 +1019,12 @@ export function RouterProvider({
|
|
|
756
1019
|
if (!getHistoryKey(history.state)) {
|
|
757
1020
|
history.replaceState({ ...getHistoryStateObject(), _furinKey: generateHistoryKey() }, "");
|
|
758
1021
|
}
|
|
1022
|
+
renderedHistoryKey.current = getHistoryKey(history.state);
|
|
759
1023
|
}, []);
|
|
760
1024
|
|
|
761
1025
|
// Render-synchronous scroll restoration.
|
|
762
1026
|
useLayoutEffect(() => {
|
|
763
1027
|
const instruction = pendingScrollRef.current;
|
|
764
|
-
// biome-ignore lint/suspicious/noUnnecessaryConditions: the ref is null until navigation schedules scrolling
|
|
765
1028
|
if (!instruction) {
|
|
766
1029
|
return;
|
|
767
1030
|
}
|
|
@@ -789,7 +1052,7 @@ export function RouterProvider({
|
|
|
789
1052
|
} else {
|
|
790
1053
|
window.scrollTo({ behavior: "instant", top: 0 });
|
|
791
1054
|
}
|
|
792
|
-
}, [currentHref]);
|
|
1055
|
+
}, [currentHref, state]);
|
|
793
1056
|
|
|
794
1057
|
// Handle browser back/forward
|
|
795
1058
|
useEffect(() => {
|
|
@@ -838,7 +1101,7 @@ export function RouterProvider({
|
|
|
838
1101
|
return;
|
|
839
1102
|
}
|
|
840
1103
|
e.preventDefault();
|
|
841
|
-
navigate(logicalHref,
|
|
1104
|
+
navigate(logicalHref, undefined);
|
|
842
1105
|
};
|
|
843
1106
|
document.addEventListener("click", handler);
|
|
844
1107
|
return () => document.removeEventListener("click", handler);
|
|
@@ -852,6 +1115,9 @@ export function RouterProvider({
|
|
|
852
1115
|
const originalFetch = window.fetch;
|
|
853
1116
|
const wrapped = async (...args: Parameters<typeof fetch>): Promise<Response> => {
|
|
854
1117
|
const response = await originalFetch.apply(window, args);
|
|
1118
|
+
if (args[1] && SYNC_REQUEST in args[1]) {
|
|
1119
|
+
return response;
|
|
1120
|
+
}
|
|
855
1121
|
if (!isSameOriginFetchResult(args[0], response.url, window.location.origin)) {
|
|
856
1122
|
return response;
|
|
857
1123
|
}
|
|
@@ -924,6 +1190,25 @@ export function RouterProvider({
|
|
|
924
1190
|
return (await response.json()) as SyncChangePagePayload;
|
|
925
1191
|
},
|
|
926
1192
|
onInvalidations: applyInvalidations,
|
|
1193
|
+
onReset: () => queries.invalidateAll(),
|
|
1194
|
+
onQueries: (identities) => {
|
|
1195
|
+
queries.invalidate(identities);
|
|
1196
|
+
const tags = new Set(identities.map(queryTag));
|
|
1197
|
+
const affected = (snapshot: RouterState) =>
|
|
1198
|
+
snapshot.querySeeds?.some((seed) => tags.has(queryTag(seed.identity)));
|
|
1199
|
+
for (const [href, entry] of prefetchCache.current) {
|
|
1200
|
+
entry.promise
|
|
1201
|
+
.then((snapshot) => {
|
|
1202
|
+
if (snapshot && affected(snapshot) && prefetchCache.current.get(href) === entry) {
|
|
1203
|
+
prefetchCache.current.delete(href);
|
|
1204
|
+
}
|
|
1205
|
+
})
|
|
1206
|
+
.catch(() => undefined);
|
|
1207
|
+
}
|
|
1208
|
+
if (autoRefresh && affected(currentState.current)) {
|
|
1209
|
+
invalidationRefresh.run();
|
|
1210
|
+
}
|
|
1211
|
+
},
|
|
927
1212
|
});
|
|
928
1213
|
const recover = () => {
|
|
929
1214
|
if (disposed) {
|
|
@@ -958,19 +1243,41 @@ export function RouterProvider({
|
|
|
958
1243
|
disposed = true;
|
|
959
1244
|
unsubscribe?.();
|
|
960
1245
|
};
|
|
961
|
-
}, [syncPath, basePath, invalidatePrefetch, invalidationRefresh, autoRefresh]);
|
|
1246
|
+
}, [syncPath, basePath, invalidatePrefetch, invalidationRefresh, autoRefresh, queries]);
|
|
962
1247
|
|
|
1248
|
+
const queryData = useMemo(
|
|
1249
|
+
() =>
|
|
1250
|
+
projectDeferredQueryData(
|
|
1251
|
+
state.data,
|
|
1252
|
+
state.querySeeds ?? [],
|
|
1253
|
+
(seed) => {
|
|
1254
|
+
if (seed.local && typeof window !== "undefined") {
|
|
1255
|
+
const original = new URL(seed.url);
|
|
1256
|
+
return queries.snapshot(
|
|
1257
|
+
new URL(original.pathname + original.search, window.location.origin).href
|
|
1258
|
+
).data;
|
|
1259
|
+
}
|
|
1260
|
+
return queries.snapshot(seed.url).data;
|
|
1261
|
+
},
|
|
1262
|
+
resolvedQueries.current
|
|
1263
|
+
),
|
|
1264
|
+
[queries, state.data, state.querySeeds, queryRevision, deferredRevision]
|
|
1265
|
+
);
|
|
1266
|
+
const renderedData = optimisticRuntime.project(queryData, currentHref);
|
|
963
1267
|
let pageElement: React.ReactNode;
|
|
964
1268
|
if (state.notFound || !state.match) {
|
|
965
|
-
|
|
1269
|
+
const notFoundElement = buildNotFoundPageElement(
|
|
966
1270
|
state.notFoundBoundaries ?? state.match?.segmentBoundaries ?? rootBoundaries,
|
|
967
1271
|
state.notFound ?? {}
|
|
968
1272
|
);
|
|
1273
|
+
pageElement = root?.layout
|
|
1274
|
+
? createElement(root.layout as React.ElementType, renderedData, notFoundElement)
|
|
1275
|
+
: notFoundElement;
|
|
969
1276
|
} else {
|
|
970
1277
|
pageElement = buildPageElement(
|
|
971
1278
|
state.match,
|
|
972
1279
|
root,
|
|
973
|
-
|
|
1280
|
+
renderedData,
|
|
974
1281
|
{
|
|
975
1282
|
onReset: () => {
|
|
976
1283
|
refresh(undefined).catch((err: unknown) => {
|
|
@@ -989,6 +1296,7 @@ export function RouterProvider({
|
|
|
989
1296
|
{
|
|
990
1297
|
basePath,
|
|
991
1298
|
currentHref,
|
|
1299
|
+
currentPattern: state.match?.pattern,
|
|
992
1300
|
defaultPreload,
|
|
993
1301
|
defaultPreloadDelay,
|
|
994
1302
|
defaultPreloadStaleTime,
|
|
@@ -1015,7 +1323,7 @@ export function RouterProvider({
|
|
|
1015
1323
|
</SearchStoreContext.Provider>
|
|
1016
1324
|
);
|
|
1017
1325
|
if (initialDocumentState === null) {
|
|
1018
|
-
return routerTree
|
|
1326
|
+
return <QueryStoreContext.Provider value={queries}>{routerTree}</QueryStoreContext.Provider>;
|
|
1019
1327
|
}
|
|
1020
1328
|
return (
|
|
1021
1329
|
<DocumentProvider
|
|
@@ -1024,7 +1332,7 @@ export function RouterProvider({
|
|
|
1024
1332
|
head: state.head,
|
|
1025
1333
|
}}
|
|
1026
1334
|
>
|
|
1027
|
-
{routerTree}
|
|
1335
|
+
<QueryStoreContext.Provider value={queries}>{routerTree}</QueryStoreContext.Provider>
|
|
1028
1336
|
</DocumentProvider>
|
|
1029
1337
|
);
|
|
1030
1338
|
}
|