@solidjs/router 0.16.0 → 0.17.0-next.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (78) hide show
  1. package/README.md +85 -79
  2. package/dist/components.jsx +22 -16
  3. package/dist/data/action.d.ts +12 -10
  4. package/dist/data/action.js +98 -78
  5. package/dist/data/events.d.ts +8 -1
  6. package/dist/data/events.js +3 -3
  7. package/dist/data/index.d.ts +2 -3
  8. package/dist/data/index.js +2 -3
  9. package/dist/data/query.d.ts +1 -3
  10. package/dist/data/query.js +10 -16
  11. package/dist/index.d.ts +2 -2
  12. package/dist/index.js +212 -261
  13. package/dist/index.jsx +1 -1
  14. package/dist/lifecycle.js +1 -1
  15. package/dist/routers/HashRouter.js +1 -1
  16. package/dist/routers/MemoryRouter.js +1 -1
  17. package/dist/routers/Router.js +2 -2
  18. package/dist/routers/StaticRouter.js +1 -1
  19. package/dist/routers/components.d.ts +0 -4
  20. package/dist/routers/components.jsx +30 -19
  21. package/dist/routing.d.ts +4 -3
  22. package/dist/routing.js +71 -49
  23. package/dist/types.d.ts +3 -18
  24. package/dist/utils.d.ts +1 -0
  25. package/dist/utils.js +8 -0
  26. package/package.json +8 -6
  27. package/dist/data/createAsync.d.ts +0 -32
  28. package/dist/data/createAsync.js +0 -93
  29. package/dist/src/components.d.ts +0 -31
  30. package/dist/src/components.jsx +0 -39
  31. package/dist/src/data/action.d.ts +0 -17
  32. package/dist/src/data/action.js +0 -163
  33. package/dist/src/data/action.spec.d.ts +0 -1
  34. package/dist/src/data/action.spec.js +0 -297
  35. package/dist/src/data/createAsync.d.ts +0 -32
  36. package/dist/src/data/createAsync.js +0 -96
  37. package/dist/src/data/createAsync.spec.d.ts +0 -1
  38. package/dist/src/data/createAsync.spec.js +0 -196
  39. package/dist/src/data/events.d.ts +0 -9
  40. package/dist/src/data/events.js +0 -123
  41. package/dist/src/data/events.spec.d.ts +0 -1
  42. package/dist/src/data/events.spec.js +0 -567
  43. package/dist/src/data/index.d.ts +0 -4
  44. package/dist/src/data/index.js +0 -4
  45. package/dist/src/data/query.d.ts +0 -23
  46. package/dist/src/data/query.js +0 -232
  47. package/dist/src/data/query.spec.d.ts +0 -1
  48. package/dist/src/data/query.spec.js +0 -354
  49. package/dist/src/data/response.d.ts +0 -4
  50. package/dist/src/data/response.js +0 -42
  51. package/dist/src/data/response.spec.d.ts +0 -1
  52. package/dist/src/data/response.spec.js +0 -165
  53. package/dist/src/index.d.ts +0 -7
  54. package/dist/src/index.jsx +0 -6
  55. package/dist/src/lifecycle.d.ts +0 -5
  56. package/dist/src/lifecycle.js +0 -69
  57. package/dist/src/routers/HashRouter.d.ts +0 -9
  58. package/dist/src/routers/HashRouter.js +0 -41
  59. package/dist/src/routers/MemoryRouter.d.ts +0 -24
  60. package/dist/src/routers/MemoryRouter.js +0 -57
  61. package/dist/src/routers/Router.d.ts +0 -9
  62. package/dist/src/routers/Router.js +0 -45
  63. package/dist/src/routers/StaticRouter.d.ts +0 -6
  64. package/dist/src/routers/StaticRouter.js +0 -15
  65. package/dist/src/routers/components.d.ts +0 -27
  66. package/dist/src/routers/components.jsx +0 -118
  67. package/dist/src/routers/createRouter.d.ts +0 -10
  68. package/dist/src/routers/createRouter.js +0 -41
  69. package/dist/src/routers/index.d.ts +0 -11
  70. package/dist/src/routers/index.js +0 -6
  71. package/dist/src/routing.d.ts +0 -175
  72. package/dist/src/routing.js +0 -560
  73. package/dist/src/types.d.ts +0 -200
  74. package/dist/src/types.js +0 -1
  75. package/dist/src/utils.d.ts +0 -13
  76. package/dist/src/utils.js +0 -185
  77. package/dist/test/helpers.d.ts +0 -6
  78. package/dist/test/helpers.js +0 -50
