@solidjs/router 0.10.0-beta.5 → 0.10.0-beta.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/README.md +3 -3
- package/dist/components.jsx +0 -2
- package/dist/data/action.js +15 -8
- package/dist/data/cache.d.ts +6 -2
- package/dist/data/cache.js +55 -12
- package/dist/data/index.d.ts +2 -2
- package/dist/data/response.d.ts +5 -1
- package/dist/data/response.js +17 -5
- package/dist/index.js +94 -29
- package/dist/routers/HashRouter.d.ts +3 -2
- package/dist/routers/MemoryRouter.d.ts +20 -2
- package/dist/routers/MemoryRouter.js +21 -6
- package/dist/routers/Router.d.ts +4 -3
- package/dist/routers/StaticRouter.d.ts +4 -3
- package/dist/routers/components.d.ts +2 -2
- package/dist/routers/createRouter.d.ts +1 -1
- package/dist/routers/index.d.ts +6 -2
- package/dist/routers/index.js +1 -1
- package/dist/routing.js +1 -1
- package/dist/types.d.ts +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -382,12 +382,12 @@ const user = createAsync(() => getUser(params.id))
|
|
|
382
382
|
|
|
383
383
|
Actions are data mutations that can trigger invalidations and further routing. A list of prebuilt response builders can be found below(TODO).
|
|
384
384
|
```jsx
|
|
385
|
+
import { action, revalidate, redirect } from "@solidjs/router"
|
|
386
|
+
|
|
385
387
|
// anywhere
|
|
386
388
|
const myAction = action(async (data) => {
|
|
387
389
|
await doMutation(data);
|
|
388
|
-
|
|
389
|
-
invalidate: [getUser, data.id]
|
|
390
|
-
}) // returns a response
|
|
390
|
+
throw redirect("/", { revalidate: getUser.keyFor(data.id) }); // throw a response to do a redirect
|
|
391
391
|
});
|
|
392
392
|
|
|
393
393
|
// in component
|
package/dist/components.jsx
CHANGED
package/dist/data/action.js
CHANGED
|
@@ -77,18 +77,25 @@ export function action(fn, name) {
|
|
|
77
77
|
}
|
|
78
78
|
async function handleResponse(response, navigate) {
|
|
79
79
|
let data;
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
if (
|
|
83
|
-
|
|
80
|
+
let keys;
|
|
81
|
+
if (response instanceof Response) {
|
|
82
|
+
if (response.headers.has("X-Revalidate")) {
|
|
83
|
+
keys = response.headers.get("X-Revalidate").split(",");
|
|
84
84
|
}
|
|
85
|
-
|
|
86
|
-
|
|
85
|
+
if (response.customBody)
|
|
86
|
+
data = await response.customBody();
|
|
87
|
+
if (redirectStatusCodes.has(response.status)) {
|
|
88
|
+
const locationUrl = response.headers.get("Location") || "/";
|
|
89
|
+
if (locationUrl.startsWith("http")) {
|
|
90
|
+
window.location.href = locationUrl;
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
navigate(locationUrl);
|
|
94
|
+
}
|
|
87
95
|
}
|
|
88
96
|
}
|
|
89
97
|
else
|
|
90
98
|
data = response;
|
|
91
|
-
|
|
92
|
-
await revalidate();
|
|
99
|
+
await revalidate(keys);
|
|
93
100
|
return data;
|
|
94
101
|
}
|
package/dist/data/cache.d.ts
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
1
|
import { type ReconcileOptions } from "solid-js/store";
|
|
2
|
-
export declare function revalidate(key?: string |
|
|
3
|
-
export
|
|
2
|
+
export declare function revalidate(key?: string | string[] | void): Promise<void>;
|
|
3
|
+
export type CachedFunction<T extends (...args: any) => U | Response, U> = T & {
|
|
4
|
+
keyFor: (...args: Parameters<T>) => string;
|
|
5
|
+
key: string;
|
|
6
|
+
};
|
|
7
|
+
export declare function cache<T extends (...args: any) => U | Response, U>(fn: T, name: string, options?: ReconcileOptions): CachedFunction<T, U>;
|
package/dist/data/cache.js
CHANGED
|
@@ -5,7 +5,19 @@ import { useNavigate, getIntent } from "../routing";
|
|
|
5
5
|
import { redirectStatusCodes } from "../utils";
|
|
6
6
|
const LocationHeader = "Location";
|
|
7
7
|
const PRELOAD_TIMEOUT = 5000;
|
|
8
|
+
const CACHE_TIMEOUT = 180000;
|
|
8
9
|
let cacheMap = new Map();
|
|
10
|
+
// cleanup forward/back cache
|
|
11
|
+
if (!isServer) {
|
|
12
|
+
setInterval(() => {
|
|
13
|
+
const now = Date.now();
|
|
14
|
+
for (let [k, v] of cacheMap.entries()) {
|
|
15
|
+
if (!v[3].size && now - v[0] > CACHE_TIMEOUT) {
|
|
16
|
+
cacheMap.delete(k);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}, 300000);
|
|
20
|
+
}
|
|
9
21
|
function getCache() {
|
|
10
22
|
if (!isServer)
|
|
11
23
|
return cacheMap;
|
|
@@ -13,13 +25,14 @@ function getCache() {
|
|
|
13
25
|
return req.routerCache || (req.routerCache = new Map());
|
|
14
26
|
}
|
|
15
27
|
export function revalidate(key) {
|
|
28
|
+
key && !Array.isArray(key) && (key = [key]);
|
|
16
29
|
return startTransition(() => {
|
|
17
30
|
const now = Date.now();
|
|
18
31
|
for (let k of cacheMap.keys()) {
|
|
19
|
-
if (key === undefined || k
|
|
20
|
-
const
|
|
21
|
-
|
|
22
|
-
|
|
32
|
+
if (key === undefined || matchKey(k, key)) {
|
|
33
|
+
const entry = cacheMap.get(k);
|
|
34
|
+
entry[0] = 0; //force cache miss
|
|
35
|
+
revalidateSignals(entry[3], now); // retrigger live signals
|
|
23
36
|
}
|
|
24
37
|
}
|
|
25
38
|
});
|
|
@@ -30,13 +43,13 @@ function revalidateSignals(set, time) {
|
|
|
30
43
|
}
|
|
31
44
|
export function cache(fn, name, options) {
|
|
32
45
|
const [store, setStore] = createStore({});
|
|
33
|
-
|
|
46
|
+
const cachedFn = ((...args) => {
|
|
34
47
|
const cache = getCache();
|
|
35
48
|
const intent = getIntent();
|
|
36
49
|
const owner = getOwner();
|
|
37
50
|
const navigate = owner ? useNavigate() : undefined;
|
|
38
51
|
const now = Date.now();
|
|
39
|
-
const key = name + (args
|
|
52
|
+
const key = name + hashKey(args);
|
|
40
53
|
let cached = cache.get(key);
|
|
41
54
|
let version;
|
|
42
55
|
if (owner) {
|
|
@@ -53,9 +66,10 @@ export function cache(fn, name, options) {
|
|
|
53
66
|
}
|
|
54
67
|
let res = cached[1];
|
|
55
68
|
if (!isServer && intent === "navigate") {
|
|
56
|
-
res =
|
|
57
|
-
|
|
58
|
-
|
|
69
|
+
res =
|
|
70
|
+
"then" in cached[1]
|
|
71
|
+
? cached[1].then(handleResponse(false), handleResponse(true))
|
|
72
|
+
: handleResponse(false)(cached[1]);
|
|
59
73
|
startTransition(() => revalidateSignals(cached[3], cached[0])); // update version
|
|
60
74
|
}
|
|
61
75
|
return res;
|
|
@@ -79,9 +93,10 @@ export function cache(fn, name, options) {
|
|
|
79
93
|
else
|
|
80
94
|
cache.set(key, (cached = [now, res, intent, new Set(version ? [version] : [])]));
|
|
81
95
|
if (intent !== "preload") {
|
|
82
|
-
res =
|
|
83
|
-
|
|
84
|
-
|
|
96
|
+
res =
|
|
97
|
+
"then" in res
|
|
98
|
+
? res.then(handleResponse(false), handleResponse(true))
|
|
99
|
+
: handleResponse(false)(res);
|
|
85
100
|
}
|
|
86
101
|
return res;
|
|
87
102
|
function handleResponse(error) {
|
|
@@ -111,4 +126,32 @@ export function cache(fn, name, options) {
|
|
|
111
126
|
};
|
|
112
127
|
}
|
|
113
128
|
});
|
|
129
|
+
cachedFn.keyFor = (...args) => name + hashKey(args);
|
|
130
|
+
cachedFn.key = name;
|
|
131
|
+
return cachedFn;
|
|
132
|
+
}
|
|
133
|
+
function matchKey(key, keys) {
|
|
134
|
+
for (let k of keys) {
|
|
135
|
+
if (key.startsWith(k))
|
|
136
|
+
return true;
|
|
137
|
+
}
|
|
138
|
+
return false;
|
|
139
|
+
}
|
|
140
|
+
// Modified from the amazing Tanstack Query library (MIT)
|
|
141
|
+
// https://github.com/TanStack/query/blob/main/packages/query-core/src/utils.ts#L168
|
|
142
|
+
function hashKey(args) {
|
|
143
|
+
return JSON.stringify(args, (_, val) => isPlainObject(val)
|
|
144
|
+
? Object.keys(val)
|
|
145
|
+
.sort()
|
|
146
|
+
.reduce((result, key) => {
|
|
147
|
+
result[key] = val[key];
|
|
148
|
+
return result;
|
|
149
|
+
}, {})
|
|
150
|
+
: val);
|
|
151
|
+
}
|
|
152
|
+
function isPlainObject(obj) {
|
|
153
|
+
let proto;
|
|
154
|
+
return (obj != null &&
|
|
155
|
+
typeof obj === "object" &&
|
|
156
|
+
(!(proto = Object.getPrototypeOf(obj)) || proto === Object.prototype));
|
|
114
157
|
}
|
package/dist/data/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export { createAsync } from "./createAsync";
|
|
2
|
-
export { action, useSubmission, useSubmissions, useAction } from "./action";
|
|
3
|
-
export { cache, revalidate } from "./cache";
|
|
2
|
+
export { action, useSubmission, useSubmissions, useAction, type Action } from "./action";
|
|
3
|
+
export { cache, revalidate, type CachedFunction } from "./cache";
|
|
4
4
|
export { redirect } from "./response";
|
package/dist/data/response.d.ts
CHANGED
|
@@ -1 +1,5 @@
|
|
|
1
|
-
export
|
|
1
|
+
export type RouterResponseInit = ResponseInit & {
|
|
2
|
+
revalidate?: string | string[];
|
|
3
|
+
};
|
|
4
|
+
export declare function redirect(url: string, init?: number | RouterResponseInit): Response;
|
|
5
|
+
export declare function reload(init: RouterResponseInit): Response;
|
package/dist/data/response.js
CHANGED
|
@@ -1,16 +1,28 @@
|
|
|
1
1
|
export function redirect(url, init = 302) {
|
|
2
|
-
let responseInit
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
let responseInit;
|
|
3
|
+
let revalidate;
|
|
4
|
+
if (typeof init === "number") {
|
|
5
|
+
responseInit = { status: init };
|
|
5
6
|
}
|
|
6
|
-
else
|
|
7
|
-
responseInit
|
|
7
|
+
else {
|
|
8
|
+
({ revalidate, ...responseInit } = init);
|
|
9
|
+
if (typeof responseInit.status === "undefined") {
|
|
10
|
+
responseInit.status = 302;
|
|
11
|
+
}
|
|
8
12
|
}
|
|
9
13
|
const headers = new Headers(responseInit.headers);
|
|
10
14
|
headers.set("Location", url);
|
|
15
|
+
revalidate && headers.set("X-Revalidate", revalidate.toString());
|
|
11
16
|
const response = new Response(null, {
|
|
12
17
|
...responseInit,
|
|
13
18
|
headers: headers
|
|
14
19
|
});
|
|
15
20
|
return response;
|
|
16
21
|
}
|
|
22
|
+
export function reload(init) {
|
|
23
|
+
const { revalidate, ...responseInit } = init;
|
|
24
|
+
return new Response(null, {
|
|
25
|
+
...responseInit,
|
|
26
|
+
...(revalidate ? { headers: new Headers(responseInit.headers).set("X-Revalidate", revalidate.toString()) } : {})
|
|
27
|
+
});
|
|
28
|
+
}
|
package/dist/index.js
CHANGED
|
@@ -586,7 +586,7 @@ function createRouteContext(router, parent, outlet, match, params) {
|
|
|
586
586
|
load && load({
|
|
587
587
|
params,
|
|
588
588
|
location,
|
|
589
|
-
intent: intent || "
|
|
589
|
+
intent: intent || "initial"
|
|
590
590
|
});
|
|
591
591
|
return route;
|
|
592
592
|
}
|
|
@@ -757,36 +757,51 @@ function StaticRouter(props) {
|
|
|
757
757
|
|
|
758
758
|
const LocationHeader = "Location";
|
|
759
759
|
const PRELOAD_TIMEOUT = 5000;
|
|
760
|
+
const CACHE_TIMEOUT = 180000;
|
|
760
761
|
let cacheMap = new Map();
|
|
762
|
+
|
|
763
|
+
// cleanup forward/back cache
|
|
764
|
+
if (!isServer) {
|
|
765
|
+
setInterval(() => {
|
|
766
|
+
const now = Date.now();
|
|
767
|
+
for (let [k, v] of cacheMap.entries()) {
|
|
768
|
+
if (!v[3].size && now - v[0] > CACHE_TIMEOUT) {
|
|
769
|
+
cacheMap.delete(k);
|
|
770
|
+
}
|
|
771
|
+
}
|
|
772
|
+
}, 300000);
|
|
773
|
+
}
|
|
761
774
|
function getCache() {
|
|
762
775
|
if (!isServer) return cacheMap;
|
|
763
776
|
const req = getRequestEvent() || sharedConfig.context;
|
|
764
777
|
return req.routerCache || (req.routerCache = new Map());
|
|
765
778
|
}
|
|
766
779
|
function revalidate(key) {
|
|
780
|
+
key && !Array.isArray(key) && (key = [key]);
|
|
767
781
|
return startTransition(() => {
|
|
768
782
|
const now = Date.now();
|
|
769
783
|
for (let k of cacheMap.keys()) {
|
|
770
|
-
if (key === undefined || k
|
|
771
|
-
const
|
|
772
|
-
|
|
773
|
-
|
|
784
|
+
if (key === undefined || matchKey(k, key)) {
|
|
785
|
+
const entry = cacheMap.get(k);
|
|
786
|
+
entry[0] = 0; //force cache miss
|
|
787
|
+
revalidateSignals(entry[3], now); // retrigger live signals
|
|
774
788
|
}
|
|
775
789
|
}
|
|
776
790
|
});
|
|
777
791
|
}
|
|
792
|
+
|
|
778
793
|
function revalidateSignals(set, time) {
|
|
779
794
|
for (let s of set) s[1](time);
|
|
780
795
|
}
|
|
781
796
|
function cache(fn, name, options) {
|
|
782
797
|
const [store, setStore] = createStore({});
|
|
783
|
-
|
|
798
|
+
const cachedFn = (...args) => {
|
|
784
799
|
const cache = getCache();
|
|
785
800
|
const intent = getIntent();
|
|
786
801
|
const owner = getOwner();
|
|
787
802
|
const navigate = owner ? useNavigate() : undefined;
|
|
788
803
|
const now = Date.now();
|
|
789
|
-
const key = name + (args
|
|
804
|
+
const key = name + hashKey(args);
|
|
790
805
|
let cached = cache.get(key);
|
|
791
806
|
let version;
|
|
792
807
|
if (owner) {
|
|
@@ -855,6 +870,28 @@ function cache(fn, name, options) {
|
|
|
855
870
|
};
|
|
856
871
|
}
|
|
857
872
|
};
|
|
873
|
+
cachedFn.keyFor = (...args) => name + hashKey(args);
|
|
874
|
+
cachedFn.key = name;
|
|
875
|
+
return cachedFn;
|
|
876
|
+
}
|
|
877
|
+
function matchKey(key, keys) {
|
|
878
|
+
for (let k of keys) {
|
|
879
|
+
if (key.startsWith(k)) return true;
|
|
880
|
+
}
|
|
881
|
+
return false;
|
|
882
|
+
}
|
|
883
|
+
|
|
884
|
+
// Modified from the amazing Tanstack Query library (MIT)
|
|
885
|
+
// https://github.com/TanStack/query/blob/main/packages/query-core/src/utils.ts#L168
|
|
886
|
+
function hashKey(args) {
|
|
887
|
+
return JSON.stringify(args, (_, val) => isPlainObject(val) ? Object.keys(val).sort().reduce((result, key) => {
|
|
888
|
+
result[key] = val[key];
|
|
889
|
+
return result;
|
|
890
|
+
}, {}) : val);
|
|
891
|
+
}
|
|
892
|
+
function isPlainObject(obj) {
|
|
893
|
+
let proto;
|
|
894
|
+
return obj != null && typeof obj === "object" && (!(proto = Object.getPrototypeOf(obj)) || proto === Object.prototype);
|
|
858
895
|
}
|
|
859
896
|
|
|
860
897
|
const actions = /* #__PURE__ */new Map();
|
|
@@ -926,16 +963,22 @@ function action(fn, name) {
|
|
|
926
963
|
}
|
|
927
964
|
async function handleResponse(response, navigate) {
|
|
928
965
|
let data;
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
if (
|
|
932
|
-
|
|
933
|
-
}
|
|
934
|
-
|
|
966
|
+
let keys;
|
|
967
|
+
if (response instanceof Response) {
|
|
968
|
+
if (response.headers.has("X-Revalidate")) {
|
|
969
|
+
keys = response.headers.get("X-Revalidate").split(",");
|
|
970
|
+
}
|
|
971
|
+
if (response.customBody) data = await response.customBody();
|
|
972
|
+
if (redirectStatusCodes.has(response.status)) {
|
|
973
|
+
const locationUrl = response.headers.get("Location") || "/";
|
|
974
|
+
if (locationUrl.startsWith("http")) {
|
|
975
|
+
window.location.href = locationUrl;
|
|
976
|
+
} else {
|
|
977
|
+
navigate(locationUrl);
|
|
978
|
+
}
|
|
935
979
|
}
|
|
936
980
|
} else data = response;
|
|
937
|
-
|
|
938
|
-
await revalidate();
|
|
981
|
+
await revalidate(keys);
|
|
939
982
|
return data;
|
|
940
983
|
}
|
|
941
984
|
|
|
@@ -1100,7 +1143,7 @@ function HashRouter(props) {
|
|
|
1100
1143
|
})(props);
|
|
1101
1144
|
}
|
|
1102
1145
|
|
|
1103
|
-
function
|
|
1146
|
+
function createMemoryHistory() {
|
|
1104
1147
|
const entries = ["/"];
|
|
1105
1148
|
let index = 0;
|
|
1106
1149
|
const listeners = [];
|
|
@@ -1110,13 +1153,13 @@ function MemoryRouter(props) {
|
|
|
1110
1153
|
const value = entries[index];
|
|
1111
1154
|
listeners.forEach(listener => listener(value));
|
|
1112
1155
|
};
|
|
1113
|
-
return
|
|
1156
|
+
return {
|
|
1114
1157
|
get: () => entries[index],
|
|
1115
|
-
set({
|
|
1158
|
+
set: ({
|
|
1116
1159
|
value,
|
|
1117
1160
|
scroll,
|
|
1118
1161
|
replace
|
|
1119
|
-
}) {
|
|
1162
|
+
}) => {
|
|
1120
1163
|
if (replace) {
|
|
1121
1164
|
entries[index] = value;
|
|
1122
1165
|
} else {
|
|
@@ -1127,20 +1170,34 @@ function MemoryRouter(props) {
|
|
|
1127
1170
|
scrollToHash(value.split("#")[1] || "", true);
|
|
1128
1171
|
}
|
|
1129
1172
|
},
|
|
1130
|
-
|
|
1173
|
+
back: () => {
|
|
1174
|
+
go(-1);
|
|
1175
|
+
},
|
|
1176
|
+
forward: () => {
|
|
1177
|
+
go(1);
|
|
1178
|
+
},
|
|
1179
|
+
go,
|
|
1180
|
+
listen: listener => {
|
|
1131
1181
|
listeners.push(listener);
|
|
1132
1182
|
return () => {
|
|
1133
1183
|
const index = listeners.indexOf(listener);
|
|
1134
1184
|
listeners.splice(index, 1);
|
|
1135
1185
|
};
|
|
1136
|
-
}
|
|
1186
|
+
}
|
|
1187
|
+
};
|
|
1188
|
+
}
|
|
1189
|
+
function MemoryRouter(props) {
|
|
1190
|
+
const memoryHistory = props.history || createMemoryHistory();
|
|
1191
|
+
return createRouter({
|
|
1192
|
+
get: memoryHistory.get,
|
|
1193
|
+
set: memoryHistory.set,
|
|
1194
|
+
init: memoryHistory.listen,
|
|
1137
1195
|
utils: {
|
|
1138
|
-
go
|
|
1196
|
+
go: memoryHistory.go
|
|
1139
1197
|
}
|
|
1140
1198
|
})(props);
|
|
1141
1199
|
}
|
|
1142
1200
|
|
|
1143
|
-
/*@refresh skip*/
|
|
1144
1201
|
const _tmpl$ = /*#__PURE__*/template(`<a>`);
|
|
1145
1202
|
function A(props) {
|
|
1146
1203
|
props = mergeProps({
|
|
@@ -1255,16 +1312,24 @@ function subFetch(fn) {
|
|
|
1255
1312
|
}
|
|
1256
1313
|
|
|
1257
1314
|
function redirect(url, init = 302) {
|
|
1258
|
-
let responseInit
|
|
1259
|
-
|
|
1315
|
+
let responseInit;
|
|
1316
|
+
let revalidate;
|
|
1317
|
+
if (typeof init === "number") {
|
|
1260
1318
|
responseInit = {
|
|
1261
|
-
status:
|
|
1319
|
+
status: init
|
|
1262
1320
|
};
|
|
1263
|
-
} else
|
|
1264
|
-
|
|
1321
|
+
} else {
|
|
1322
|
+
({
|
|
1323
|
+
revalidate,
|
|
1324
|
+
...responseInit
|
|
1325
|
+
} = init);
|
|
1326
|
+
if (typeof responseInit.status === "undefined") {
|
|
1327
|
+
responseInit.status = 302;
|
|
1328
|
+
}
|
|
1265
1329
|
}
|
|
1266
1330
|
const headers = new Headers(responseInit.headers);
|
|
1267
1331
|
headers.set("Location", url);
|
|
1332
|
+
revalidate && headers.set("X-Revalidate", revalidate.toString());
|
|
1268
1333
|
const response = new Response(null, {
|
|
1269
1334
|
...responseInit,
|
|
1270
1335
|
headers: headers
|
|
@@ -1272,4 +1337,4 @@ function redirect(url, init = 302) {
|
|
|
1272
1337
|
return response;
|
|
1273
1338
|
}
|
|
1274
1339
|
|
|
1275
|
-
export { A, HashRouter, MemoryRouter, Navigate, Route, Router, StaticRouter, mergeSearchString as _mergeSearchString, action, cache, createAsync, createBeforeLeave, createRouter, redirect, revalidate, useAction, useBeforeLeave, useHref, useIsRouting, useLocation, useMatch, useNavigate, useParams, useResolvedPath, useSearchParams, useSubmission, useSubmissions };
|
|
1340
|
+
export { A, HashRouter, MemoryRouter, Navigate, Route, Router, StaticRouter, mergeSearchString as _mergeSearchString, action, cache, createAsync, createBeforeLeave, createMemoryHistory, createRouter, redirect, revalidate, useAction, useBeforeLeave, useHref, useIsRouting, useLocation, useMatch, useNavigate, useParams, useResolvedPath, useSearchParams, useSubmission, useSubmissions };
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { JSX } from "solid-js";
|
|
2
|
-
import type {
|
|
2
|
+
import type { BaseRouterProps } from "./components";
|
|
3
3
|
export declare function hashParser(str: string): string;
|
|
4
|
-
export
|
|
4
|
+
export type HashRouterProps = BaseRouterProps;
|
|
5
|
+
export declare function HashRouter(props: HashRouterProps): JSX.Element;
|
|
@@ -1,3 +1,21 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { LocationChange } from "../types";
|
|
2
|
+
import type { BaseRouterProps } from "./components";
|
|
2
3
|
import type { JSX } from "solid-js";
|
|
3
|
-
export
|
|
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
|
+
};
|
|
21
|
+
export declare function MemoryRouter(props: MemoryRouterProps): JSX.Element;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { createRouter, scrollToHash } from "./createRouter";
|
|
2
|
-
export function
|
|
2
|
+
export function createMemoryHistory() {
|
|
3
3
|
const entries = ["/"];
|
|
4
4
|
let index = 0;
|
|
5
5
|
const listeners = [];
|
|
@@ -9,9 +9,9 @@ export function MemoryRouter(props) {
|
|
|
9
9
|
const value = entries[index];
|
|
10
10
|
listeners.forEach(listener => listener(value));
|
|
11
11
|
};
|
|
12
|
-
return
|
|
12
|
+
return {
|
|
13
13
|
get: () => entries[index],
|
|
14
|
-
set({ value, scroll, replace }) {
|
|
14
|
+
set: ({ value, scroll, replace }) => {
|
|
15
15
|
if (replace) {
|
|
16
16
|
entries[index] = value;
|
|
17
17
|
}
|
|
@@ -23,15 +23,30 @@ export function MemoryRouter(props) {
|
|
|
23
23
|
scrollToHash(value.split("#")[1] || "", true);
|
|
24
24
|
}
|
|
25
25
|
},
|
|
26
|
-
|
|
26
|
+
back: () => {
|
|
27
|
+
go(-1);
|
|
28
|
+
},
|
|
29
|
+
forward: () => {
|
|
30
|
+
go(1);
|
|
31
|
+
},
|
|
32
|
+
go,
|
|
33
|
+
listen: (listener) => {
|
|
27
34
|
listeners.push(listener);
|
|
28
35
|
return () => {
|
|
29
36
|
const index = listeners.indexOf(listener);
|
|
30
37
|
listeners.splice(index, 1);
|
|
31
38
|
};
|
|
32
|
-
}
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
export function MemoryRouter(props) {
|
|
43
|
+
const memoryHistory = props.history || createMemoryHistory();
|
|
44
|
+
return createRouter({
|
|
45
|
+
get: memoryHistory.get,
|
|
46
|
+
set: memoryHistory.set,
|
|
47
|
+
init: memoryHistory.listen,
|
|
33
48
|
utils: {
|
|
34
|
-
go
|
|
49
|
+
go: memoryHistory.go
|
|
35
50
|
}
|
|
36
51
|
})(props);
|
|
37
52
|
}
|
package/dist/routers/Router.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { BaseRouterProps } from "./components";
|
|
2
2
|
import type { JSX } from "solid-js";
|
|
3
|
-
export
|
|
3
|
+
export type RouterProps = BaseRouterProps & {
|
|
4
4
|
url?: string;
|
|
5
|
-
}
|
|
5
|
+
};
|
|
6
|
+
export declare function Router(props: RouterProps): JSX.Element;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { type
|
|
1
|
+
import { type BaseRouterProps } from "./components";
|
|
2
2
|
import type { JSX } from "solid-js";
|
|
3
|
-
export
|
|
3
|
+
export type StaticRouterProps = BaseRouterProps & {
|
|
4
4
|
url?: string;
|
|
5
|
-
}
|
|
5
|
+
};
|
|
6
|
+
export declare function StaticRouter(props: StaticRouterProps): JSX.Element;
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import type { Component, JSX } from "solid-js";
|
|
2
2
|
import type { MatchFilters, RouteLoadFunc, RouterIntegration, RouteSectionProps } from "../types";
|
|
3
|
-
export type
|
|
3
|
+
export type BaseRouterProps = {
|
|
4
4
|
base?: string;
|
|
5
5
|
actionBase?: string;
|
|
6
6
|
root?: Component<RouteSectionProps>;
|
|
7
7
|
children?: JSX.Element;
|
|
8
8
|
};
|
|
9
|
-
export declare const createRouterComponent: (router: RouterIntegration) => (props:
|
|
9
|
+
export declare const createRouterComponent: (router: RouterIntegration) => (props: BaseRouterProps) => JSX.Element;
|
|
10
10
|
export type RouteProps<S extends string> = {
|
|
11
11
|
path?: S | S[];
|
|
12
12
|
children?: JSX.Element;
|
|
@@ -5,6 +5,6 @@ export declare function createRouter(config: {
|
|
|
5
5
|
init?: (notify: (value?: string | LocationChange) => void) => () => void;
|
|
6
6
|
create?: (router: RouterContext) => void;
|
|
7
7
|
utils?: Partial<RouterUtils>;
|
|
8
|
-
}): (props: import("./components").
|
|
8
|
+
}): (props: import("./components").BaseRouterProps) => import("solid-js").JSX.Element;
|
|
9
9
|
export declare function bindEvent(target: EventTarget, type: string, handler: EventListener): () => void;
|
|
10
10
|
export declare function scrollToHash(hash: string, fallbackTop?: boolean): void;
|
package/dist/routers/index.d.ts
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
export { Route } from "./components";
|
|
2
|
-
export type {
|
|
2
|
+
export type { BaseRouterProps, RouteProps } from "./components";
|
|
3
3
|
export { createRouter } from "./createRouter";
|
|
4
4
|
export { Router } from "./Router";
|
|
5
|
+
export type { RouterProps } from "./Router";
|
|
5
6
|
export { HashRouter } from "./HashRouter";
|
|
6
|
-
export {
|
|
7
|
+
export type { HashRouterProps } from "./HashRouter";
|
|
8
|
+
export { MemoryRouter, createMemoryHistory } from "./MemoryRouter";
|
|
9
|
+
export type { MemoryRouterProps, MemoryHistory } from "./MemoryRouter";
|
|
7
10
|
export { StaticRouter } from "./StaticRouter";
|
|
11
|
+
export type { StaticRouterProps } from "./StaticRouter";
|
package/dist/routers/index.js
CHANGED
|
@@ -2,5 +2,5 @@ export { Route } from "./components";
|
|
|
2
2
|
export { createRouter } from "./createRouter";
|
|
3
3
|
export { Router } from "./Router";
|
|
4
4
|
export { HashRouter } from "./HashRouter";
|
|
5
|
-
export { MemoryRouter } from "./MemoryRouter";
|
|
5
|
+
export { MemoryRouter, createMemoryHistory } from "./MemoryRouter";
|
|
6
6
|
export { StaticRouter } from "./StaticRouter";
|
package/dist/routing.js
CHANGED
|
@@ -381,6 +381,6 @@ export function createRouteContext(router, parent, outlet, match, params) {
|
|
|
381
381
|
component &&
|
|
382
382
|
component.preload &&
|
|
383
383
|
component.preload();
|
|
384
|
-
load && load({ params, location, intent: intent || "
|
|
384
|
+
load && load({ params, location, intent: intent || "initial" });
|
|
385
385
|
return route;
|
|
386
386
|
}
|
package/dist/types.d.ts
CHANGED
|
@@ -39,7 +39,7 @@ export interface RouterIntegration {
|
|
|
39
39
|
create?: (router: RouterContext) => void;
|
|
40
40
|
utils?: Partial<RouterUtils>;
|
|
41
41
|
}
|
|
42
|
-
export type Intent = "native" | "navigate" | "preload";
|
|
42
|
+
export type Intent = "initial" | "native" | "navigate" | "preload";
|
|
43
43
|
export interface RouteLoadFuncArgs {
|
|
44
44
|
params: Params;
|
|
45
45
|
location: Location;
|