@solidjs/router 0.17.0-next.6 → 1.0.0-next.11
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 +526 -716
- package/dist/claims.d.ts +21 -0
- package/dist/claims.js +115 -0
- package/dist/data/action.d.ts +15 -0
- package/dist/data/action.js +165 -25
- package/dist/data/events.d.ts +8 -0
- package/dist/data/events.js +23 -22
- package/dist/data/flash.d.ts +1 -5
- package/dist/data/flash.js +10 -18
- package/dist/data/flashCookie.d.ts +7 -0
- package/dist/data/flashCookie.js +20 -0
- package/dist/data/serverForms.d.ts +1 -0
- package/dist/data/serverForms.js +5 -0
- package/dist/fs.d.ts +53 -0
- package/dist/fs.js +30 -0
- package/dist/index.d.ts +5 -3
- package/dist/index.js +1896 -1156
- 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 +10 -21
- package/dist/routers/components.jsx +29 -51
- package/dist/routers/factory.d.ts +66 -0
- package/dist/routers/factory.jsx +151 -0
- package/dist/routers/history.d.ts +26 -0
- package/dist/routers/history.js +180 -0
- package/dist/routers/index.d.ts +4 -11
- package/dist/routers/index.js +2 -6
- package/dist/routers/scrollRestoration.d.ts +41 -0
- package/dist/routers/scrollRestoration.js +134 -0
- package/dist/routing.d.ts +79 -52
- package/dist/routing.js +304 -128
- package/dist/server.d.ts +31 -17
- package/dist/server.js +68 -12
- package/dist/types.d.ts +74 -5
- package/dist/utils.d.ts +2 -0
- package/dist/utils.js +2 -0
- package/package.json +8 -7
- package/dist/components.d.ts +0 -31
- package/dist/components.jsx +0 -46
- 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 -9
- package/dist/routers/Router.js +0 -45
- 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 -40
package/dist/index.jsx
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export * from "./routers/index.js";
|
|
2
|
-
export * from "./components.jsx";
|
|
3
2
|
export * from "./lifecycle.js";
|
|
4
|
-
export { useHref, useIsRouting, useLocation, useMatch,
|
|
3
|
+
export { useHref, useIsRouting, useLinkState, useLocation, useMatch, useNavigate, usePreloadRoute, useParams, useResolvedPath, useRouteMatches, useSearchParams, RouterContextObj as RouterContext } from "./routing.js";
|
|
5
4
|
export { mergeSearchString as _mergeSearchString } from "./utils.js";
|
|
5
|
+
export { int } from "./paths.js";
|
|
6
6
|
export * from "./data/index.js";
|
package/dist/lifecycle.d.ts
CHANGED
|
@@ -1,5 +1,30 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { BeforeLeaveEventArgs, BeforeLeaveLifecycle } from "./types.js";
|
|
2
2
|
export declare function createBeforeLeave(): BeforeLeaveLifecycle;
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
/**
|
|
4
|
+
* useBeforeLeave takes a function that will be called prior to leaving a route.
|
|
5
|
+
* The function will be called with:
|
|
6
|
+
*
|
|
7
|
+
* - from (*Location*): current location (before change).
|
|
8
|
+
* - to (*string | number*): path passed to `navigate`.
|
|
9
|
+
* - options (*NavigateOptions*): options passed to navigate.
|
|
10
|
+
* - preventDefault (*function*): call to block the route change.
|
|
11
|
+
* - defaultPrevented (*readonly boolean*): `true` if any previously called leave handlers called `preventDefault`.
|
|
12
|
+
* - retry (*function*, force?: boolean ): call to retry the same navigation, perhaps after confirming with the user. Pass `true` to skip running the leave handlers again (i.e. force navigate without confirming).
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```js
|
|
16
|
+
* useBeforeLeave((e: BeforeLeaveEventArgs) => {
|
|
17
|
+
* if (form.isDirty && !e.defaultPrevented) {
|
|
18
|
+
* // preventDefault to block immediately and prompt user async
|
|
19
|
+
* e.preventDefault();
|
|
20
|
+
* setTimeout(() => {
|
|
21
|
+
* if (window.confirm("Discard unsaved changes - are you sure?")) {
|
|
22
|
+
* // user wants to proceed anyway so retry with force=true
|
|
23
|
+
* e.retry(true);
|
|
24
|
+
* }
|
|
25
|
+
* }, 100);
|
|
26
|
+
* }
|
|
27
|
+
* });
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
export declare const useBeforeLeave: (listener: (e: BeforeLeaveEventArgs) => void) => void;
|
package/dist/lifecycle.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { onCleanup } from "solid-js";
|
|
2
|
+
import { useLocation, useNavigate, useRouter } from "./routing.js";
|
|
2
3
|
export function createBeforeLeave() {
|
|
3
4
|
let listeners = new Set();
|
|
4
5
|
function subscribe(listener) {
|
|
@@ -38,39 +39,41 @@ export function createBeforeLeave() {
|
|
|
38
39
|
confirm
|
|
39
40
|
};
|
|
40
41
|
}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
}
|
|
42
|
+
/**
|
|
43
|
+
* useBeforeLeave takes a function that will be called prior to leaving a route.
|
|
44
|
+
* The function will be called with:
|
|
45
|
+
*
|
|
46
|
+
* - from (*Location*): current location (before change).
|
|
47
|
+
* - to (*string | number*): path passed to `navigate`.
|
|
48
|
+
* - options (*NavigateOptions*): options passed to navigate.
|
|
49
|
+
* - preventDefault (*function*): call to block the route change.
|
|
50
|
+
* - defaultPrevented (*readonly boolean*): `true` if any previously called leave handlers called `preventDefault`.
|
|
51
|
+
* - retry (*function*, force?: boolean ): call to retry the same navigation, perhaps after confirming with the user. Pass `true` to skip running the leave handlers again (i.e. force navigate without confirming).
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```js
|
|
55
|
+
* useBeforeLeave((e: BeforeLeaveEventArgs) => {
|
|
56
|
+
* if (form.isDirty && !e.defaultPrevented) {
|
|
57
|
+
* // preventDefault to block immediately and prompt user async
|
|
58
|
+
* e.preventDefault();
|
|
59
|
+
* setTimeout(() => {
|
|
60
|
+
* if (window.confirm("Discard unsaved changes - are you sure?")) {
|
|
61
|
+
* // user wants to proceed anyway so retry with force=true
|
|
62
|
+
* e.retry(true);
|
|
63
|
+
* }
|
|
64
|
+
* }, 100);
|
|
65
|
+
* }
|
|
66
|
+
* });
|
|
67
|
+
* ```
|
|
68
|
+
*/
|
|
69
|
+
export const useBeforeLeave = (listener) => {
|
|
70
|
+
// Installing the guard here (instead of eagerly in the router/history) keeps
|
|
71
|
+
// all of createBeforeLeave tree-shakeable for apps that never block leaves.
|
|
72
|
+
const slot = useRouter().beforeLeave;
|
|
73
|
+
const s = (slot.current || (slot.current = createBeforeLeave())).subscribe({
|
|
74
|
+
listener,
|
|
75
|
+
location: useLocation(),
|
|
76
|
+
navigate: useNavigate()
|
|
77
|
+
});
|
|
78
|
+
onCleanup(s);
|
|
79
|
+
};
|
package/dist/paths.d.ts
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import type { Params, RouteDefinition, SearchParams, SetSearchParams, StandardSchemaV1, TypedPath, TypedSearchPath } from "./types.js";
|
|
2
|
+
declare const FILTER_TYPE: unique symbol;
|
|
3
|
+
/**
|
|
4
|
+
* A match filter that also declares the TypeScript type accepted for the
|
|
5
|
+
* param at `paths` callsites (the runtime param remains a string).
|
|
6
|
+
*/
|
|
7
|
+
export interface TypedMatchFilter<T> {
|
|
8
|
+
(s: string): boolean;
|
|
9
|
+
readonly [FILTER_TYPE]: T;
|
|
10
|
+
}
|
|
11
|
+
/** Matches integer segments; types the param as `number` in the path proxy. */
|
|
12
|
+
export declare const int: TypedMatchFilter<number>;
|
|
13
|
+
type Flat<T> = {
|
|
14
|
+
[K in keyof T]: T[K];
|
|
15
|
+
} & {};
|
|
16
|
+
type SegmentsOf<P extends string> = P extends `${infer A}/${infer B}` ? [...SegmentsOf<A>, ...SegmentsOf<B>] : P extends "" ? [] : [P];
|
|
17
|
+
type FilterArg<F> = F extends TypedMatchFilter<infer T> ? T : F extends readonly (infer S)[] ? S : string | number;
|
|
18
|
+
type ParamArg<Name extends string, F> = Name extends keyof F ? FilterArg<F[Name]> : string | number;
|
|
19
|
+
/** Collects a maximal run of required params (and a trailing splat) into one call's argument tuple. */
|
|
20
|
+
type ParamRun<Segs extends readonly string[], F, Args extends readonly unknown[] = [], PAcc extends Params = {}> = Segs extends readonly [infer H extends string, ...infer R extends readonly string[]] ? H extends `:${string}?` ? {
|
|
21
|
+
args: Args;
|
|
22
|
+
params: PAcc;
|
|
23
|
+
rest: Segs;
|
|
24
|
+
} : H extends `:${infer N}` ? ParamRun<R, F, [...Args, ParamArg<N, F>], PAcc & {
|
|
25
|
+
[K in N]: string;
|
|
26
|
+
}> : H extends `*${infer N}` ? {
|
|
27
|
+
args: [...Args, string | number];
|
|
28
|
+
params: PAcc & {
|
|
29
|
+
[K in N]: string;
|
|
30
|
+
};
|
|
31
|
+
rest: R;
|
|
32
|
+
} : {
|
|
33
|
+
args: Args;
|
|
34
|
+
params: PAcc;
|
|
35
|
+
rest: Segs;
|
|
36
|
+
} : {
|
|
37
|
+
args: Args;
|
|
38
|
+
params: PAcc;
|
|
39
|
+
rest: Segs;
|
|
40
|
+
};
|
|
41
|
+
/** The search param types a route end carries: input builds URLs, output is what parsing returns. */
|
|
42
|
+
export interface SearchTypes {
|
|
43
|
+
input: any;
|
|
44
|
+
output: any;
|
|
45
|
+
}
|
|
46
|
+
export interface DefaultSearchTypes {
|
|
47
|
+
input: SetSearchParams;
|
|
48
|
+
output: SearchParams;
|
|
49
|
+
}
|
|
50
|
+
/** Terminating calls available on every route end: zero-arg, or search object plus optional hash. */
|
|
51
|
+
export interface PathEnd<Sch extends SearchTypes = DefaultSearchTypes, P extends Params = Params> extends TypedPath<P>, TypedSearchPath<Sch["input"], Sch["output"]> {
|
|
52
|
+
(): string;
|
|
53
|
+
(search: Sch["input"], hash?: string): string;
|
|
54
|
+
}
|
|
55
|
+
type SearchTypesOf<Def> = Def extends {
|
|
56
|
+
search: infer S;
|
|
57
|
+
} ? S extends StandardSchemaV1<any, any> ? {
|
|
58
|
+
input: NonNullable<S["~standard"]["types"]>["input"];
|
|
59
|
+
output: NonNullable<S["~standard"]["types"]>["output"];
|
|
60
|
+
} : DefaultSearchTypes : DefaultSearchTypes;
|
|
61
|
+
type FiltersOf<Def> = Def extends {
|
|
62
|
+
matchFilters: infer F;
|
|
63
|
+
} ? F : {};
|
|
64
|
+
type ChildrenOf<Def> = Def extends {
|
|
65
|
+
children: infer C;
|
|
66
|
+
} ? C : undefined;
|
|
67
|
+
/**
|
|
68
|
+
* Sees through a lazy `children` thunk: the routes the import's promise
|
|
69
|
+
* resolves to (its `default` or `routes` export, matching the runtime) type
|
|
70
|
+
* exactly like inline children. Only tables genuinely built at runtime —
|
|
71
|
+
* where the thunk's return type is a plain `RouteDefinition[]` — degrade to
|
|
72
|
+
* untyped, definitionally.
|
|
73
|
+
*/
|
|
74
|
+
type ResolvedChildren<C> = C extends () => infer R ? Awaited<R> extends infer M ? M extends {
|
|
75
|
+
default: infer D;
|
|
76
|
+
} ? D : M extends {
|
|
77
|
+
routes: infer D;
|
|
78
|
+
} ? D : M : never : C;
|
|
79
|
+
type ChildPaths<C, PAcc extends Params> = [C] extends [undefined] ? {} : ResolvedChildren<C> extends infer RC ? RC extends readonly unknown[] ? TuplePaths<RC, PAcc> : RouteContrib<RC, PAcc> : never;
|
|
80
|
+
type TuplePaths<R extends readonly unknown[], PAcc extends Params> = R extends readonly [
|
|
81
|
+
infer H,
|
|
82
|
+
...infer T extends readonly unknown[]
|
|
83
|
+
] ? RouteContrib<H, PAcc> & TuplePaths<T, PAcc> : {};
|
|
84
|
+
type PathLeaf<Sch extends SearchTypes, C, PAcc extends Params> = PathEnd<Sch, Flat<PAcc>> & ChildPaths<C, PAcc>;
|
|
85
|
+
type PathNode<Segs extends readonly string[], F, Sch extends SearchTypes, C, PAcc extends Params> = Segs extends readonly [infer H extends string, ...infer R extends readonly string[]] ? H extends `:${infer N}?` ? ((arg: ParamArg<N, F>) => PathNode<R, F, Sch, C, PAcc & {
|
|
86
|
+
[K in N]?: string;
|
|
87
|
+
}>) & PathNode<R, F, Sch, C, PAcc & {
|
|
88
|
+
[K in N]?: string;
|
|
89
|
+
}> : H extends `:${string}` | `*${string}` ? ParamCallNode<Segs, F, Sch, C, PAcc> : {
|
|
90
|
+
[K in H]: PathNode<R, F, Sch, C, PAcc>;
|
|
91
|
+
} : PathLeaf<Sch, C, PAcc>;
|
|
92
|
+
type ParamCallNode<Segs extends readonly string[], F, Sch extends SearchTypes, C, PAcc extends Params> = ParamRun<Segs, F> extends {
|
|
93
|
+
args: infer A extends readonly unknown[];
|
|
94
|
+
params: infer P2 extends Params;
|
|
95
|
+
rest: infer R2 extends readonly string[];
|
|
96
|
+
} ? ((...args: A) => PathNode<R2, F, Sch, C, PAcc & P2>) & (R2 extends readonly [] ? {
|
|
97
|
+
(...args: [...A, Sch["input"], string?]): string;
|
|
98
|
+
} : {}) & TypedPath<Flat<PAcc & P2>> : never;
|
|
99
|
+
type RouteContrib<Def, PAcc extends Params> = Def extends {
|
|
100
|
+
path: infer P;
|
|
101
|
+
} ? [P] extends [undefined] ? ChildPaths<ChildrenOf<Def>, PAcc> : P extends readonly string[] ? MultiPathContrib<P, Def, PAcc> : P extends string ? string extends P ? any : PathNode<SegmentsOf<P>, FiltersOf<Def>, SearchTypesOf<Def>, ChildrenOf<Def>, PAcc> : any : ChildPaths<ChildrenOf<Def>, PAcc>;
|
|
102
|
+
type MultiPathContrib<Ps extends readonly string[], Def, PAcc extends Params> = Ps extends readonly [infer H extends string, ...infer T extends readonly string[]] ? PathNode<SegmentsOf<H>, FiltersOf<Def>, SearchTypesOf<Def>, ChildrenOf<Def>, PAcc> & MultiPathContrib<T, Def, PAcc> : {};
|
|
103
|
+
/**
|
|
104
|
+
* The type of a router instance's `paths` proxy for a given route tree.
|
|
105
|
+
* Requires the tree to be a literal tuple (`as const` or a `const` type
|
|
106
|
+
* param); non-literal trees fall back to an untyped proxy.
|
|
107
|
+
*/
|
|
108
|
+
export type RoutePaths<R extends readonly RouteDefinition[]> = number extends R["length"] ? any : PathEnd<DefaultSearchTypes, {}> & TuplePaths<R, {}>;
|
|
109
|
+
/** Extracts the params record a paths node binds, as runtime (string-valued) params. */
|
|
110
|
+
export type PathParamsOf<N> = N extends TypedPath<infer P> ? Flat<P> : Params;
|
|
111
|
+
/**
|
|
112
|
+
* Creates the runtime path proxy. It is instance-scoped: `renderPath` comes
|
|
113
|
+
* from the router's history adapter (eg. hash routing prefixes `#`), and
|
|
114
|
+
* `base` is baked into every produced path.
|
|
115
|
+
*/
|
|
116
|
+
export declare function createPathsProxy(renderPath?: (path: string) => string, base?: string): any;
|
|
117
|
+
export {};
|
package/dist/paths.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { mergeSearchString, normalizePath } from "./utils.js";
|
|
2
|
+
/** Matches integer segments; types the param as `number` in the path proxy. */
|
|
3
|
+
export const int = ((s) => /^-?\d+$/.test(s));
|
|
4
|
+
// ---------------------------------------------------------------------------
|
|
5
|
+
// Runtime
|
|
6
|
+
// ---------------------------------------------------------------------------
|
|
7
|
+
const encodeParam = (value) => String(value).split("/").map(encodeURIComponent).join("/");
|
|
8
|
+
/**
|
|
9
|
+
* Creates the runtime path proxy. It is instance-scoped: `renderPath` comes
|
|
10
|
+
* from the router's history adapter (eg. hash routing prefixes `#`), and
|
|
11
|
+
* `base` is baked into every produced path.
|
|
12
|
+
*/
|
|
13
|
+
export function createPathsProxy(renderPath = p => p, base = "") {
|
|
14
|
+
const toHref = (pathname, suffix = "") => renderPath(pathname || "/") + suffix;
|
|
15
|
+
function node(pathname) {
|
|
16
|
+
const build = (...args) => {
|
|
17
|
+
let path = pathname;
|
|
18
|
+
for (let i = 0; i < args.length; i++) {
|
|
19
|
+
const arg = args[i];
|
|
20
|
+
if (typeof arg === "object" && arg !== null) {
|
|
21
|
+
// a search object terminates; an optional hash string may follow
|
|
22
|
+
const hash = typeof args[i + 1] === "string" ? `#${args[i + 1]}` : "";
|
|
23
|
+
return toHref(path, mergeSearchString("", arg) + hash);
|
|
24
|
+
}
|
|
25
|
+
path += `/${encodeParam(arg)}`;
|
|
26
|
+
}
|
|
27
|
+
// zero-arg calls terminate; param-only calls stay chainable
|
|
28
|
+
return args.length ? node(path) : toHref(path);
|
|
29
|
+
};
|
|
30
|
+
return new Proxy(build, {
|
|
31
|
+
get(_, prop) {
|
|
32
|
+
if (prop === "toString")
|
|
33
|
+
return () => toHref(pathname);
|
|
34
|
+
if (typeof prop === "symbol")
|
|
35
|
+
return prop === Symbol.toPrimitive ? () => toHref(pathname) : undefined;
|
|
36
|
+
return node(`${pathname}/${prop}`);
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
return node(normalizePath(base));
|
|
41
|
+
}
|
|
@@ -1,24 +1,13 @@
|
|
|
1
1
|
import type { Component } from "solid-js";
|
|
2
2
|
import type { JSX } from "@solidjs/web";
|
|
3
|
-
import type {
|
|
4
|
-
export
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* A component that wraps the content of every route.
|
|
8
|
-
*/
|
|
3
|
+
import type { Branch, RoutePreloadFunc, RouterContext, RouteSectionProps } from "../types.js";
|
|
4
|
+
export declare function Root(props: {
|
|
5
|
+
routerState: RouterContext;
|
|
9
6
|
root?: Component<RouteSectionProps>;
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
path?: S | S[];
|
|
18
|
-
children?: JSX.Element;
|
|
19
|
-
preload?: RoutePreloadFunc<T>;
|
|
20
|
-
matchFilters?: MatchFilters<S>;
|
|
21
|
-
component?: RouteSectionComponent<T>;
|
|
22
|
-
info?: Record<string, any>;
|
|
23
|
-
};
|
|
24
|
-
export declare const Route: <S extends string, T = unknown>(props: RouteProps<S, T>) => JSX.Element;
|
|
7
|
+
preload?: RoutePreloadFunc;
|
|
8
|
+
children: JSX.Element;
|
|
9
|
+
}): JSX.Element;
|
|
10
|
+
export declare function Routes(props: {
|
|
11
|
+
routerState: RouterContext;
|
|
12
|
+
branches: () => Branch[];
|
|
13
|
+
}): JSX.Element;
|
|
@@ -1,47 +1,19 @@
|
|
|
1
1
|
/*@refresh skip*/
|
|
2
|
-
import {
|
|
2
|
+
import { createMemo, createRoot, getOwner, onCleanup, runWithOwner, untrack } from "solid-js";
|
|
3
3
|
import { getRequestEvent, isServer } from "@solidjs/web";
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
export const createRouterComponent = (router) => function IntegratedRouter(props) {
|
|
7
|
-
const { base, singleFlight, transformUrl, root, rootPreload, routeChildren } = untrack(() => ({
|
|
8
|
-
base: props.base,
|
|
9
|
-
singleFlight: props.singleFlight,
|
|
10
|
-
transformUrl: props.transformUrl,
|
|
11
|
-
root: props.root,
|
|
12
|
-
rootPreload: props.rootPreload,
|
|
13
|
-
routeChildren: props.children
|
|
14
|
-
}));
|
|
15
|
-
const routeDefs = resolveRouteDefinitions(routeChildren);
|
|
16
|
-
const branches = createMemo(() => createBranches(routeDefs(), base || ""));
|
|
17
|
-
let context;
|
|
18
|
-
const routerState = createRouterContext(router, branches, () => context, {
|
|
19
|
-
base,
|
|
20
|
-
singleFlight,
|
|
21
|
-
transformUrl,
|
|
22
|
-
});
|
|
23
|
-
router.create && router.create(routerState);
|
|
24
|
-
if (!isServer && routerState.singleFlight) {
|
|
25
|
-
// Registering the consumer IS the single-flight opt-in: the transport
|
|
26
|
-
// sends the request header while one is subscribed. `singleFlight={false}`
|
|
27
|
-
// simply never subscribes, so the server is never asked to collect.
|
|
28
|
-
onCleanup(setupFlightDataConsumer(routerState));
|
|
29
|
-
}
|
|
30
|
-
return (<RouterContextObj value={routerState}>
|
|
31
|
-
<Root routerState={routerState} root={root} preload={rootPreload}>
|
|
32
|
-
{(context = getOwner()) && null}
|
|
33
|
-
<Routes routerState={routerState} branches={branches()}/>
|
|
34
|
-
</Root>
|
|
35
|
-
</RouterContextObj>);
|
|
36
|
-
};
|
|
37
|
-
function Root(props) {
|
|
4
|
+
import { createRouteContext, getIntent, getRouteMatches, resolveLazySubtree, RouteContextObj, setInPreloadFn, unresolvedLazyMatches } from "../routing.js";
|
|
5
|
+
export function Root(props) {
|
|
38
6
|
const location = props.routerState.location;
|
|
39
7
|
const params = props.routerState.params;
|
|
40
8
|
const data = createMemo(() => props.preload &&
|
|
41
9
|
untrack(() => {
|
|
42
10
|
setInPreloadFn(true);
|
|
43
|
-
|
|
44
|
-
|
|
11
|
+
try {
|
|
12
|
+
return props.preload({ params, location, intent: getIntent() || "initial" });
|
|
13
|
+
}
|
|
14
|
+
finally {
|
|
15
|
+
setInPreloadFn(false);
|
|
16
|
+
}
|
|
45
17
|
}));
|
|
46
18
|
const RootComp = props.root;
|
|
47
19
|
if (RootComp) {
|
|
@@ -51,22 +23,29 @@ function Root(props) {
|
|
|
51
23
|
}
|
|
52
24
|
return props.children;
|
|
53
25
|
}
|
|
54
|
-
function Routes(props) {
|
|
26
|
+
export function Routes(props) {
|
|
55
27
|
if (isServer) {
|
|
56
28
|
const e = getRequestEvent();
|
|
57
29
|
if (e && e.router && e.router.dataOnly) {
|
|
58
|
-
dataOnly(e, props.routerState, props.branches);
|
|
30
|
+
dataOnly(e, props.routerState, props.branches());
|
|
59
31
|
return;
|
|
60
32
|
}
|
|
61
|
-
e &&
|
|
62
|
-
|
|
63
|
-
|
|
33
|
+
if (e && !(e.router && e.router.matches)) {
|
|
34
|
+
// A lazy getter rather than a snapshot: unresolved lazy subtrees mean
|
|
35
|
+
// the match chain can still improve while the render streams, and
|
|
36
|
+
// frameworks read this after the render settles.
|
|
37
|
+
Object.defineProperty((e.router || (e.router = {})), "matches", {
|
|
38
|
+
configurable: true,
|
|
39
|
+
enumerable: true,
|
|
40
|
+
get: () => props.routerState.matches().map(({ route, path, params }) => ({
|
|
64
41
|
path: route.originalPath,
|
|
65
42
|
pattern: route.pattern,
|
|
66
43
|
match: path,
|
|
67
44
|
params,
|
|
68
45
|
info: route.info
|
|
69
|
-
}))
|
|
46
|
+
}))
|
|
47
|
+
});
|
|
48
|
+
}
|
|
70
49
|
}
|
|
71
50
|
const disposers = [];
|
|
72
51
|
let root;
|
|
@@ -79,6 +58,9 @@ function Routes(props) {
|
|
|
79
58
|
// computation (which disposes its children every time it re-runs).
|
|
80
59
|
const owner = getOwner();
|
|
81
60
|
const routeStates = createMemo((prev) => {
|
|
61
|
+
// While a lazy subtree resolves, `matches()` is not ready and this
|
|
62
|
+
// computation parks with it — no route contexts are created against
|
|
63
|
+
// placeholder matches.
|
|
82
64
|
const nextMatches = props.routerState.matches();
|
|
83
65
|
const previousMatches = prevMatches;
|
|
84
66
|
let equal = previousMatches && nextMatches.length === previousMatches.length;
|
|
@@ -130,19 +112,15 @@ const createOutlet = (child) => {
|
|
|
130
112
|
return undefined;
|
|
131
113
|
};
|
|
132
114
|
};
|
|
133
|
-
export const Route = (props) => {
|
|
134
|
-
const childRoutes = children(() => props.children);
|
|
135
|
-
return merge(props, {
|
|
136
|
-
get children() {
|
|
137
|
-
return childRoutes();
|
|
138
|
-
}
|
|
139
|
-
});
|
|
140
|
-
};
|
|
141
115
|
// for data only mode with single flight mutations
|
|
142
116
|
function dataOnly(event, routerState, branches) {
|
|
143
117
|
const url = new URL(event.request.url);
|
|
144
118
|
const prevMatches = getRouteMatches(branches, new URL(event.router.previousUrl || event.request.url).pathname);
|
|
145
119
|
const matches = getRouteMatches(branches, url.pathname);
|
|
120
|
+
// This pass is synchronous — an unresolved lazy subtree can only be
|
|
121
|
+
// kicked, not awaited (the flight collector's own runner awaits; see
|
|
122
|
+
// src/server.ts). Best effort so a subsequent pass sees the table.
|
|
123
|
+
unresolvedLazyMatches([...prevMatches, ...matches]).forEach(resolveLazySubtree);
|
|
146
124
|
for (let match = 0; match < matches.length; match++) {
|
|
147
125
|
if (!prevMatches[match] || matches[match].route !== prevMatches[match].route)
|
|
148
126
|
event.router.dataOnly = true;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type { JSX } from "@solidjs/web";
|
|
2
|
+
import type { RoutePaths } from "../paths.js";
|
|
3
|
+
import type { OutputMatch, RouteDefinition, RoutePreloadFunc, RouteSectionProps } from "../types.js";
|
|
4
|
+
import type { RouterHistory } from "./history.js";
|
|
5
|
+
/**
|
|
6
|
+
* Identity helper that preserves literal types when the route tree is
|
|
7
|
+
* declared as a separate variable. `createRouter` infers literally from
|
|
8
|
+
* inline arrays, but an extracted `const routes = [...]` widens paths to
|
|
9
|
+
* `string` and silently degrades `paths` and the typed hooks unless it is
|
|
10
|
+
* declared `as const` — `defineRoutes` makes that impossible to forget.
|
|
11
|
+
*/
|
|
12
|
+
export declare function defineRoutes<const R extends readonly RouteDefinition[]>(routes: R): R;
|
|
13
|
+
export interface RouterConfig<R extends readonly RouteDefinition[] = RouteDefinition[]> {
|
|
14
|
+
/** The route tree. Immutable per instance — it is the source of truth for matching *and* types. */
|
|
15
|
+
routes: R;
|
|
16
|
+
base?: string;
|
|
17
|
+
/**
|
|
18
|
+
* Runs once per mount/request (and on single-flight collection passes) to
|
|
19
|
+
* warm app-wide data. Its result is passed to the render-prop child as
|
|
20
|
+
* `props.data`.
|
|
21
|
+
*/
|
|
22
|
+
preload?: RoutePreloadFunc;
|
|
23
|
+
/**
|
|
24
|
+
* History adapter for client navigation; defaults to browser history. On
|
|
25
|
+
* the server only its `utils` apply — the location comes from the request
|
|
26
|
+
* event or the provider's `url` prop.
|
|
27
|
+
*/
|
|
28
|
+
history?: RouterHistory;
|
|
29
|
+
singleFlight?: boolean;
|
|
30
|
+
actionBase?: string;
|
|
31
|
+
explicitLinks?: boolean;
|
|
32
|
+
/** Preload route code/data on link hover and focus. Defaults to `true`. */
|
|
33
|
+
preloadLinks?: boolean;
|
|
34
|
+
/**
|
|
35
|
+
* Explicit scroll restoration for back/forward navigation: positions are
|
|
36
|
+
* saved per history entry and restored once the navigation settles,
|
|
37
|
+
* replacing the browser heuristic that loses offsets when the destination
|
|
38
|
+
* route forces a layout while rendering. Defaults to `true` with the
|
|
39
|
+
* default browser history; a custom `history` adapter owns its session and
|
|
40
|
+
* must opt in explicitly.
|
|
41
|
+
*/
|
|
42
|
+
scrollRestoration?: boolean;
|
|
43
|
+
transformUrl?: (url: string) => string;
|
|
44
|
+
}
|
|
45
|
+
export interface RouterProps {
|
|
46
|
+
/**
|
|
47
|
+
* Server-only: the location for this render when no request event is in
|
|
48
|
+
* scope (SSG scripts, tests, runtimes without `node:async_hooks`). A
|
|
49
|
+
* request event established by the server harness takes precedence.
|
|
50
|
+
* Ignored on the client, where the history adapter owns the location.
|
|
51
|
+
*/
|
|
52
|
+
url?: string;
|
|
53
|
+
children?: (props: RouteSectionProps) => JSX.Element;
|
|
54
|
+
}
|
|
55
|
+
export interface RouterInstance<R extends readonly RouteDefinition[] = RouteDefinition[]> {
|
|
56
|
+
/** The instance is the provider component; the render-prop child receives the matched content as `props.children`. */
|
|
57
|
+
(props: RouterProps): JSX.Element;
|
|
58
|
+
/** Typed path proxy — builds URLs through property access and calls. */
|
|
59
|
+
readonly paths: RoutePaths<R>;
|
|
60
|
+
readonly routes: R;
|
|
61
|
+
/** The config the instance was created with — lets server integrations (flight collector, no-JS handler) consume the instance directly. */
|
|
62
|
+
readonly config: RouterConfig<R>;
|
|
63
|
+
/** Pure matching against an arbitrary URL — no rendering or request context involved. Root→leaf; `[]` when nothing matches. */
|
|
64
|
+
match(url: string): OutputMatch[];
|
|
65
|
+
}
|
|
66
|
+
export declare function createRouter<const R extends readonly RouteDefinition[]>(config: RouterConfig<R>): RouterInstance<R>;
|