@@ -1,165 +0,0 @@
1
- import { redirect, reload, json } from "./response.js";
2
- describe("redirect", () => {
3
- test("should create redirect response with default `302` status", () => {
4
- const response = redirect("/new-path");
5
- expect(response.status).toBe(302);
6
- expect(response.headers.get("Location")).toBe("/new-path");
7
- });
8
- test("should create redirect response with custom status", () => {
9
- const response = redirect("/permanent-redirect", 301);
10
- expect(response.status).toBe(301);
11
- expect(response.headers.get("Location")).toBe("/permanent-redirect");
12
- });
13
- test("should create redirect response with `RouterResponseInit` object", () => {
14
- const response = redirect("/custom-redirect", {
15
- status: 307,
16
- headers: { "X-Custom": "header" }
17
- });
18
- expect(response.status).toBe(307);
19
- expect(response.headers.get("Location")).toBe("/custom-redirect");
20
- expect(response.headers.get("X-Custom")).toBe("header");
21
- });
22
- test("should include `revalidate` header when specified", () => {
23
- const response = redirect("/revalidate-redirect", {
24
- revalidate: ["key1", "key2"]
25
- });
26
- expect(response.headers.get("X-Revalidate")).toBe("key1,key2");
27
- });
28
- test("should include `revalidate` header with `string` value", () => {
29
- const response = redirect("/single-revalidate", {
30
- revalidate: "single-key"
31
- });
32
- expect(response.headers.get("X-Revalidate")).toBe("single-key");
33
- });
34
- test("should preserve custom headers while adding Location", () => {
35
- const response = redirect("/with-headers", {
36
- headers: {
37
- "Content-Type": "application/json",
38
- "X-Custom": "value"
39
- }
40
- });
41
- expect(response.headers.get("Location")).toBe("/with-headers");
42
- expect(response.headers.get("Content-Type")).toBe("application/json");
43
- expect(response.headers.get("X-Custom")).toBe("value");
44
- });
45
- test("should handle absolute URLs", () => {
46
- const response = redirect("https://external.com/path");
47
- expect(response.headers.get("Location")).toBe("https://external.com/path");
48
- });
49
- });
50
- describe("reload", () => {
51
- test("should create reload response with default empty body", () => {
52
- const response = reload();
53
- expect(response.status).toBe(200);
54
- expect(response.body).toBeNull();
55
- });
56
- test("should create reload response with custom status", () => {
57
- const response = reload({ status: 204 });
58
- expect(response.status).toBe(204);
59
- });
60
- test("should include revalidate header when specified", () => {
61
- const response = reload({
62
- revalidate: ["cache-key"]
63
- });
64
- expect(response.headers.get("X-Revalidate")).toBe("cache-key");
65
- });
66
- test("should include revalidate header with array of keys", () => {
67
- const response = reload({
68
- revalidate: ["key1", "key2", "key3"]
69
- });
70
- expect(response.headers.get("X-Revalidate")).toBe("key1,key2,key3");
71
- });
72
- test("should preserve custom headers", () => {
73
- const response = reload({
74
- headers: {
75
- "X-Custom-Header": "custom-value",
76
- "Cache-Control": "no-cache"
77
- }
78
- });
79
- expect(response.headers.get("X-Custom-Header")).toBe("custom-value");
80
- expect(response.headers.get("Cache-Control")).toBe("no-cache");
81
- });
82
- test("should combine custom headers with revalidate", () => {
83
- const response = reload({
84
- revalidate: "reload-key",
85
- headers: {
86
- "X-Source": "reload-action"
87
- }
88
- });
89
- expect(response.headers.get("X-Revalidate")).toBe("reload-key");
90
- expect(response.headers.get("X-Source")).toBe("reload-action");
91
- });
92
- });
93
- describe("json", () => {
94
- test("should create `JSON` response with data", () => {
95
- const data = { message: "Hello", count: 42 };
96
- const response = json(data);
97
- expect(response.status).toBe(200);
98
- expect(response.headers.get("Content-Type")).toBe("application/json");
99
- expect(typeof response.customBody).toBe("function");
100
- expect(response.customBody()).toEqual(data);
101
- });
102
- test("should serialize data to `JSON` in response body", async () => {
103
- const data = { test: true, items: [1, 2, 3] };
104
- const response = json(data);
105
- const body = await response.text();
106
- expect(body).toBe(JSON.stringify(data));
107
- });
108
- test("should create `JSON` response with custom status", () => {
109
- const response = json({ error: "Not found" }, { status: 404 });
110
- expect(response.status).toBe(404);
111
- expect(response.headers.get("Content-Type")).toBe("application/json");
112
- });
113
- test("should include revalidate header when specified", () => {
114
- const response = json({ updated: true }, { revalidate: ["data-key"] });
115
- expect(response.headers.get("X-Revalidate")).toBe("data-key");
116
- });
117
- test("should preserve custom headers while adding Content-Type", () => {
118
- const response = json({ data: "test" }, {
119
- headers: {
120
- "X-API-Version": "v1",
121
- "Cache-Control": "max-age=3600"
122
- }
123
- });
124
- expect(response.headers.get("Content-Type")).toBe("application/json");
125
- expect(response.headers.get("X-API-Version")).toBe("v1");
126
- expect(response.headers.get("Cache-Control")).toBe("max-age=3600");
127
- });
128
- test("should handle `null` data", () => {
129
- const response = json(null);
130
- expect(response.customBody()).toBeNull();
131
- });
132
- test("should handle undefined data", () => {
133
- const response = json(undefined);
134
- expect(response.customBody()).toBeUndefined();
135
- });
136
- test("should handle complex nested data", () => {
137
- const complexData = {
138
- user: { id: 1, name: "John" },
139
- preferences: { theme: "dark", lang: "en" },
140
- items: [
141
- { id: 1, title: "Item 1" },
142
- { id: 2, title: "Item 2" }
143
- ]
144
- };
145
- const response = json(complexData);
146
- expect(response.customBody()).toEqual(complexData);
147
- });
148
- test("should combine all options", () => {
149
- const data = { message: "Success" };
150
- const response = json(data, {
151
- status: 201,
152
- revalidate: ["user-data", "cache-key"],
153
- headers: {
154
- "X-Created": "true",
155
- Location: "/new-resource"
156
- }
157
- });
158
- expect(response.status).toBe(201);
159
- expect(response.headers.get("Content-Type")).toBe("application/json");
160
- expect(response.headers.get("X-Revalidate")).toBe("user-data,cache-key");
161
- expect(response.headers.get("X-Created")).toBe("true");
162
- expect(response.headers.get("Location")).toBe("/new-resource");
163
- expect(response.customBody()).toEqual(data);
164
- });
165
- });
@@ -1,7 +0,0 @@
1
- export * from "./routers/index.js";
2
- export * from "./components.jsx";
3
- export * from "./lifecycle.js";
4
- export { useHref, useIsRouting, useLocation, useMatch, useCurrentMatches, useNavigate, useParams, useResolvedPath, useSearchParams, useBeforeLeave, usePreloadRoute, RouterContextObj as RouterContext } from "./routing.js";
5
- export { mergeSearchString as _mergeSearchString } from "./utils.js";
6
- export * from "./data/index.js";
7
- export type { Location, LocationChange, SearchParams, MatchFilter, MatchFilters, NavigateOptions, Navigator, OutputMatch, Params, PathMatch, RouteSectionProps, RoutePreloadFunc, RoutePreloadFuncArgs, RouteDefinition, RouteDescription, RouteMatch, RouterIntegration, RouterUtils, SetParams, Submission, BeforeLeaveEventArgs, RouteLoadFunc, RouteLoadFuncArgs, RouterResponseInit, CustomResponse } from "./types.js";
@@ -1,6 +0,0 @@
1
- export * from "./routers/index.js";
2
- export * from "./components.jsx";
3
- export * from "./lifecycle.js";
4
- export { useHref, useIsRouting, useLocation, useMatch, useCurrentMatches, useNavigate, useParams, useResolvedPath, useSearchParams, useBeforeLeave, usePreloadRoute, RouterContextObj as RouterContext } from "./routing.js";
5
- export { mergeSearchString as _mergeSearchString } from "./utils.js";
6
- export * from "./data/index.js";
@@ -1,5 +0,0 @@
1
- import { BeforeLeaveLifecycle, LocationChange } from "./types.js";
2
- export declare function createBeforeLeave(): BeforeLeaveLifecycle;
3
- export declare function saveCurrentDepth(): void;
4
- export declare function keepDepth(state: any): any;
5
- export declare function notifyIfNotBlocked(notify: (value?: string | LocationChange) => void, block: (delta: number | null) => boolean): () => void;
@@ -1,69 +0,0 @@
1
- import { isServer } from "solid-js/web";
2
- export function createBeforeLeave() {
3
- let listeners = new Set();
4
- function subscribe(listener) {
5
- listeners.add(listener);
6
- return () => listeners.delete(listener);
7
- }
8
- let ignore = false;
9
- function confirm(to, options) {
10
- if (ignore)
11
- return !(ignore = false);
12
- const e = {
13
- to,
14
- options,
15
- defaultPrevented: false,
16
- preventDefault: () => (e.defaultPrevented = true)
17
- };
18
- for (const l of listeners)
19
- l.listener({
20
- ...e,
21
- from: l.location,
22
- retry: (force) => {
23
- force && (ignore = true);
24
- l.navigate(to, { ...options, resolve: false });
25
- }
26
- });
27
- return !e.defaultPrevented;
28
- }
29
- return {
30
- subscribe,
31
- confirm
32
- };
33
- }
34
- // The following supports browser initiated blocking (eg back/forward)
35
- let depth;
36
- export function saveCurrentDepth() {
37
- if (!window.history.state || window.history.state._depth == null) {
38
- window.history.replaceState({ ...window.history.state, _depth: window.history.length - 1 }, "");
39
- }
40
- depth = window.history.state._depth;
41
- }
42
- if (!isServer) {
43
- saveCurrentDepth();
44
- }
45
- export function keepDepth(state) {
46
- return {
47
- ...state,
48
- _depth: window.history.state && window.history.state._depth
49
- };
50
- }
51
- export function notifyIfNotBlocked(notify, block) {
52
- let ignore = false;
53
- return () => {
54
- const prevDepth = depth;
55
- saveCurrentDepth();
56
- const delta = prevDepth == null ? null : depth - prevDepth;
57
- if (ignore) {
58
- ignore = false;
59
- return;
60
- }
61
- if (delta && block(delta)) {
62
- ignore = true;
63
- window.history.go(-delta);
64
- }
65
- else {
66
- notify();
67
- }
68
- };
69
- }
@@ -1,9 +0,0 @@
1
- import type { JSX } from "solid-js";
2
- import type { BaseRouterProps } from "./components.jsx";
3
- export declare function hashParser(str: string): string;
4
- export type HashRouterProps = BaseRouterProps & {
5
- actionBase?: string;
6
- explicitLinks?: boolean;
7
- preload?: boolean;
8
- };
9
- export declare function HashRouter(props: HashRouterProps): JSX.Element;
@@ -1,41 +0,0 @@
1
- import { setupNativeEvents } from "../data/events.js";
2
- import { createRouter, scrollToHash, bindEvent } from "./createRouter.js";
3
- import { createBeforeLeave, keepDepth, notifyIfNotBlocked, saveCurrentDepth } from "../lifecycle.js";
4
- export function hashParser(str) {
5
- const to = str.replace(/^.*?#/, "");
6
- // Hash-only hrefs like `#foo` from plain anchors will come in as `/#foo` whereas a link to
7
- // `/foo` will be `/#/foo`. Check if the to starts with a `/` and if not append it as a hash
8
- // to the current path so we can handle these in-page anchors correctly.
9
- if (!to.startsWith("/")) {
10
- const [, path = "/"] = window.location.hash.split("#", 2);
11
- return `${path}#${to}`;
12
- }
13
- return to;
14
- }
15
- export function HashRouter(props) {
16
- const getSource = () => window.location.hash.slice(1);
17
- const beforeLeave = createBeforeLeave();
18
- return createRouter({
19
- get: getSource,
20
- set({ value, replace, scroll, state }) {
21
- if (replace) {
22
- window.history.replaceState(keepDepth(state), "", "#" + value);
23
- }
24
- else {
25
- window.history.pushState(state, "", "#" + value);
26
- }
27
- const hashIndex = value.indexOf("#");
28
- const hash = hashIndex >= 0 ? value.slice(hashIndex + 1) : "";
29
- scrollToHash(hash, scroll);
30
- saveCurrentDepth();
31
- },
32
- init: notify => bindEvent(window, "hashchange", notifyIfNotBlocked(notify, delta => !beforeLeave.confirm(delta && delta < 0 ? delta : getSource()))),
33
- create: setupNativeEvents({ preload: props.preload, explicitLinks: props.explicitLinks, actionBase: props.actionBase }),
34
- utils: {
35
- go: delta => window.history.go(delta),
36
- renderPath: path => `#${path}`,
37
- parsePath: hashParser,
38
- beforeLeave
39
- }
40
- })(props);
41
- }
@@ -1,24 +0,0 @@
1
- import type { LocationChange } from "../types.js";
2
- import type { BaseRouterProps } from "./components.jsx";
3
- import type { JSX } from "solid-js";
4
- export type MemoryHistory = {
5
- get: () => string;
6
- set: (change: LocationChange) => void;
7
- go: (delta: number) => void;
8
- listen: (listener: (value: string) => void) => () => void;
9
- };
10
- export declare function createMemoryHistory(): {
11
- get: () => string;
12
- set: ({ value, scroll, replace }: LocationChange) => void;
13
- back: () => void;
14
- forward: () => void;
15
- go: (n: number) => void;
16
- listen: (listener: (value: string) => void) => () => void;
17
- };
18
- export type MemoryRouterProps = BaseRouterProps & {
19
- history?: MemoryHistory;
20
- actionBase?: string;
21
- explicitLinks?: boolean;
22
- preload?: boolean;
23
- };
24
- export declare function MemoryRouter(props: MemoryRouterProps): JSX.Element;
@@ -1,57 +0,0 @@
1
- import { createRouter, scrollToHash } from "./createRouter.js";
2
- import { setupNativeEvents } from "../data/events.js";
3
- export function createMemoryHistory() {
4
- const entries = ["/"];
5
- let index = 0;
6
- const listeners = [];
7
- const go = (n) => {
8
- // https://github.com/remix-run/react-router/blob/682810ca929d0e3c64a76f8d6e465196b7a2ac58/packages/router/history.ts#L245
9
- index = Math.max(0, Math.min(index + n, entries.length - 1));
10
- const value = entries[index];
11
- listeners.forEach(listener => listener(value));
12
- };
13
- return {
14
- get: () => entries[index],
15
- set: ({ value, scroll, replace }) => {
16
- if (replace) {
17
- entries[index] = value;
18
- }
19
- else {
20
- entries.splice(index + 1, entries.length - index, value);
21
- index++;
22
- }
23
- listeners.forEach(listener => listener(value));
24
- setTimeout(() => {
25
- if (scroll) {
26
- scrollToHash(value.split("#")[1] || "", true);
27
- }
28
- }, 0);
29
- },
30
- back: () => {
31
- go(-1);
32
- },
33
- forward: () => {
34
- go(1);
35
- },
36
- go,
37
- listen: (listener) => {
38
- listeners.push(listener);
39
- return () => {
40
- const index = listeners.indexOf(listener);
41
- listeners.splice(index, 1);
42
- };
43
- }
44
- };
45
- }
46
- export function MemoryRouter(props) {
47
- const memoryHistory = props.history || createMemoryHistory();
48
- return createRouter({
49
- get: memoryHistory.get,
50
- set: memoryHistory.set,
51
- init: memoryHistory.listen,
52
- create: setupNativeEvents({ preload: props.preload, explicitLinks: props.explicitLinks, actionBase: props.actionBase }),
53
- utils: {
54
- go: memoryHistory.go
55
- }
56
- })(props);
57
- }
@@ -1,9 +0,0 @@
1
- import type { BaseRouterProps } from "./components.jsx";
2
- import type { JSX } from "solid-js";
3
- export type RouterProps = BaseRouterProps & {
4
- url?: string;
5
- actionBase?: string;
6
- explicitLinks?: boolean;
7
- preload?: boolean;
8
- };
9
- export declare function Router(props: RouterProps): JSX.Element;
@@ -1,45 +0,0 @@
1
- import { isServer } from "solid-js/web";
2
- import { createRouter, scrollToHash, bindEvent } from "./createRouter.js";
3
- import { StaticRouter } from "./StaticRouter.js";
4
- import { setupNativeEvents } from "../data/events.js";
5
- import { createBeforeLeave, keepDepth, notifyIfNotBlocked, saveCurrentDepth } from "../lifecycle.js";
6
- export function Router(props) {
7
- if (isServer)
8
- return StaticRouter(props);
9
- const getSource = () => {
10
- const url = window.location.pathname.replace(/^\/+/, "/") + window.location.search;
11
- const state = window.history.state && window.history.state._depth && Object.keys(window.history.state).length === 1 ? undefined : window.history.state;
12
- return {
13
- value: url + window.location.hash,
14
- state
15
- };
16
- };
17
- const beforeLeave = createBeforeLeave();
18
- return createRouter({
19
- get: getSource,
20
- set({ value, replace, scroll, state }) {
21
- if (replace) {
22
- window.history.replaceState(keepDepth(state), "", value);
23
- }
24
- else {
25
- window.history.pushState(state, "", value);
26
- }
27
- scrollToHash(decodeURIComponent(window.location.hash.slice(1)), scroll);
28
- saveCurrentDepth();
29
- },
30
- init: notify => bindEvent(window, "popstate", notifyIfNotBlocked(notify, delta => {
31
- if (delta) {
32
- return !beforeLeave.confirm(delta);
33
- }
34
- else {
35
- const s = getSource();
36
- return !beforeLeave.confirm(s.value, { state: s.state });
37
- }
38
- })),
39
- create: setupNativeEvents({ preload: props.preload, explicitLinks: props.explicitLinks, actionBase: props.actionBase, transformUrl: props.transformUrl }),
40
- utils: {
41
- go: delta => window.history.go(delta),
42
- beforeLeave
43
- }
44
- })(props);
45
- }
@@ -1,6 +0,0 @@
1
- import { type BaseRouterProps } from "./components.jsx";
2
- import type { JSX } from "solid-js";
3
- export type StaticRouterProps = BaseRouterProps & {
4
- url?: string;
5
- };
6
- export declare function StaticRouter(props: StaticRouterProps): JSX.Element;
@@ -1,15 +0,0 @@
1
- import { getRequestEvent } from "solid-js/web";
2
- import { createRouterComponent } from "./components.jsx";
3
- function getPath(url) {
4
- const u = new URL(url);
5
- return u.pathname + u.search;
6
- }
7
- export function StaticRouter(props) {
8
- let e;
9
- const obj = {
10
- value: props.url || ((e = getRequestEvent()) && getPath(e.request.url)) || "",
11
- };
12
- return createRouterComponent({
13
- signal: [() => obj, next => Object.assign(obj, next)]
14
- })(props);
15
- }
@@ -1,27 +0,0 @@
1
- import type { Component, JSX } from "solid-js";
2
- import type { MatchFilters, RouteDefinition, RoutePreloadFunc, RouterIntegration, RouteSectionProps } from "../types.js";
3
- export type BaseRouterProps = {
4
- base?: string;
5
- /**
6
- * A component that wraps the content of every route.
7
- */
8
- root?: Component<RouteSectionProps>;
9
- rootPreload?: RoutePreloadFunc;
10
- singleFlight?: boolean;
11
- children?: JSX.Element | RouteDefinition | RouteDefinition[];
12
- transformUrl?: (url: string) => string;
13
- /** @deprecated use rootPreload */
14
- rootLoad?: RoutePreloadFunc;
15
- };
16
- export declare const createRouterComponent: (router: RouterIntegration) => (props: BaseRouterProps) => JSX.Element;
17
- export type RouteProps<S extends string, T = unknown> = {
18
- path?: S | S[];
19
- children?: JSX.Element;
20
- preload?: RoutePreloadFunc<T>;
21
- matchFilters?: MatchFilters<S>;
22
- component?: Component<RouteSectionProps<T>>;
23
- info?: Record<string, any>;
24
- /** @deprecated use preload */
25
- load?: RoutePreloadFunc<T>;
26
- };
27
- export declare const Route: <S extends string, T = unknown>(props: RouteProps<S, T>) => JSX.Element;
@@ -1,118 +0,0 @@
1
- /*@refresh skip*/
2
- import { children, createMemo, createRoot, getOwner, mergeProps, on, Show, untrack } from "solid-js";
3
- import { getRequestEvent, isServer } from "solid-js/web";
4
- import { createBranches, createRouteContext, createRouterContext, getIntent, getRouteMatches, RouteContextObj, RouterContextObj, setInPreloadFn } from "../routing.js";
5
- export const createRouterComponent = (router) => (props) => {
6
- const { base } = props;
7
- const routeDefs = children(() => props.children);
8
- const branches = createMemo(() => createBranches(routeDefs(), props.base || ""));
9
- let context;
10
- const routerState = createRouterContext(router, branches, () => context, {
11
- base,
12
- singleFlight: props.singleFlight,
13
- transformUrl: props.transformUrl,
14
- });
15
- router.create && router.create(routerState);
16
- return (<RouterContextObj.Provider value={routerState}>
17
- <Root routerState={routerState} root={props.root} preload={props.rootPreload || props.rootLoad}>
18
- {(context = getOwner()) && null}
19
- <Routes routerState={routerState} branches={branches()}/>
20
- </Root>
21
- </RouterContextObj.Provider>);
22
- };
23
- function Root(props) {
24
- const location = props.routerState.location;
25
- const params = props.routerState.params;
26
- const data = createMemo(() => props.preload &&
27
- untrack(() => {
28
- setInPreloadFn(true);
29
- props.preload({ params, location, intent: getIntent() || "initial" });
30
- setInPreloadFn(false);
31
- }));
32
- return (<Show when={props.root} keyed fallback={props.children}>
33
- {Root => (<Root params={params} location={location} data={data()}>
34
- {props.children}
35
- </Root>)}
36
- </Show>);
37
- }
38
- function Routes(props) {
39
- if (isServer) {
40
- const e = getRequestEvent();
41
- if (e && e.router && e.router.dataOnly) {
42
- dataOnly(e, props.routerState, props.branches);
43
- return;
44
- }
45
- e &&
46
- ((e.router || (e.router = {})).matches ||
47
- (e.router.matches = props.routerState.matches().map(({ route, path, params }) => ({
48
- path: route.originalPath,
49
- pattern: route.pattern,
50
- match: path,
51
- params,
52
- info: route.info
53
- }))));
54
- }
55
- const disposers = [];
56
- let root;
57
- const routeStates = createMemo(on(props.routerState.matches, (nextMatches, prevMatches, prev) => {
58
- let equal = prevMatches && nextMatches.length === prevMatches.length;
59
- const next = [];
60
- for (let i = 0, len = nextMatches.length; i < len; i++) {
61
- const prevMatch = prevMatches && prevMatches[i];
62
- const nextMatch = nextMatches[i];
63
- if (prev && prevMatch && nextMatch.route.key === prevMatch.route.key) {
64
- next[i] = prev[i];
65
- }
66
- else {
67
- equal = false;
68
- if (disposers[i]) {
69
- disposers[i]();
70
- }
71
- createRoot(dispose => {
72
- disposers[i] = dispose;
73
- next[i] = createRouteContext(props.routerState, next[i - 1] || props.routerState.base, createOutlet(() => routeStates()[i + 1]), () => {
74
- const routeMatches = props.routerState.matches();
75
- return routeMatches[i] ?? routeMatches[0];
76
- });
77
- });
78
- }
79
- }
80
- disposers.splice(nextMatches.length).forEach(dispose => dispose());
81
- if (prev && equal) {
82
- return prev;
83
- }
84
- root = next[0];
85
- return next;
86
- }));
87
- return createOutlet(() => routeStates() && root)();
88
- }
89
- const createOutlet = (child) => {
90
- return () => (<Show when={child()} keyed>
91
- {child => <RouteContextObj.Provider value={child}>{child.outlet()}</RouteContextObj.Provider>}
92
- </Show>);
93
- };
94
- export const Route = (props) => {
95
- const childRoutes = children(() => props.children);
96
- return mergeProps(props, {
97
- get children() {
98
- return childRoutes();
99
- }
100
- });
101
- };
102
- // for data only mode with single flight mutations
103
- function dataOnly(event, routerState, branches) {
104
- const url = new URL(event.request.url);
105
- const prevMatches = getRouteMatches(branches, new URL(event.router.previousUrl || event.request.url).pathname);
106
- const matches = getRouteMatches(branches, url.pathname);
107
- for (let match = 0; match < matches.length; match++) {
108
- if (!prevMatches[match] || matches[match].route !== prevMatches[match].route)
109
- event.router.dataOnly = true;
110
- const { route, params } = matches[match];
111
- route.preload &&
112
- route.preload({
113
- params,
114
- location: routerState.location,
115
- intent: "preload"
116
- });
117
- }
118
- }
@@ -1,10 +0,0 @@
1
- import type { LocationChange, RouterContext, RouterUtils } from "../types.js";
2
- export declare function createRouter(config: {
3
- get: () => string | LocationChange;
4
- set: (next: LocationChange) => void;
5
- init?: (notify: (value?: string | LocationChange) => void) => () => void;
6
- create?: (router: RouterContext) => void;
7
- utils?: Partial<RouterUtils>;
8
- }): (props: import("./components.jsx").BaseRouterProps) => import("solid-js").JSX.Element;
9
- export declare function bindEvent(target: EventTarget, type: string, handler: EventListener): () => void;
10
- export declare function scrollToHash(hash: string, fallbackTop?: boolean): void;