@solidjs/router 1.0.0 → 2.0.0-next.13
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/README.md +606 -697
- package/dist/claims.d.ts +21 -0
- package/dist/claims.js +115 -0
- package/dist/data/action.d.ts +40 -11
- package/dist/data/action.js +307 -101
- package/dist/data/events.d.ts +8 -0
- package/dist/data/events.js +24 -23
- package/dist/data/index.d.ts +2 -4
- package/dist/data/index.js +2 -4
- package/dist/data/query.d.ts +1 -3
- package/dist/data/query.js +75 -32
- package/dist/data/serverForms.d.ts +1 -0
- package/dist/data/serverForms.js +5 -0
- package/dist/fs.d.ts +94 -0
- package/dist/fs.js +51 -0
- package/dist/index.d.ts +24 -3
- package/dist/index.js +1949 -1204
- package/dist/index.jsx +2 -2
- package/dist/lifecycle.d.ts +29 -4
- package/dist/lifecycle.js +40 -37
- package/dist/paths.d.ts +117 -0
- package/dist/paths.js +41 -0
- package/dist/routers/components.d.ts +12 -26
- package/dist/routers/components.jsx +68 -54
- package/dist/routers/factory.d.ts +121 -0
- package/dist/routers/factory.jsx +154 -0
- package/dist/routers/history.d.ts +26 -0
- package/dist/routers/history.js +180 -0
- package/dist/routers/index.d.ts +5 -11
- package/dist/routers/index.js +2 -6
- package/dist/routers/scrollRestoration.d.ts +10 -1
- package/dist/routers/scrollRestoration.js +49 -11
- package/dist/routing.d.ts +96 -52
- package/dist/routing.js +429 -172
- package/dist/server.d.ts +47 -0
- package/dist/server.js +156 -0
- package/dist/types.d.ts +158 -44
- package/dist/utils.d.ts +2 -0
- package/dist/utils.js +2 -0
- package/package.json +12 -7
- package/dist/components.d.ts +0 -31
- package/dist/components.jsx +0 -40
- package/dist/data/createAsync.d.ts +0 -32
- package/dist/data/createAsync.js +0 -96
- package/dist/data/response.d.ts +0 -4
- package/dist/data/response.js +0 -42
- package/dist/routers/HashRouter.d.ts +0 -9
- package/dist/routers/HashRouter.js +0 -41
- package/dist/routers/MemoryRouter.d.ts +0 -24
- package/dist/routers/MemoryRouter.js +0 -57
- package/dist/routers/Router.d.ts +0 -17
- package/dist/routers/Router.js +0 -59
- package/dist/routers/StaticRouter.d.ts +0 -6
- package/dist/routers/StaticRouter.js +0 -15
- package/dist/routers/createRouter.d.ts +0 -10
- package/dist/routers/createRouter.js +0 -41
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
/*@refresh skip*/
|
|
2
|
+
import { createSignal, getOwner, onCleanup, sharedConfig, untrack } from "solid-js";
|
|
3
|
+
import { getRequestEvent, isServer } from "@solidjs/web";
|
|
4
|
+
import { setupLinkClaims } from "../claims.js";
|
|
5
|
+
import { setupNativeEvents } from "../data/events.js";
|
|
6
|
+
import { createPathsProxy } from "../paths.js";
|
|
7
|
+
import { createBranches, createRouterContext, getRouteMatches, registerFlightRouter, RouterContextObj, trackLazySubtrees, useOptionalContext } from "../routing.js";
|
|
8
|
+
import { mockBase } from "../utils.js";
|
|
9
|
+
import { Root, Routes } from "./components.jsx";
|
|
10
|
+
import { browserHistory } from "./history.js";
|
|
11
|
+
import { createScrollRestoration, withScrollRestoration } from "./scrollRestoration.js";
|
|
12
|
+
/**
|
|
13
|
+
* Identity helper that preserves literal types when the route tree is
|
|
14
|
+
* declared as a separate variable. `createRouter` infers literally from
|
|
15
|
+
* inline arrays, but an extracted `const routes = [...]` widens paths to
|
|
16
|
+
* `string` and silently degrades `paths` and the typed hooks unless it is
|
|
17
|
+
* declared `as const` — `defineRoutes` makes that impossible to forget.
|
|
18
|
+
*/
|
|
19
|
+
export function defineRoutes(routes) {
|
|
20
|
+
return routes;
|
|
21
|
+
}
|
|
22
|
+
export function defineRoute(route) {
|
|
23
|
+
return route;
|
|
24
|
+
}
|
|
25
|
+
/** Wraps a history adapter in the integration signal the router core consumes. Must run under a reactive owner. */
|
|
26
|
+
function createIntegration(history) {
|
|
27
|
+
let ignore = false;
|
|
28
|
+
const wrap = (value) => (typeof value === "string" ? { value } : value);
|
|
29
|
+
const [read, write] = createSignal(wrap(history.get()), {
|
|
30
|
+
equals: (a, b) => a.value === b.value && a.state === b.state,
|
|
31
|
+
ownedWrite: true
|
|
32
|
+
});
|
|
33
|
+
const signal = [
|
|
34
|
+
read,
|
|
35
|
+
(next) => {
|
|
36
|
+
!ignore && history.set(next);
|
|
37
|
+
if (sharedConfig.registry && !sharedConfig.done)
|
|
38
|
+
sharedConfig.done = true;
|
|
39
|
+
write(next);
|
|
40
|
+
}
|
|
41
|
+
];
|
|
42
|
+
history.init &&
|
|
43
|
+
onCleanup(history.init((value = history.get()) => {
|
|
44
|
+
ignore = true;
|
|
45
|
+
signal[1](wrap(value));
|
|
46
|
+
ignore = false;
|
|
47
|
+
}));
|
|
48
|
+
return { signal, utils: history.utils };
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Server default: a static view of the request URL — no signal machinery, a
|
|
52
|
+
* server render never navigates. The request event (when the harness scopes
|
|
53
|
+
* one) wins; the provider's `url` prop is the fallback for renders outside a
|
|
54
|
+
* request scope (SSG scripts, server-side tests, runtimes without
|
|
55
|
+
* `node:async_hooks`). History adapters are a client navigation concern and
|
|
56
|
+
* play no part in locating a server render.
|
|
57
|
+
*/
|
|
58
|
+
function staticIntegration(url, utils) {
|
|
59
|
+
const e = getRequestEvent();
|
|
60
|
+
const source = e ? e.request.url : url;
|
|
61
|
+
let value = "";
|
|
62
|
+
if (source) {
|
|
63
|
+
const u = new URL(source, mockBase);
|
|
64
|
+
value = u.pathname + u.search;
|
|
65
|
+
}
|
|
66
|
+
const obj = { value };
|
|
67
|
+
return { signal: [() => obj, next => Object.assign(obj, next)], utils };
|
|
68
|
+
}
|
|
69
|
+
export function createRouter(config) {
|
|
70
|
+
const basePath = config.base || "";
|
|
71
|
+
// Routes are immutable per instance, so compilation is shared by every
|
|
72
|
+
// mount, request, and `match()` call — recompiled only when a lazy subtree
|
|
73
|
+
// resolves (append-only: resolution, not mutation). Reading the version
|
|
74
|
+
// inside a computation subscribes it; plain calls just see current state.
|
|
75
|
+
let compiled;
|
|
76
|
+
let compiledVersion = -1;
|
|
77
|
+
const branches = () => {
|
|
78
|
+
const version = trackLazySubtrees();
|
|
79
|
+
if (!compiled || compiledVersion !== version) {
|
|
80
|
+
compiled = createBranches(config.routes, basePath);
|
|
81
|
+
compiledVersion = version;
|
|
82
|
+
}
|
|
83
|
+
return compiled;
|
|
84
|
+
};
|
|
85
|
+
const renderPath = (config.history && config.history.utils && config.history.utils.renderPath) || undefined;
|
|
86
|
+
function RouterComponent(props) {
|
|
87
|
+
// One router per app: the session (location, history, delegation, link
|
|
88
|
+
// claims, preloading) has a single owner, and a second instance would
|
|
89
|
+
// fight it — stale content on click navigations, conflicting link
|
|
90
|
+
// attributes. Compose route trees instead; lazy subtrees are the planned
|
|
91
|
+
// answer for definitions unknown at build time.
|
|
92
|
+
if (useOptionalContext(RouterContextObj)) {
|
|
93
|
+
console.warn("Mounting a router inside another router is not supported. " +
|
|
94
|
+
"Compose route trees in one createRouter config instead.");
|
|
95
|
+
}
|
|
96
|
+
const root = untrack(() => props.children);
|
|
97
|
+
let restoration;
|
|
98
|
+
let history = config.history;
|
|
99
|
+
if (!isServer && (config.scrollRestoration ?? !history)) {
|
|
100
|
+
restoration = createScrollRestoration();
|
|
101
|
+
history = withScrollRestoration(history || browserHistory(), restoration);
|
|
102
|
+
}
|
|
103
|
+
const integration = isServer
|
|
104
|
+
? staticIntegration(props.url, config.history && config.history.utils)
|
|
105
|
+
: createIntegration(history || browserHistory());
|
|
106
|
+
let context;
|
|
107
|
+
const routerState = createRouterContext(integration, branches, () => context, {
|
|
108
|
+
base: basePath,
|
|
109
|
+
singleFlight: config.singleFlight,
|
|
110
|
+
transformUrl: config.transformUrl
|
|
111
|
+
});
|
|
112
|
+
if (!isServer) {
|
|
113
|
+
setupNativeEvents({
|
|
114
|
+
preload: config.preloadLinks,
|
|
115
|
+
explicitLinks: config.explicitLinks,
|
|
116
|
+
actionBase: config.actionBase,
|
|
117
|
+
transformUrl: config.transformUrl
|
|
118
|
+
})(routerState);
|
|
119
|
+
setupLinkClaims(routerState, config.explicitLinks);
|
|
120
|
+
if (routerState.singleFlight)
|
|
121
|
+
onCleanup(registerFlightRouter(routerState));
|
|
122
|
+
restoration && restoration.create(routerState);
|
|
123
|
+
}
|
|
124
|
+
return (<RouterContextObj value={routerState}>
|
|
125
|
+
<Root routerState={routerState} root={root} preload={config.preload}>
|
|
126
|
+
{(context = getOwner()) && null}
|
|
127
|
+
<Routes routerState={routerState} branches={branches}/>
|
|
128
|
+
</Root>
|
|
129
|
+
</RouterContextObj>);
|
|
130
|
+
}
|
|
131
|
+
const instance = Object.assign(RouterComponent, {
|
|
132
|
+
routes: config.routes,
|
|
133
|
+
config,
|
|
134
|
+
match(url) {
|
|
135
|
+
const u = new URL(url, mockBase);
|
|
136
|
+
const pathname = config.transformUrl ? config.transformUrl(u.pathname) : u.pathname;
|
|
137
|
+
return getRouteMatches(branches(), pathname).map(({ route, path, params }) => ({
|
|
138
|
+
path: route.originalPath,
|
|
139
|
+
pattern: route.pattern,
|
|
140
|
+
match: path,
|
|
141
|
+
params,
|
|
142
|
+
info: route.info
|
|
143
|
+
}));
|
|
144
|
+
}
|
|
145
|
+
});
|
|
146
|
+
// Built on first access (a getter via Object.assign would run during the
|
|
147
|
+
// copy) so runtimes without Proxy — some older TVs — can still route as
|
|
148
|
+
// long as they never touch typed paths.
|
|
149
|
+
let paths;
|
|
150
|
+
Object.defineProperty(instance, "paths", {
|
|
151
|
+
get: () => paths || (paths = createPathsProxy(renderPath, basePath))
|
|
152
|
+
});
|
|
153
|
+
return instance;
|
|
154
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { LocationChange, RouterUtils } from "../types.js";
|
|
2
|
+
export declare function bindEvent(target: EventTarget, type: string, handler: EventListener): () => void;
|
|
3
|
+
export declare function saveCurrentDepth(): void;
|
|
4
|
+
/**
|
|
5
|
+
* A history adapter: the source of truth for the current URL and how
|
|
6
|
+
* navigations write back to it. Adapters are plain imported values so
|
|
7
|
+
* unused ones never enter the bundle — `createRouter` defaults to browser
|
|
8
|
+
* history on the client and the request URL on the server.
|
|
9
|
+
*/
|
|
10
|
+
export interface RouterHistory {
|
|
11
|
+
get: () => string | LocationChange;
|
|
12
|
+
set: (next: LocationChange) => void;
|
|
13
|
+
init?: (notify: (value?: string | LocationChange) => void) => () => void;
|
|
14
|
+
utils?: Partial<RouterUtils>;
|
|
15
|
+
}
|
|
16
|
+
export declare function browserHistory(): RouterHistory;
|
|
17
|
+
export declare function hashParser(str: string): string;
|
|
18
|
+
export declare function hashHistory(): RouterHistory;
|
|
19
|
+
export interface MemoryHistoryAdapter extends RouterHistory {
|
|
20
|
+
get: () => string;
|
|
21
|
+
go: (delta: number) => void;
|
|
22
|
+
back: () => void;
|
|
23
|
+
forward: () => void;
|
|
24
|
+
listen: (listener: (value: string) => void) => () => void;
|
|
25
|
+
}
|
|
26
|
+
export declare function memoryHistory(initial?: string): MemoryHistoryAdapter;
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
import { isServer } from "@solidjs/web";
|
|
2
|
+
export function bindEvent(target, type, handler) {
|
|
3
|
+
target.addEventListener(type, handler);
|
|
4
|
+
return () => target.removeEventListener(type, handler);
|
|
5
|
+
}
|
|
6
|
+
// Depth stamping supports blocking browser-initiated navigation (back/forward)
|
|
7
|
+
// for `useBeforeLeave`. It stays always-on — a couple of history.state writes —
|
|
8
|
+
// so blocking stays exact no matter when the first guard subscribes, while the
|
|
9
|
+
// guard machinery itself lives behind the lazy `beforeLeave` slot.
|
|
10
|
+
let depth;
|
|
11
|
+
export function saveCurrentDepth() {
|
|
12
|
+
if (!window.history.state || window.history.state._depth == null) {
|
|
13
|
+
window.history.replaceState({ ...window.history.state, _depth: window.history.length - 1 }, "");
|
|
14
|
+
}
|
|
15
|
+
depth = window.history.state._depth;
|
|
16
|
+
}
|
|
17
|
+
function keepDepth(state) {
|
|
18
|
+
return {
|
|
19
|
+
...state,
|
|
20
|
+
_depth: window.history.state && window.history.state._depth
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
function notifyIfNotBlocked(notify, block) {
|
|
24
|
+
let ignore = false;
|
|
25
|
+
return () => {
|
|
26
|
+
const prevDepth = depth;
|
|
27
|
+
saveCurrentDepth();
|
|
28
|
+
const delta = prevDepth == null ? null : depth - prevDepth;
|
|
29
|
+
if (ignore) {
|
|
30
|
+
ignore = false;
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
if (delta && block(delta)) {
|
|
34
|
+
ignore = true;
|
|
35
|
+
window.history.go(-delta);
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
notify();
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
function scrollToHash(hash, fallbackTop) {
|
|
43
|
+
const el = hash && document.getElementById(hash);
|
|
44
|
+
if (el) {
|
|
45
|
+
el.scrollIntoView();
|
|
46
|
+
}
|
|
47
|
+
else if (fallbackTop) {
|
|
48
|
+
window.scrollTo(0, 0);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
export function browserHistory() {
|
|
52
|
+
const getSource = () => {
|
|
53
|
+
const url = window.location.pathname + window.location.search;
|
|
54
|
+
const state = window.history.state &&
|
|
55
|
+
window.history.state._depth &&
|
|
56
|
+
Object.keys(window.history.state).length === 1
|
|
57
|
+
? undefined
|
|
58
|
+
: window.history.state;
|
|
59
|
+
return {
|
|
60
|
+
value: url + window.location.hash,
|
|
61
|
+
state
|
|
62
|
+
};
|
|
63
|
+
};
|
|
64
|
+
const beforeLeave = {};
|
|
65
|
+
if (!isServer)
|
|
66
|
+
saveCurrentDepth();
|
|
67
|
+
return {
|
|
68
|
+
get: getSource,
|
|
69
|
+
set({ value, replace, scroll, state }) {
|
|
70
|
+
if (replace) {
|
|
71
|
+
window.history.replaceState(keepDepth(state), "", value);
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
window.history.pushState(state, "", value);
|
|
75
|
+
}
|
|
76
|
+
scrollToHash(decodeURIComponent(window.location.hash.slice(1)), scroll);
|
|
77
|
+
saveCurrentDepth();
|
|
78
|
+
},
|
|
79
|
+
init: notify => bindEvent(window, "popstate", notifyIfNotBlocked(notify, delta => {
|
|
80
|
+
const guard = beforeLeave.current;
|
|
81
|
+
if (!guard)
|
|
82
|
+
return false;
|
|
83
|
+
if (delta) {
|
|
84
|
+
return !guard.confirm(delta);
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
const s = getSource();
|
|
88
|
+
return !guard.confirm(s.value, { state: s.state });
|
|
89
|
+
}
|
|
90
|
+
})),
|
|
91
|
+
utils: {
|
|
92
|
+
go: delta => window.history.go(delta),
|
|
93
|
+
beforeLeave
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
export function hashParser(str) {
|
|
98
|
+
const to = str.replace(/^.*?#/, "");
|
|
99
|
+
// Hash-only hrefs like `#foo` from plain anchors will come in as `/#foo` whereas a link to
|
|
100
|
+
// `/foo` will be `/#/foo`. Check if the to starts with a `/` and if not append it as a hash
|
|
101
|
+
// to the current path so we can handle these in-page anchors correctly.
|
|
102
|
+
if (!to.startsWith("/")) {
|
|
103
|
+
const [, path = "/"] = window.location.hash.split("#", 2);
|
|
104
|
+
return `${path}#${to}`;
|
|
105
|
+
}
|
|
106
|
+
return to;
|
|
107
|
+
}
|
|
108
|
+
export function hashHistory() {
|
|
109
|
+
const getSource = () => window.location.hash.slice(1);
|
|
110
|
+
const beforeLeave = {};
|
|
111
|
+
if (!isServer)
|
|
112
|
+
saveCurrentDepth();
|
|
113
|
+
return {
|
|
114
|
+
get: getSource,
|
|
115
|
+
set({ value, replace, scroll, state }) {
|
|
116
|
+
if (replace) {
|
|
117
|
+
window.history.replaceState(keepDepth(state), "", "#" + value);
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
window.history.pushState(state, "", "#" + value);
|
|
121
|
+
}
|
|
122
|
+
const hashIndex = value.indexOf("#");
|
|
123
|
+
const hash = hashIndex >= 0 ? value.slice(hashIndex + 1) : "";
|
|
124
|
+
scrollToHash(hash, scroll);
|
|
125
|
+
saveCurrentDepth();
|
|
126
|
+
},
|
|
127
|
+
init: notify => bindEvent(window, "hashchange", notifyIfNotBlocked(notify, delta => {
|
|
128
|
+
const guard = beforeLeave.current;
|
|
129
|
+
return !!guard && !guard.confirm(delta && delta < 0 ? delta : getSource());
|
|
130
|
+
})),
|
|
131
|
+
utils: {
|
|
132
|
+
go: delta => window.history.go(delta),
|
|
133
|
+
renderPath: path => `#${path}`,
|
|
134
|
+
parsePath: hashParser,
|
|
135
|
+
beforeLeave
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
export function memoryHistory(initial = "/") {
|
|
140
|
+
const entries = [initial];
|
|
141
|
+
let index = 0;
|
|
142
|
+
const listeners = [];
|
|
143
|
+
const go = (n) => {
|
|
144
|
+
// https://github.com/remix-run/react-router/blob/682810ca929d0e3c64a76f8d6e465196b7a2ac58/packages/router/history.ts#L245
|
|
145
|
+
index = Math.max(0, Math.min(index + n, entries.length - 1));
|
|
146
|
+
const value = entries[index];
|
|
147
|
+
listeners.forEach(listener => listener(value));
|
|
148
|
+
};
|
|
149
|
+
const listen = (listener) => {
|
|
150
|
+
listeners.push(listener);
|
|
151
|
+
return () => {
|
|
152
|
+
const i = listeners.indexOf(listener);
|
|
153
|
+
listeners.splice(i, 1);
|
|
154
|
+
};
|
|
155
|
+
};
|
|
156
|
+
return {
|
|
157
|
+
get: () => entries[index],
|
|
158
|
+
set: ({ value, scroll, replace }) => {
|
|
159
|
+
if (replace) {
|
|
160
|
+
entries[index] = value;
|
|
161
|
+
}
|
|
162
|
+
else {
|
|
163
|
+
entries.splice(index + 1, entries.length - index, value);
|
|
164
|
+
index++;
|
|
165
|
+
}
|
|
166
|
+
listeners.forEach(listener => listener(value));
|
|
167
|
+
setTimeout(() => {
|
|
168
|
+
if (scroll) {
|
|
169
|
+
scrollToHash(value.split("#")[1] || "", true);
|
|
170
|
+
}
|
|
171
|
+
}, 0);
|
|
172
|
+
},
|
|
173
|
+
back: () => go(-1),
|
|
174
|
+
forward: () => go(1),
|
|
175
|
+
go,
|
|
176
|
+
listen,
|
|
177
|
+
init: listen,
|
|
178
|
+
utils: { go }
|
|
179
|
+
};
|
|
180
|
+
}
|
package/dist/routers/index.d.ts
CHANGED
|
@@ -1,11 +1,5 @@
|
|
|
1
|
-
export {
|
|
2
|
-
export type {
|
|
3
|
-
export {
|
|
4
|
-
export {
|
|
5
|
-
export type {
|
|
6
|
-
export { HashRouter } from "./HashRouter.js";
|
|
7
|
-
export type { HashRouterProps } from "./HashRouter.js";
|
|
8
|
-
export { MemoryRouter, createMemoryHistory } from "./MemoryRouter.js";
|
|
9
|
-
export type { MemoryRouterProps, MemoryHistory } from "./MemoryRouter.js";
|
|
10
|
-
export { StaticRouter } from "./StaticRouter.js";
|
|
11
|
-
export type { StaticRouterProps } from "./StaticRouter.js";
|
|
1
|
+
export { createRouter, defineRoute, defineRoutes } from "./factory.jsx";
|
|
2
|
+
export type { DefinedRoute } from "./factory.jsx";
|
|
3
|
+
export type { RouterConfig, RouterInstance, RouterProps } from "./factory.jsx";
|
|
4
|
+
export { browserHistory, hashHistory, memoryHistory } from "./history.js";
|
|
5
|
+
export type { RouterHistory, MemoryHistoryAdapter } from "./history.js";
|
package/dist/routers/index.js
CHANGED
|
@@ -1,6 +1,2 @@
|
|
|
1
|
-
export {
|
|
2
|
-
export {
|
|
3
|
-
export { Router } from "./Router.js";
|
|
4
|
-
export { HashRouter } from "./HashRouter.js";
|
|
5
|
-
export { MemoryRouter, createMemoryHistory } from "./MemoryRouter.js";
|
|
6
|
-
export { StaticRouter } from "./StaticRouter.js";
|
|
1
|
+
export { createRouter, defineRoute, defineRoutes } from "./factory.jsx";
|
|
2
|
+
export { browserHistory, hashHistory, memoryHistory } from "./history.js";
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { RouterContext } from "../types.js";
|
|
2
|
+
import type { RouterHistory } from "./history.js";
|
|
2
3
|
/**
|
|
3
4
|
* Explicit scroll restoration for back/forward navigation. The browser's
|
|
4
5
|
* native same-document heuristic is unreliable for suspense-driven rendering:
|
|
@@ -24,9 +25,17 @@ import type { RouterContext } from "../types.js";
|
|
|
24
25
|
* fold — keeps whatever offset the document can hold.
|
|
25
26
|
*/
|
|
26
27
|
export declare function createScrollRestoration(): {
|
|
27
|
-
/**
|
|
28
|
+
/** When the adapter notifies a traversal: mark the target for restoration. */
|
|
28
29
|
onPop(): void;
|
|
29
30
|
/** After a push: forward entries died, and this depth may be reused. */
|
|
30
31
|
onPush(): void;
|
|
31
32
|
create(router: RouterContext): void;
|
|
32
33
|
};
|
|
34
|
+
export type ScrollRestoration = ReturnType<typeof createScrollRestoration>;
|
|
35
|
+
/**
|
|
36
|
+
* Threads restoration through a history adapter: pushes prune dead forward
|
|
37
|
+
* entries, and adapter notifications (unblocked pops) mark the traversal
|
|
38
|
+
* target. Notification runs after the adapter's depth bookkeeping, so the
|
|
39
|
+
* marked depth is the entry being restored to.
|
|
40
|
+
*/
|
|
41
|
+
export declare function withScrollRestoration(history: RouterHistory, restoration: ScrollRestoration): RouterHistory;
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import { createEffect,
|
|
2
|
-
import { saveCurrentDepth } from "
|
|
3
|
-
import { bindEvent } from "./createRouter.js";
|
|
1
|
+
import { createEffect, onCleanup } from "solid-js";
|
|
2
|
+
import { bindEvent, saveCurrentDepth } from "./history.js";
|
|
4
3
|
const STORAGE_KEY = "solid-router:scroll";
|
|
5
4
|
/**
|
|
6
5
|
* Explicit scroll restoration for back/forward navigation. The browser's
|
|
@@ -29,7 +28,7 @@ const STORAGE_KEY = "solid-router:scroll";
|
|
|
29
28
|
export function createScrollRestoration() {
|
|
30
29
|
window.history.scrollRestoration = "manual";
|
|
31
30
|
// the current entry needs its depth stamp for captures to have a key, even
|
|
32
|
-
// if something replaced history.state after the
|
|
31
|
+
// if something replaced history.state after the adapter stamped it
|
|
33
32
|
saveCurrentDepth();
|
|
34
33
|
let positions = {};
|
|
35
34
|
try {
|
|
@@ -69,7 +68,7 @@ export function createScrollRestoration() {
|
|
|
69
68
|
programmatic = false;
|
|
70
69
|
};
|
|
71
70
|
return {
|
|
72
|
-
/**
|
|
71
|
+
/** When the adapter notifies a traversal: mark the target for restoration. */
|
|
73
72
|
onPop() {
|
|
74
73
|
pending = depth();
|
|
75
74
|
},
|
|
@@ -81,16 +80,55 @@ export function createScrollRestoration() {
|
|
|
81
80
|
+k >= d && delete positions[k];
|
|
82
81
|
},
|
|
83
82
|
create(router) {
|
|
84
|
-
|
|
83
|
+
// Restore once the traversal has settled: key on the location (a fully
|
|
84
|
+
// synchronous pop commits without isRouting ever flipping) and on
|
|
85
|
+
// isRouting, which reports in-flight transitions — native pops
|
|
86
|
+
// included — and holds the restore until they commit. restore() no-ops
|
|
87
|
+
// unless a traversal marked a target, so push navigations are inert.
|
|
88
|
+
// `transparent` keeps the effect invisible to the hydration id scheme —
|
|
89
|
+
// same reasoning as the link-claims effect (claims.ts): this setup is
|
|
90
|
+
// client-only, so an id-consuming node here has no server counterpart
|
|
91
|
+
// and every hydration id allocated after it shifts by one child slot.
|
|
92
|
+
// The visible failure is any <Loading> content that settled before the
|
|
93
|
+
// shell flush (a cache hit, a preloaded query): its serialized value and
|
|
94
|
+
// inlined markup are keyed under the server's ids, the shifted client
|
|
95
|
+
// misses both, recomputes, and re-renders the route fresh — duplicating
|
|
96
|
+
// the server DOM and leaving it inert.
|
|
97
|
+
createEffect(() => ({
|
|
98
|
+
url: router.location.pathname + router.location.search + router.location.hash,
|
|
99
|
+
routing: router.isRouting()
|
|
100
|
+
}), current => {
|
|
101
|
+
if (!current.routing)
|
|
102
|
+
restore();
|
|
103
|
+
}, { transparent: true });
|
|
85
104
|
onCleanup(() => unbind.forEach(u => u()));
|
|
86
|
-
// reload/back_forward document loads land on an existing entry
|
|
87
|
-
// navigation starts a new one and belongs at the top
|
|
105
|
+
// reload/back_forward document loads land on an existing entry (a fresh
|
|
106
|
+
// navigation starts a new one and belongs at the top); the effect's
|
|
107
|
+
// initial run performs the restore after first render
|
|
88
108
|
const [nav] = (performance.getEntriesByType &&
|
|
89
109
|
performance.getEntriesByType("navigation"));
|
|
90
|
-
if (nav && nav.type !== "navigate")
|
|
110
|
+
if (nav && nav.type !== "navigate")
|
|
91
111
|
pending = depth();
|
|
92
|
-
restore();
|
|
93
|
-
}
|
|
94
112
|
}
|
|
95
113
|
};
|
|
96
114
|
}
|
|
115
|
+
/**
|
|
116
|
+
* Threads restoration through a history adapter: pushes prune dead forward
|
|
117
|
+
* entries, and adapter notifications (unblocked pops) mark the traversal
|
|
118
|
+
* target. Notification runs after the adapter's depth bookkeeping, so the
|
|
119
|
+
* marked depth is the entry being restored to.
|
|
120
|
+
*/
|
|
121
|
+
export function withScrollRestoration(history, restoration) {
|
|
122
|
+
return {
|
|
123
|
+
...history,
|
|
124
|
+
set(next) {
|
|
125
|
+
history.set(next);
|
|
126
|
+
next.replace || restoration.onPush();
|
|
127
|
+
},
|
|
128
|
+
init: history.init &&
|
|
129
|
+
(notify => history.init(value => {
|
|
130
|
+
restoration.onPop();
|
|
131
|
+
notify(value);
|
|
132
|
+
}))
|
|
133
|
+
};
|
|
134
|
+
}
|