@teyik0/furin 0.9.0-alpha.4 → 0.9.0-alpha.6
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 +1 -0
- package/dist/adapter/vercel.js +9 -6
- package/dist/build/index.js +74 -31
- package/dist/build/route-metadata.d.ts +1 -1
- package/dist/cli/index.js +74 -31
- package/dist/client/chunks/{client-rdxyrw11.js → client-hjqd5m3b.js} +1 -1
- package/dist/client/chunks/{client-p0jqzfvc.js → client-whph3y67.js} +1 -1
- package/dist/client/chunks/{client-a7spxfnn.js → client-xf7r768c.js} +13 -1
- package/dist/client/define-route.d.ts +12 -0
- package/dist/client/internal/runtime-types.d.ts +7 -0
- package/dist/client/link.js +200 -40
- package/dist/client/route-element.d.ts +5 -0
- package/dist/client/router/history.d.ts +6 -0
- package/dist/client/router/link-utils.d.ts +7 -0
- package/dist/client/router/navigation-controller.d.ts +20 -2
- package/dist/client/router/pending.d.ts +17 -0
- package/dist/client/router/search/index.d.ts +2 -0
- package/dist/client/router/search/index.js +7 -3
- package/dist/client/router/types.d.ts +21 -1
- package/dist/client.js +19 -4
- package/dist/define-route.d.ts +37 -10
- package/dist/furin.js +88 -29
- package/dist/plugin/client-plugin.d.ts +1 -1
- package/dist/plugin/index.js +36 -17
- package/dist/plugin/route-manifest.d.ts +2 -1
- package/dist/plugin/strip-server-stages.d.ts +1 -1
- package/dist/server/internal.d.ts +1 -0
- package/dist/server/internal.js +42 -15
- package/dist/server/router/defined-route.d.ts +3 -0
- package/dist/server/router/types.d.ts +7 -1
- package/dist/server/static-export.js +45 -15
- package/dist/shared/pending.d.ts +13 -0
- package/dist/shared/search-params.d.ts +1 -0
- package/package.json +1 -1
- package/src/adapter/runtime-build.ts +4 -0
- package/src/adapter/vercel.ts +21 -8
- package/src/build/hydrate.ts +11 -2
- package/src/build/preload-manifest.ts +3 -1
- package/src/build/route-metadata.ts +3 -2
- package/src/client/define-route.ts +23 -3
- package/src/client/internal/runtime-types.ts +7 -0
- package/src/client/link.tsx +26 -4
- package/src/client/route-element.tsx +41 -15
- package/src/client/router/context.ts +29 -8
- package/src/client/router/history.ts +10 -0
- package/src/client/router/link-utils.ts +23 -0
- package/src/client/router/navigation-controller.ts +239 -19
- package/src/client/router/pending.ts +58 -0
- package/src/client/router/provider.tsx +5 -2
- package/src/client/router/search/index.ts +7 -2
- package/src/client/router/types.ts +29 -3
- package/src/define-route.ts +74 -26
- package/src/plugin/client-plugin.ts +5 -4
- package/src/plugin/route-config.ts +9 -2
- package/src/plugin/route-manifest.ts +11 -3
- package/src/plugin/route-tree.ts +2 -2
- package/src/plugin/strip-server-stages.ts +60 -35
- package/src/server/internal.ts +1 -0
- package/src/server/router/defined-route.ts +6 -0
- package/src/server/router/discovery.ts +5 -2
- package/src/server/router/types.ts +7 -1
- package/src/shared/pending.ts +16 -0
- package/src/shared/search-params.ts +1 -1
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
import { type Context, createContext, use, useCallback, useContext, useMemo } from "react";
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
applyLinkParams,
|
|
4
|
+
navigationHrefPolicy,
|
|
5
|
+
type RuntimeRouteTarget,
|
|
6
|
+
resolveRouteTarget,
|
|
7
|
+
resolveTargetSearch,
|
|
8
|
+
} from "./link-utils.ts";
|
|
3
9
|
import type {
|
|
4
10
|
NavigationOptions,
|
|
5
11
|
Prefetch,
|
|
@@ -8,6 +14,7 @@ import type {
|
|
|
8
14
|
RouterNavigate,
|
|
9
15
|
RouterStore,
|
|
10
16
|
StableRouter,
|
|
17
|
+
TargetNavigationOptions,
|
|
11
18
|
} from "./types.ts";
|
|
12
19
|
|
|
13
20
|
// Duplicate module copies (separate entrypoints, client references) must share
|
|
@@ -80,10 +87,18 @@ export function useRouterStore(): RouterStore {
|
|
|
80
87
|
return { getCurrentHref: () => value.currentHref, router: value, subscribe: subscribeNever };
|
|
81
88
|
}
|
|
82
89
|
|
|
90
|
+
function targetSearch(target: RuntimeRouteTarget, routes: StableRouter["searchRoutes"]) {
|
|
91
|
+
return resolveTargetSearch(
|
|
92
|
+
applyLinkParams(target.to, target.params as Record<string, string | number> | undefined),
|
|
93
|
+
target.search,
|
|
94
|
+
routes
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
|
|
83
98
|
export function useTypedNavigation(context: StableRouter): Pick<Router, "navigate" | "prefetch"> {
|
|
84
99
|
const { navigate: navigateHref, prefetch: prefetchHref, searchRoutes } = context;
|
|
85
100
|
const navigate: RouterNavigate = useCallback(
|
|
86
|
-
(next: string | (RuntimeRouteTarget &
|
|
101
|
+
(next: string | (RuntimeRouteTarget & TargetNavigationOptions), opts?: NavigationOptions) => {
|
|
87
102
|
const href = typeof next === "string" ? next : resolveRouteTarget(next, searchRoutes);
|
|
88
103
|
const policy = navigationHrefPolicy(
|
|
89
104
|
href,
|
|
@@ -99,11 +114,12 @@ export function useTypedNavigation(context: StableRouter): Pick<Router, "navigat
|
|
|
99
114
|
if (typeof next === "string") {
|
|
100
115
|
return navigateHref(href, opts);
|
|
101
116
|
}
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
117
|
+
return navigateHref(href, {
|
|
118
|
+
replace: next.replace,
|
|
119
|
+
resetScroll: next.resetScroll,
|
|
120
|
+
search: targetSearch(next, searchRoutes),
|
|
121
|
+
shallow: next.shallow,
|
|
122
|
+
});
|
|
107
123
|
},
|
|
108
124
|
[navigateHref, searchRoutes]
|
|
109
125
|
);
|
|
@@ -120,7 +136,12 @@ export function useTypedNavigation(context: StableRouter): Pick<Router, "navigat
|
|
|
120
136
|
if (policy !== "internal") {
|
|
121
137
|
return;
|
|
122
138
|
}
|
|
123
|
-
prefetchHref(
|
|
139
|
+
prefetchHref(
|
|
140
|
+
href,
|
|
141
|
+
typeof next === "string"
|
|
142
|
+
? opts
|
|
143
|
+
: { search: targetSearch(next, searchRoutes), staleTime: next.staleTime }
|
|
144
|
+
);
|
|
124
145
|
},
|
|
125
146
|
[prefetchHref, searchRoutes]
|
|
126
147
|
);
|
|
@@ -1,9 +1,19 @@
|
|
|
1
|
+
import type { SearchParamsInput } from "../../shared/search-params.ts";
|
|
2
|
+
|
|
1
3
|
export const SCROLL_STORAGE_KEY = "__furin_scroll__";
|
|
2
4
|
|
|
3
5
|
export function getHistoryKey(state: unknown): string | undefined {
|
|
4
6
|
return (state as { _furinKey?: string } | null)?._furinKey;
|
|
5
7
|
}
|
|
6
8
|
|
|
9
|
+
/**
|
|
10
|
+
* Search furin recorded for the page committed at a history entry: the
|
|
11
|
+
* server-validated query, or the typed search an in-place commit was given.
|
|
12
|
+
*/
|
|
13
|
+
export function getHistorySearch(state: unknown): SearchParamsInput | undefined {
|
|
14
|
+
return (state as { _furinSearch?: SearchParamsInput } | null)?._furinSearch;
|
|
15
|
+
}
|
|
16
|
+
|
|
7
17
|
export function generateHistoryKey(): string {
|
|
8
18
|
return Math.random().toString(36).slice(2, 9);
|
|
9
19
|
}
|
|
@@ -132,6 +132,29 @@ export function resolveRouteTarget(
|
|
|
132
132
|
);
|
|
133
133
|
}
|
|
134
134
|
|
|
135
|
+
/**
|
|
136
|
+
* The validated search a structured destination will read, as far as the
|
|
137
|
+
* client knows it: `search` over the route's schema defaults, empty values
|
|
138
|
+
* dropped like the URL drops them. Undefined when `path` carries its own
|
|
139
|
+
* query string, which only the server parses.
|
|
140
|
+
*/
|
|
141
|
+
export function resolveTargetSearch(
|
|
142
|
+
path: string,
|
|
143
|
+
search: object | null | undefined,
|
|
144
|
+
routes: SearchRouteMetadata[]
|
|
145
|
+
): SearchParamsInput | undefined {
|
|
146
|
+
if (path.includes("?")) {
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
const resolved: SearchParamsInput = { ...findSearchDefaultsForRouteTarget(path, routes) };
|
|
150
|
+
for (const [key, value] of Object.entries(search ?? {})) {
|
|
151
|
+
if (value !== null && value !== undefined) {
|
|
152
|
+
resolved[key] = value;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
return resolved;
|
|
156
|
+
}
|
|
157
|
+
|
|
135
158
|
/**
|
|
136
159
|
* Builds a full href string from a pathname, optional search params, and optional hash.
|
|
137
160
|
* Null/undefined search values are omitted.
|
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
import { log } from "evlog";
|
|
2
|
+
import { deepEqual, type SearchParamsInput } from "../../shared/search-params.ts";
|
|
2
3
|
import { CacheGeneration } from "./cache-generation.ts";
|
|
3
|
-
import {
|
|
4
|
+
import {
|
|
5
|
+
generateHistoryKey,
|
|
6
|
+
getHistoryKey,
|
|
7
|
+
getHistorySearch,
|
|
8
|
+
saveScrollPosition,
|
|
9
|
+
} from "./history.ts";
|
|
4
10
|
import {
|
|
5
11
|
normalizeHref,
|
|
6
12
|
shouldAutoRefreshPath,
|
|
@@ -9,6 +15,7 @@ import {
|
|
|
9
15
|
} from "./link-utils.ts";
|
|
10
16
|
import type { OptimisticRuntime } from "./optimistic.ts";
|
|
11
17
|
import type { PageResult } from "./page-fetch.ts";
|
|
18
|
+
import type { PendingScreen } from "./pending.ts";
|
|
12
19
|
import type { LoadedClientRoute, RouterState } from "./types.ts";
|
|
13
20
|
|
|
14
21
|
/** What React renders: the committed page plus navigation status. */
|
|
@@ -16,6 +23,8 @@ export interface NavigationSnapshot {
|
|
|
16
23
|
/** Logical href (basePath stripped) of the committed page. */
|
|
17
24
|
href: string;
|
|
18
25
|
isNavigating: boolean;
|
|
26
|
+
/** Pending screen shown in place of the committed page while a slow navigation loads. */
|
|
27
|
+
pending?: PendingScreen;
|
|
19
28
|
/** Bumped on every non-error commit so error boundaries reset. */
|
|
20
29
|
resetVersion: number;
|
|
21
30
|
state: RouterState;
|
|
@@ -33,19 +42,33 @@ export interface NavigationControllerConfig {
|
|
|
33
42
|
signal: AbortSignal | undefined;
|
|
34
43
|
}) => Promise<PageResult>;
|
|
35
44
|
maxCacheSize: number;
|
|
45
|
+
/** Pending screen of the route `href` targets, measured against the committed match. */
|
|
46
|
+
resolvePending: (request: {
|
|
47
|
+
currentMatch: LoadedClientRoute | null;
|
|
48
|
+
href: string;
|
|
49
|
+
}) => Promise<PendingScreen | undefined>;
|
|
36
50
|
}
|
|
37
51
|
|
|
38
52
|
export interface NavigateRequest {
|
|
39
53
|
/** Runs right before the new snapshot is published (HMR swaps components here). */
|
|
40
54
|
beforeCommit?: () => void;
|
|
41
55
|
hmrRefresh?: boolean;
|
|
56
|
+
/** `false` keeps the current page on screen instead of the target's pending screen. */
|
|
57
|
+
pending?: boolean;
|
|
42
58
|
replace?: boolean;
|
|
43
59
|
resetScroll?: boolean;
|
|
60
|
+
/**
|
|
61
|
+
* Validated search the destination will read. Only with it can a page
|
|
62
|
+
* already loaded for the same path be reused (see `loaderDeps`, `shallow`).
|
|
63
|
+
*/
|
|
64
|
+
search?: SearchParamsInput;
|
|
65
|
+
/** Same path: commit the new URL and search in place without running loaders. */
|
|
66
|
+
shallow?: boolean;
|
|
44
67
|
/** Returning `false` discards the navigation result instead of committing it. */
|
|
45
68
|
shouldCommit?: () => boolean;
|
|
46
69
|
}
|
|
47
70
|
|
|
48
|
-
export type RefreshRequest = Omit<NavigateRequest, "replace">;
|
|
71
|
+
export type RefreshRequest = Omit<NavigateRequest, "replace" | "search" | "shallow">;
|
|
49
72
|
|
|
50
73
|
export type ScrollInstruction = { type: "restore"; key: string } | { type: "reset"; href: string };
|
|
51
74
|
|
|
@@ -61,6 +84,8 @@ interface FetchedPage {
|
|
|
61
84
|
interface PageCacheEntry {
|
|
62
85
|
createdAt: number;
|
|
63
86
|
promise: Promise<FetchedPage>;
|
|
87
|
+
/** The page once `promise` resolved, so it can be matched synchronously. */
|
|
88
|
+
settled?: FetchedPage;
|
|
64
89
|
staleTime: number;
|
|
65
90
|
}
|
|
66
91
|
|
|
@@ -80,6 +105,36 @@ function isExpired(entry: PageCacheEntry): boolean {
|
|
|
80
105
|
return Date.now() - entry.createdAt > entry.staleTime;
|
|
81
106
|
}
|
|
82
107
|
|
|
108
|
+
function pathnameOf(href: string): string {
|
|
109
|
+
return normalizeHref(new URL(href, "http://furin.local").pathname);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* True when `state` is a page loaded for `href`'s path whose route declares
|
|
114
|
+
* `loaderDeps` and reads the same dependencies from `search`.
|
|
115
|
+
*/
|
|
116
|
+
function sharesLoaderDeps(
|
|
117
|
+
state: RouterState,
|
|
118
|
+
stateHref: string,
|
|
119
|
+
href: string,
|
|
120
|
+
search: SearchParamsInput
|
|
121
|
+
): boolean {
|
|
122
|
+
const loaderDeps = state.match?.pageRoute.loaderDeps;
|
|
123
|
+
return (
|
|
124
|
+
loaderDeps !== undefined &&
|
|
125
|
+
pathnameOf(stateHref) === pathnameOf(href) &&
|
|
126
|
+
deepEqual(
|
|
127
|
+
loaderDeps((state.data.query as SearchParamsInput | undefined) ?? {}),
|
|
128
|
+
loaderDeps(search)
|
|
129
|
+
)
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** The same loaded page, read with another search. */
|
|
134
|
+
function withSearch(state: RouterState, search: SearchParamsInput): RouterState {
|
|
135
|
+
return { ...state, data: { ...state.data, query: search }, finalHref: undefined };
|
|
136
|
+
}
|
|
137
|
+
|
|
83
138
|
function historyStateObject(): object {
|
|
84
139
|
const value = history.state;
|
|
85
140
|
return value !== null && typeof value === "object" ? value : {};
|
|
@@ -107,6 +162,8 @@ export class NavigationController {
|
|
|
107
162
|
#pendingUserNavigation: { promise: Promise<void>; resolve: () => void } | null = null;
|
|
108
163
|
#renderedHistoryKey: string | undefined;
|
|
109
164
|
#pendingScroll: ScrollInstruction | null = null;
|
|
165
|
+
/** When the current pending screen must stay until, at the earliest. */
|
|
166
|
+
#pendingUntil = 0;
|
|
110
167
|
|
|
111
168
|
constructor(options: {
|
|
112
169
|
config: NavigationControllerConfig;
|
|
@@ -156,15 +213,23 @@ export class NavigationController {
|
|
|
156
213
|
return this.navigate(href, {
|
|
157
214
|
beforeCommit: request?.beforeCommit,
|
|
158
215
|
hmrRefresh: request?.hmrRefresh,
|
|
216
|
+
pending: false,
|
|
159
217
|
replace: true,
|
|
160
218
|
resetScroll: request?.resetScroll ?? false,
|
|
161
219
|
shouldCommit: request?.shouldCommit,
|
|
162
220
|
});
|
|
163
221
|
};
|
|
164
222
|
|
|
165
|
-
readonly prefetch = (
|
|
223
|
+
readonly prefetch = (
|
|
224
|
+
href: string,
|
|
225
|
+
staleTime: number | undefined,
|
|
226
|
+
search?: SearchParamsInput
|
|
227
|
+
): void => {
|
|
166
228
|
const existing = this.#cache.get(href);
|
|
167
|
-
if (
|
|
229
|
+
if (
|
|
230
|
+
(existing && !isExpired(existing)) ||
|
|
231
|
+
(search && (this.#inPlace(href, search, false) || this.#sharedPage(href, search)))
|
|
232
|
+
) {
|
|
168
233
|
return;
|
|
169
234
|
}
|
|
170
235
|
const generation = this.#generation.current;
|
|
@@ -256,9 +321,14 @@ export class NavigationController {
|
|
|
256
321
|
/** Keys the initial history entry so its scroll position can be restored. */
|
|
257
322
|
adoptHistoryEntry(): void {
|
|
258
323
|
history.scrollRestoration = "manual";
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
324
|
+
history.replaceState(
|
|
325
|
+
{
|
|
326
|
+
...historyStateObject(),
|
|
327
|
+
_furinKey: getHistoryKey(history.state) ?? generateHistoryKey(),
|
|
328
|
+
_furinSearch: this.#snapshot.state.data.query,
|
|
329
|
+
},
|
|
330
|
+
""
|
|
331
|
+
);
|
|
262
332
|
this.#renderedHistoryKey = getHistoryKey(history.state);
|
|
263
333
|
}
|
|
264
334
|
|
|
@@ -280,11 +350,71 @@ export class NavigationController {
|
|
|
280
350
|
|
|
281
351
|
#setNavigating(isNavigating: boolean): void {
|
|
282
352
|
if (this.#snapshot.isNavigating !== isNavigating) {
|
|
283
|
-
this.#publish({
|
|
353
|
+
this.#publish({
|
|
354
|
+
...this.#snapshot,
|
|
355
|
+
isNavigating,
|
|
356
|
+
pending: isNavigating ? this.#snapshot.pending : undefined,
|
|
357
|
+
});
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
/**
|
|
362
|
+
* Shows the target's pending screen once the navigation outlasts its `ms`,
|
|
363
|
+
* counted from the navigation start so a slow route module does not delay it.
|
|
364
|
+
* A superseding navigation to the same screen keeps it up without a blink.
|
|
365
|
+
*/
|
|
366
|
+
#schedulePending(href: string, kind: NavigationKind, version: number): void {
|
|
367
|
+
const startedAt = Date.now();
|
|
368
|
+
const wanted =
|
|
369
|
+
kind.type === "pop" || !(kind.request.hmrRefresh || kind.request.pending === false);
|
|
370
|
+
const screen = wanted
|
|
371
|
+
? this.#config.resolvePending({ currentMatch: this.#currentMatch(), href })
|
|
372
|
+
: Promise.resolve(undefined);
|
|
373
|
+
screen
|
|
374
|
+
.then((resolved) => {
|
|
375
|
+
const shown = this.#snapshot.pending;
|
|
376
|
+
if (
|
|
377
|
+
this.#version !== version ||
|
|
378
|
+
(shown && shown.component === resolved?.component && shown.depth === resolved.depth)
|
|
379
|
+
) {
|
|
380
|
+
return;
|
|
381
|
+
}
|
|
382
|
+
if (shown) {
|
|
383
|
+
this.#publish({ ...this.#snapshot, pending: undefined });
|
|
384
|
+
}
|
|
385
|
+
if (!resolved) {
|
|
386
|
+
return;
|
|
387
|
+
}
|
|
388
|
+
setTimeout(
|
|
389
|
+
() => {
|
|
390
|
+
if (this.#version === version && this.#snapshot.isNavigating) {
|
|
391
|
+
this.#pendingUntil = Date.now() + resolved.minMs;
|
|
392
|
+
this.#publish({ ...this.#snapshot, pending: resolved });
|
|
393
|
+
}
|
|
394
|
+
},
|
|
395
|
+
startedAt + resolved.ms - Date.now()
|
|
396
|
+
);
|
|
397
|
+
})
|
|
398
|
+
.catch((error: unknown) => {
|
|
399
|
+
log.warn({ action: "pending_resolve_failed", error: String(error), href });
|
|
400
|
+
});
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
/** Keeps a shown pending screen up for its `minMs`. */
|
|
404
|
+
async #holdPending(): Promise<void> {
|
|
405
|
+
const remaining = this.#snapshot.pending ? this.#pendingUntil - Date.now() : 0;
|
|
406
|
+
if (remaining > 0) {
|
|
407
|
+
await new Promise((resolve) => setTimeout(resolve, remaining));
|
|
284
408
|
}
|
|
285
409
|
}
|
|
286
410
|
|
|
287
411
|
#store(href: string, entry: PageCacheEntry): void {
|
|
412
|
+
entry.promise.then(
|
|
413
|
+
(page) => {
|
|
414
|
+
entry.settled = page;
|
|
415
|
+
},
|
|
416
|
+
() => undefined
|
|
417
|
+
);
|
|
288
418
|
this.#cache.delete(href);
|
|
289
419
|
this.#cache.set(href, entry);
|
|
290
420
|
if (this.#cache.size > this.#config.maxCacheSize) {
|
|
@@ -327,6 +457,16 @@ export class NavigationController {
|
|
|
327
457
|
}
|
|
328
458
|
|
|
329
459
|
async #run(href: string, kind: NavigationKind): Promise<void> {
|
|
460
|
+
const search = kind.type === "push" ? kind.request.search : getHistorySearch(history.state);
|
|
461
|
+
const shallow = kind.type === "push" && kind.request.shallow === true;
|
|
462
|
+
if (
|
|
463
|
+
search &&
|
|
464
|
+
this.#inPlace(href, search, shallow) &&
|
|
465
|
+
!(kind.type === "push" && kind.request.shouldCommit?.() === false)
|
|
466
|
+
) {
|
|
467
|
+
this.#commitInPlace(href, kind, search);
|
|
468
|
+
return;
|
|
469
|
+
}
|
|
330
470
|
const hmrRefresh = kind.type === "push" && kind.request.hmrRefresh === true;
|
|
331
471
|
const finishUserNavigation = hmrRefresh ? undefined : this.#beginUserNavigation();
|
|
332
472
|
this.#version += 1;
|
|
@@ -337,12 +477,13 @@ export class NavigationController {
|
|
|
337
477
|
abort.signal.addEventListener("abort", onAbort, { once: true });
|
|
338
478
|
let committed = false;
|
|
339
479
|
this.#setNavigating(true);
|
|
480
|
+
this.#schedulePending(href, kind, version);
|
|
340
481
|
try {
|
|
341
482
|
if (abort.signal.aborted) {
|
|
342
483
|
return;
|
|
343
484
|
}
|
|
344
485
|
committed = await Promise.race([
|
|
345
|
-
this.#load(href, kind, version, abort.signal),
|
|
486
|
+
this.#load(href, kind, version, abort.signal, search),
|
|
346
487
|
cancelled.promise,
|
|
347
488
|
]);
|
|
348
489
|
} finally {
|
|
@@ -365,12 +506,14 @@ export class NavigationController {
|
|
|
365
506
|
href: string,
|
|
366
507
|
kind: NavigationKind,
|
|
367
508
|
version: number,
|
|
368
|
-
signal: AbortSignal
|
|
509
|
+
signal: AbortSignal,
|
|
510
|
+
search: SearchParamsInput | undefined
|
|
369
511
|
): Promise<boolean> {
|
|
370
512
|
const loaded = await this.#read(
|
|
371
513
|
href,
|
|
372
514
|
signal,
|
|
373
|
-
kind.type === "push" && kind.request.hmrRefresh === true
|
|
515
|
+
kind.type === "push" && kind.request.hmrRefresh === true,
|
|
516
|
+
search
|
|
374
517
|
);
|
|
375
518
|
if (!this.#accepts(kind, version)) {
|
|
376
519
|
return false;
|
|
@@ -405,6 +548,7 @@ export class NavigationController {
|
|
|
405
548
|
this.#hardNavigate(kind, href);
|
|
406
549
|
return false;
|
|
407
550
|
}
|
|
551
|
+
await this.#holdPending();
|
|
408
552
|
if (!this.#accepts(kind, version)) {
|
|
409
553
|
return false;
|
|
410
554
|
}
|
|
@@ -422,7 +566,8 @@ export class NavigationController {
|
|
|
422
566
|
async #read(
|
|
423
567
|
href: string,
|
|
424
568
|
signal: AbortSignal,
|
|
425
|
-
hmrRefresh: boolean
|
|
569
|
+
hmrRefresh: boolean,
|
|
570
|
+
search: SearchParamsInput | undefined
|
|
426
571
|
): Promise<{ fetched: boolean; page: FetchedPage }> {
|
|
427
572
|
const cached = this.#cache.get(href);
|
|
428
573
|
if (cached && !isExpired(cached) && !this.#optimistic.has(href)) {
|
|
@@ -431,9 +576,74 @@ export class NavigationController {
|
|
|
431
576
|
return { fetched: false, page };
|
|
432
577
|
}
|
|
433
578
|
}
|
|
579
|
+
const shared = search && this.#sharedPage(href, search);
|
|
580
|
+
if (shared) {
|
|
581
|
+
return { fetched: false, page: shared };
|
|
582
|
+
}
|
|
434
583
|
return { fetched: true, page: await this.#fetchConfirmed(href, signal, hmrRefresh) };
|
|
435
584
|
}
|
|
436
585
|
|
|
586
|
+
/** Whether the committed page can take `href` without loading: same path, and loader deps kept or `shallow`. */
|
|
587
|
+
#inPlace(href: string, search: SearchParamsInput, shallow: boolean): boolean {
|
|
588
|
+
const { href: committedHref, state } = this.#snapshot;
|
|
589
|
+
return (
|
|
590
|
+
state.match !== null &&
|
|
591
|
+
!(state.error || state.notFound) &&
|
|
592
|
+
!this.#optimistic.has(committedHref) &&
|
|
593
|
+
(shallow
|
|
594
|
+
? pathnameOf(committedHref) === pathnameOf(href)
|
|
595
|
+
: sharesLoaderDeps(state, committedHref, href, search))
|
|
596
|
+
);
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
/** A cached page for another search of `href`'s path that reads the same loader deps. */
|
|
600
|
+
#sharedPage(href: string, search: SearchParamsInput): FetchedPage | undefined {
|
|
601
|
+
if (this.#optimistic.has(href)) {
|
|
602
|
+
return;
|
|
603
|
+
}
|
|
604
|
+
for (const [cachedHref, entry] of this.#cache) {
|
|
605
|
+
const page = entry.settled;
|
|
606
|
+
if (
|
|
607
|
+
page?.result.kind === "page" &&
|
|
608
|
+
!isExpired(entry) &&
|
|
609
|
+
this.#isReusable(cachedHref, page) &&
|
|
610
|
+
sharesLoaderDeps(page.result.state, cachedHref, href, search)
|
|
611
|
+
) {
|
|
612
|
+
return {
|
|
613
|
+
...page,
|
|
614
|
+
result: { ...page.result, state: withSearch(page.result.state, search) },
|
|
615
|
+
};
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
/** Publishes the committed page under the new URL and search; supersedes pending navigations. */
|
|
621
|
+
#commitInPlace(href: string, kind: NavigationKind, search: SearchParamsInput): void {
|
|
622
|
+
this.#beginUserNavigation()();
|
|
623
|
+
this.#version += 1;
|
|
624
|
+
this.#pendingAbort?.abort();
|
|
625
|
+
this.#pendingAbort = null;
|
|
626
|
+
const committedHref =
|
|
627
|
+
kind.type === "push"
|
|
628
|
+
? this.#commitPush(
|
|
629
|
+
{
|
|
630
|
+
...kind.request,
|
|
631
|
+
// The page stays: keep the scroll position unless a hash asks to move.
|
|
632
|
+
resetScroll: kind.request.resetScroll ?? href.includes("#"),
|
|
633
|
+
},
|
|
634
|
+
href,
|
|
635
|
+
search
|
|
636
|
+
)
|
|
637
|
+
: this.#commitPop(kind.key, href, href, search);
|
|
638
|
+
this.#publish({
|
|
639
|
+
...this.#snapshot,
|
|
640
|
+
href: committedHref,
|
|
641
|
+
isNavigating: false,
|
|
642
|
+
pending: undefined,
|
|
643
|
+
state: withSearch(this.#snapshot.state, search),
|
|
644
|
+
});
|
|
645
|
+
}
|
|
646
|
+
|
|
437
647
|
/** A cached page is reusable only when nothing it depends on changed since its fetch. */
|
|
438
648
|
#isReusable(href: string, page: FetchedPage): boolean {
|
|
439
649
|
const { result } = page;
|
|
@@ -494,7 +704,7 @@ export class NavigationController {
|
|
|
494
704
|
}
|
|
495
705
|
visited.add(href);
|
|
496
706
|
// biome-ignore lint/performance/noAwaitInLoops: each redirect target depends on the previous response.
|
|
497
|
-
({ page } = await this.#read(href, signal, false));
|
|
707
|
+
({ page } = await this.#read(href, signal, false, undefined));
|
|
498
708
|
}
|
|
499
709
|
return isRenderable(page.result) ? { href, page, result: page.result } : null;
|
|
500
710
|
}
|
|
@@ -522,8 +732,8 @@ export class NavigationController {
|
|
|
522
732
|
}
|
|
523
733
|
const href =
|
|
524
734
|
kind.type === "push"
|
|
525
|
-
? this.#commitPush(kind.request, effective)
|
|
526
|
-
: this.#commitPop(kind.key, effective, requestedHref);
|
|
735
|
+
? this.#commitPush(kind.request, effective, state.data.query)
|
|
736
|
+
: this.#commitPop(kind.key, effective, requestedHref, state.data.query);
|
|
527
737
|
this.#publish({
|
|
528
738
|
href,
|
|
529
739
|
isNavigating: false,
|
|
@@ -540,7 +750,7 @@ export class NavigationController {
|
|
|
540
750
|
}
|
|
541
751
|
|
|
542
752
|
/** Pushes (or replaces) the history entry and returns the committed logical href. */
|
|
543
|
-
#commitPush(request: NavigateRequest, effective: string): string {
|
|
753
|
+
#commitPush(request: NavigateRequest, effective: string, search: unknown): string {
|
|
544
754
|
const { basePath } = this.#config;
|
|
545
755
|
const physical = basePath + effective;
|
|
546
756
|
if (request.resetScroll ?? true) {
|
|
@@ -551,6 +761,7 @@ export class NavigationController {
|
|
|
551
761
|
{
|
|
552
762
|
...historyStateObject(),
|
|
553
763
|
_furinKey: getHistoryKey(history.state) ?? generateHistoryKey(),
|
|
764
|
+
_furinSearch: search,
|
|
554
765
|
},
|
|
555
766
|
"",
|
|
556
767
|
physical
|
|
@@ -559,7 +770,7 @@ export class NavigationController {
|
|
|
559
770
|
if (this.#renderedHistoryKey) {
|
|
560
771
|
saveScrollPosition(this.#renderedHistoryKey);
|
|
561
772
|
}
|
|
562
|
-
history.pushState({ _furinKey: generateHistoryKey() }, "", physical);
|
|
773
|
+
history.pushState({ _furinKey: generateHistoryKey(), _furinSearch: search }, "", physical);
|
|
563
774
|
}
|
|
564
775
|
this.#renderedHistoryKey = getHistoryKey(history.state);
|
|
565
776
|
const url = new URL(physical, window.location.origin);
|
|
@@ -567,9 +778,18 @@ export class NavigationController {
|
|
|
567
778
|
}
|
|
568
779
|
|
|
569
780
|
/** Adopts the popped entry (canonicalised after redirects) and returns the committed logical href. */
|
|
570
|
-
#commitPop(
|
|
781
|
+
#commitPop(
|
|
782
|
+
key: string | undefined,
|
|
783
|
+
effective: string,
|
|
784
|
+
requestedHref: string,
|
|
785
|
+
search: unknown
|
|
786
|
+
): string {
|
|
571
787
|
if (effective !== requestedHref) {
|
|
572
|
-
history.replaceState(
|
|
788
|
+
history.replaceState(
|
|
789
|
+
{ ...historyStateObject(), _furinSearch: search },
|
|
790
|
+
"",
|
|
791
|
+
this.#config.basePath + effective
|
|
792
|
+
);
|
|
573
793
|
}
|
|
574
794
|
this.#renderedHistoryKey = key;
|
|
575
795
|
if (key) {
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type { PendingComponent } from "../../shared/pending.ts";
|
|
2
|
+
import { collectRouteChainFromRoute } from "../../shared/utils/index.ts";
|
|
3
|
+
import type { ClientRoute, LoadedClientRoute } from "./types.ts";
|
|
4
|
+
|
|
5
|
+
const DEFAULT_PENDING_MS = 1000;
|
|
6
|
+
const DEFAULT_PENDING_MIN_MS = 500;
|
|
7
|
+
|
|
8
|
+
/** A pending screen to render inside the committed layout at `depth`. */
|
|
9
|
+
export interface PendingScreen {
|
|
10
|
+
component: PendingComponent;
|
|
11
|
+
depth: number;
|
|
12
|
+
minMs: number;
|
|
13
|
+
ms: number;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* The pending screen a navigation to `href` shows, if any. It renders inside
|
|
18
|
+
* the deepest layout the target shares with the current page, in place of
|
|
19
|
+
* the first segment that changes, as in TanStack Router (only the matches
|
|
20
|
+
* being loaded show their pending) and Next.js (a `loading.tsx` falls back
|
|
21
|
+
* only when its child segment changes). The deepest declaration wins.
|
|
22
|
+
*/
|
|
23
|
+
export async function resolvePendingScreen(
|
|
24
|
+
routes: readonly ClientRoute[],
|
|
25
|
+
href: string,
|
|
26
|
+
current: LoadedClientRoute | null
|
|
27
|
+
): Promise<PendingScreen | undefined> {
|
|
28
|
+
const { pathname } = new URL(href, "http://furin.local");
|
|
29
|
+
const route = routes.find((candidate) => candidate.regex.test(pathname));
|
|
30
|
+
if (!(route && current)) {
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
const loaded = await route.load();
|
|
34
|
+
const target = collectRouteChainFromRoute(loaded.default._route);
|
|
35
|
+
const committed = collectRouteChainFromRoute(current.pageRoute);
|
|
36
|
+
// The last entry of a chain is the page itself, not a layout.
|
|
37
|
+
let shared = 0;
|
|
38
|
+
while (
|
|
39
|
+
shared < target.length - 1 &&
|
|
40
|
+
shared < committed.length - 1 &&
|
|
41
|
+
target[shared]?.layout === committed[shared]?.layout
|
|
42
|
+
) {
|
|
43
|
+
shared += 1;
|
|
44
|
+
}
|
|
45
|
+
const boundary = (loaded.segmentBoundaries ?? route.segmentBoundaries)?.findLast(
|
|
46
|
+
(candidate) => candidate.pending !== undefined && candidate.depth === shared - 1
|
|
47
|
+
);
|
|
48
|
+
if (!boundary?.pending) {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
const { component, minMs, ms } = boundary.pending;
|
|
52
|
+
return {
|
|
53
|
+
component,
|
|
54
|
+
depth: boundary.depth,
|
|
55
|
+
minMs: minMs ?? DEFAULT_PENDING_MIN_MS,
|
|
56
|
+
ms: ms ?? DEFAULT_PENDING_MS,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
@@ -36,6 +36,7 @@ import {
|
|
|
36
36
|
} from "./navigation-controller.ts";
|
|
37
37
|
import { createOptimisticRuntime, registerOptimisticRuntime } from "./optimistic.ts";
|
|
38
38
|
import { fetchPageState } from "./page-fetch.ts";
|
|
39
|
+
import { resolvePendingScreen } from "./pending.ts";
|
|
39
40
|
import { createSearchStore, SearchStoreContext } from "./search-store.ts";
|
|
40
41
|
import { detectStaticMode } from "./spa-response.ts";
|
|
41
42
|
import type {
|
|
@@ -162,6 +163,7 @@ export function RouterProvider({
|
|
|
162
163
|
hmrRefresh
|
|
163
164
|
),
|
|
164
165
|
maxCacheSize: prefetchCacheSize,
|
|
166
|
+
resolvePending: ({ currentMatch, href }) => resolvePendingScreen(routes, href, currentMatch),
|
|
165
167
|
}),
|
|
166
168
|
[
|
|
167
169
|
basePath,
|
|
@@ -192,8 +194,8 @@ export function RouterProvider({
|
|
|
192
194
|
const { href: currentHref, isNavigating, resetVersion: boundaryResetVersion, state } = navigation;
|
|
193
195
|
const { navigate, refresh, invalidatePath: invalidatePrefetch } = controller;
|
|
194
196
|
const prefetch = useCallback(
|
|
195
|
-
(href: string, opts: { staleTime?: number } | undefined) => {
|
|
196
|
-
controller.prefetch(href, opts?.staleTime);
|
|
197
|
+
(href: string, opts: { search?: SearchParamsInput; staleTime?: number } | undefined) => {
|
|
198
|
+
controller.prefetch(href, opts?.staleTime, opts?.search);
|
|
197
199
|
},
|
|
198
200
|
[controller]
|
|
199
201
|
);
|
|
@@ -407,6 +409,7 @@ export function RouterProvider({
|
|
|
407
409
|
},
|
|
408
410
|
chain,
|
|
409
411
|
data: renderedData,
|
|
412
|
+
pending: navigation.pending,
|
|
410
413
|
page: state.match
|
|
411
414
|
? { component: state.match.component, pattern: state.match.pattern }
|
|
412
415
|
: undefined,
|
|
@@ -4,7 +4,7 @@ import {
|
|
|
4
4
|
findSearchDefaultsForRouteTarget,
|
|
5
5
|
type SearchParamsInput,
|
|
6
6
|
} from "../../../shared/search-params.ts";
|
|
7
|
-
import { buildHref } from "../link-utils.ts";
|
|
7
|
+
import { buildHref, resolveTargetSearch } from "../link-utils.ts";
|
|
8
8
|
import {
|
|
9
9
|
FALLBACK_SEARCH_STORE,
|
|
10
10
|
type SearchStore,
|
|
@@ -44,6 +44,8 @@ export type SetSearchInput<To extends SearchRouteTo> =
|
|
|
44
44
|
export interface SetSearchOptions {
|
|
45
45
|
replace?: boolean;
|
|
46
46
|
resetScroll?: boolean;
|
|
47
|
+
/** See `TargetNavigationOptions.shallow`. */
|
|
48
|
+
shallow?: boolean;
|
|
47
49
|
}
|
|
48
50
|
|
|
49
51
|
export type SetSearch<To extends SearchRouteTo> = (
|
|
@@ -124,7 +126,10 @@ export function useSearch<To extends SearchRouteTo, TSelected>(
|
|
|
124
126
|
const pathname = pathnameFromLogicalHref(snapshot.currentHref);
|
|
125
127
|
const searchDefaults = findSearchDefaultsForRouteTarget(pathname, snapshot.searchRoutes);
|
|
126
128
|
const href = buildHref(pathname, merged, undefined, searchDefaults);
|
|
127
|
-
return snapshot.navigate(href,
|
|
129
|
+
return snapshot.navigate(href, {
|
|
130
|
+
...opts,
|
|
131
|
+
search: resolveTargetSearch(pathname, merged, snapshot.searchRoutes),
|
|
132
|
+
});
|
|
128
133
|
},
|
|
129
134
|
[from, store]
|
|
130
135
|
);
|