@solidjs/router 0.10.0-beta.1 → 0.10.0-beta.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components.jsx +5 -1
- package/dist/data/action.d.ts +2 -1
- package/dist/data/action.js +15 -20
- package/dist/data/cache.js +30 -31
- package/dist/data/createAsync.d.ts +10 -1
- package/dist/data/createAsync.js +2 -2
- package/dist/data/index.d.ts +2 -1
- package/dist/data/index.js +2 -1
- package/dist/data/response.d.ts +1 -0
- package/dist/data/response.js +16 -0
- package/dist/index.js +89 -69
- package/dist/routing.js +14 -12
- package/package.json +4 -4
package/dist/components.jsx
CHANGED
|
@@ -9,7 +9,7 @@ export const Router = (props) => {
|
|
|
9
9
|
const { source, url, base } = props;
|
|
10
10
|
const integration = source ||
|
|
11
11
|
(isServer
|
|
12
|
-
? staticIntegration({ value: url || ((e = getRequestEvent()) && e.request.url) || "" })
|
|
12
|
+
? staticIntegration({ value: url || ((e = getRequestEvent()) && getPath(e.request.url)) || "" })
|
|
13
13
|
: pathIntegration());
|
|
14
14
|
const routeDefs = children(() => props.children);
|
|
15
15
|
const branches = createMemo(() => createBranches(props.root ? { component: props.root, children: routeDefs() } : routeDefs(), props.base || ""));
|
|
@@ -18,6 +18,10 @@ export const Router = (props) => {
|
|
|
18
18
|
<Routes routerState={routerState} branches={branches()}/>
|
|
19
19
|
</RouterContextObj.Provider>);
|
|
20
20
|
};
|
|
21
|
+
function getPath(url) {
|
|
22
|
+
const u = new URL(url);
|
|
23
|
+
return u.pathname + u.search;
|
|
24
|
+
}
|
|
21
25
|
function Routes(props) {
|
|
22
26
|
const matches = createMemo(() => getRouteMatches(props.branches, props.routerState.location.pathname));
|
|
23
27
|
const params = createMemoObject(() => {
|
package/dist/data/action.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
import { JSX } from "solid-js";
|
|
1
2
|
import { Submission } from "../types";
|
|
2
|
-
export type Action<T, U> = (vars: T) => Promise<U
|
|
3
|
+
export type Action<T, U> = ((vars: T) => Promise<U>) & JSX.SerializableAttributeValue;
|
|
3
4
|
export declare function useSubmissions<T, U>(fn: Action<T, U>, filter?: (arg: T) => boolean): Submission<T, U>[] & {
|
|
4
5
|
pending: boolean;
|
|
5
6
|
};
|
package/dist/data/action.js
CHANGED
|
@@ -49,6 +49,11 @@ export function action(fn, name) {
|
|
|
49
49
|
const [result, setResult] = createSignal();
|
|
50
50
|
let submission;
|
|
51
51
|
const router = this;
|
|
52
|
+
async function handler(res) {
|
|
53
|
+
const data = await handleResponse(res, router.navigatorFactory());
|
|
54
|
+
data ? setResult({ data }) : submission.clear();
|
|
55
|
+
return data;
|
|
56
|
+
}
|
|
52
57
|
router.submissions[1](s => [
|
|
53
58
|
...s,
|
|
54
59
|
(submission = {
|
|
@@ -66,29 +71,15 @@ export function action(fn, name) {
|
|
|
66
71
|
retry() {
|
|
67
72
|
setResult(undefined);
|
|
68
73
|
const p = fn(variables);
|
|
69
|
-
p.then(
|
|
70
|
-
const keys = handleResponse(data, router.navigatorFactory());
|
|
71
|
-
await revalidate(keys);
|
|
72
|
-
data ? setResult({ data }) : submission.clear();
|
|
73
|
-
return data;
|
|
74
|
-
}).catch(error => {
|
|
75
|
-
setResult({ data: error });
|
|
76
|
-
});
|
|
74
|
+
p.then(handler, handler);
|
|
77
75
|
return p;
|
|
78
76
|
}
|
|
79
77
|
})
|
|
80
78
|
]);
|
|
81
|
-
p.then(
|
|
82
|
-
const keys = handleResponse(data, router.navigatorFactory());
|
|
83
|
-
await revalidate(keys);
|
|
84
|
-
data ? setResult({ data }) : submission.clear();
|
|
85
|
-
return data;
|
|
86
|
-
}).catch(error => {
|
|
87
|
-
setResult({ data: error });
|
|
88
|
-
});
|
|
79
|
+
p.then(handler, handler);
|
|
89
80
|
return p;
|
|
90
81
|
}
|
|
91
|
-
const url = fn.url || `action:${name}` || !isServer ? `action:${fn.name}` : "";
|
|
82
|
+
const url = fn.url || (name && `action:${name}`) || (!isServer ? `action:${fn.name}` : "");
|
|
92
83
|
mutate.toString = () => {
|
|
93
84
|
if (!url)
|
|
94
85
|
throw new Error("Client Actions need explicit names if server rendered");
|
|
@@ -98,7 +89,8 @@ export function action(fn, name) {
|
|
|
98
89
|
registerAction(url, mutate);
|
|
99
90
|
return mutate;
|
|
100
91
|
}
|
|
101
|
-
function handleResponse(response, navigate) {
|
|
92
|
+
async function handleResponse(response, navigate) {
|
|
93
|
+
let data;
|
|
102
94
|
if (response instanceof Response && redirectStatusCodes.has(response.status)) {
|
|
103
95
|
const locationUrl = response.headers.get("Location") || "/";
|
|
104
96
|
if (locationUrl.startsWith("http")) {
|
|
@@ -108,6 +100,9 @@ function handleResponse(response, navigate) {
|
|
|
108
100
|
navigate(locationUrl);
|
|
109
101
|
}
|
|
110
102
|
}
|
|
111
|
-
|
|
112
|
-
|
|
103
|
+
else
|
|
104
|
+
data = response;
|
|
105
|
+
// TODO: handle keys
|
|
106
|
+
await revalidate();
|
|
107
|
+
return data;
|
|
113
108
|
}
|
package/dist/data/cache.js
CHANGED
|
@@ -50,13 +50,11 @@ export function cache(fn, name, options) {
|
|
|
50
50
|
version && cached[3].add(version);
|
|
51
51
|
if (cached[2] === "preload" && intent !== "preload") {
|
|
52
52
|
cached[0] = now;
|
|
53
|
-
cached[1] =
|
|
54
|
-
"then" in cached[1]
|
|
55
|
-
? cached[1].then(handleResponse)
|
|
56
|
-
: handleResponse(cached[1]);
|
|
57
|
-
cached[2] = intent;
|
|
58
53
|
}
|
|
59
54
|
if (!isServer && intent === "navigate") {
|
|
55
|
+
"then" in cached[1]
|
|
56
|
+
? cached[1].then(handleResponse(false), handleResponse(true))
|
|
57
|
+
: handleResponse(false)(cached[1]);
|
|
60
58
|
startTransition(() => revalidateSignals(cached[3], cached[0])); // update version
|
|
61
59
|
}
|
|
62
60
|
return cached[1];
|
|
@@ -69,10 +67,9 @@ export function cache(fn, name, options) {
|
|
|
69
67
|
sharedConfig.context && sharedConfig.context.serialize(key, res);
|
|
70
68
|
}
|
|
71
69
|
if (intent !== "preload") {
|
|
72
|
-
res
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
: handleResponse(res);
|
|
70
|
+
"then" in res
|
|
71
|
+
? res.then(handleResponse(false), handleResponse(true))
|
|
72
|
+
: handleResponse(false)(res);
|
|
76
73
|
}
|
|
77
74
|
if (cached) {
|
|
78
75
|
cached[0] = now;
|
|
@@ -86,29 +83,31 @@ export function cache(fn, name, options) {
|
|
|
86
83
|
else
|
|
87
84
|
cache.set(key, (cached = [now, res, intent, new Set(version ? [version] : [])]));
|
|
88
85
|
return res;
|
|
89
|
-
function
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
86
|
+
function handleResponse(error) {
|
|
87
|
+
return (v) => {
|
|
88
|
+
if (v instanceof Response && redirectStatusCodes.has(v.status)) {
|
|
89
|
+
if (navigate) {
|
|
90
|
+
startTransition(() => {
|
|
91
|
+
let url = v.headers.get(LocationHeader);
|
|
92
|
+
if (url && url.startsWith("/")) {
|
|
93
|
+
navigate(url, {
|
|
94
|
+
replace: true
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
else if (!isServer && url) {
|
|
98
|
+
window.location.href = url;
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
return;
|
|
96
103
|
}
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
if (navigate)
|
|
105
|
-
isServer ? handleRedirect(v) : setTimeout(() => handleRedirect(v), 0);
|
|
106
|
-
return;
|
|
107
|
-
}
|
|
108
|
-
if (isServer)
|
|
109
|
-
return v;
|
|
110
|
-
setStore(key, reconcile(v, options));
|
|
111
|
-
return store[key];
|
|
104
|
+
if (error)
|
|
105
|
+
throw error;
|
|
106
|
+
if (isServer)
|
|
107
|
+
return v;
|
|
108
|
+
setStore(key, reconcile(v, options));
|
|
109
|
+
return store[key];
|
|
110
|
+
};
|
|
112
111
|
}
|
|
113
112
|
});
|
|
114
113
|
}
|
|
@@ -2,4 +2,13 @@
|
|
|
2
2
|
* This is mock of the eventual Solid 2.0 primitive. It is not fully featured.
|
|
3
3
|
*/
|
|
4
4
|
import { type Accessor } from "solid-js";
|
|
5
|
-
export declare function createAsync<T>(fn: () => Promise<T
|
|
5
|
+
export declare function createAsync<T>(fn: () => Promise<T>, options: {
|
|
6
|
+
name?: string;
|
|
7
|
+
initialValue: T;
|
|
8
|
+
deferStream?: boolean;
|
|
9
|
+
}): Accessor<T>;
|
|
10
|
+
export declare function createAsync<T>(fn: () => Promise<T>, options?: {
|
|
11
|
+
name?: string;
|
|
12
|
+
initialValue?: T;
|
|
13
|
+
deferStream?: boolean;
|
|
14
|
+
}): Accessor<T | undefined>;
|
package/dist/data/createAsync.js
CHANGED
|
@@ -3,8 +3,8 @@
|
|
|
3
3
|
*/
|
|
4
4
|
import { createResource, sharedConfig } from "solid-js";
|
|
5
5
|
import { isServer } from "solid-js/web";
|
|
6
|
-
export function createAsync(fn) {
|
|
7
|
-
const [resource] = createResource(() => subFetch(fn), v => v);
|
|
6
|
+
export function createAsync(fn, options) {
|
|
7
|
+
const [resource] = createResource(() => subFetch(fn), v => v, options);
|
|
8
8
|
return () => resource();
|
|
9
9
|
}
|
|
10
10
|
// mock promise while hydrating to prevent fetching
|
package/dist/data/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
export { createAsync } from "./createAsync";
|
|
2
|
-
export { action, useSubmission, useSubmissions } from "./action";
|
|
2
|
+
export { action, useSubmission, useSubmissions, useAction } from "./action";
|
|
3
3
|
export { cache, revalidate } from "./cache";
|
|
4
|
+
export { redirect } from "./response";
|
package/dist/data/index.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
export { createAsync } from "./createAsync";
|
|
2
|
-
export { action, useSubmission, useSubmissions } from "./action";
|
|
2
|
+
export { action, useSubmission, useSubmissions, useAction } from "./action";
|
|
3
3
|
export { cache, revalidate } from "./cache";
|
|
4
|
+
export { redirect } from "./response";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function redirect(url: string, init?: number | ResponseInit): Response;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export function redirect(url, init = 302) {
|
|
2
|
+
let responseInit = init;
|
|
3
|
+
if (typeof responseInit === "number") {
|
|
4
|
+
responseInit = { status: responseInit };
|
|
5
|
+
}
|
|
6
|
+
else if (typeof responseInit.status === "undefined") {
|
|
7
|
+
responseInit.status = 302;
|
|
8
|
+
}
|
|
9
|
+
const headers = new Headers(responseInit.headers);
|
|
10
|
+
headers.set("Location", url);
|
|
11
|
+
const response = new Response(null, {
|
|
12
|
+
...responseInit,
|
|
13
|
+
headers: headers
|
|
14
|
+
});
|
|
15
|
+
return response;
|
|
16
|
+
}
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { isServer, delegateEvents, getRequestEvent, createComponent as createComponent$1, spread, mergeProps as mergeProps$1, template } from 'solid-js/web';
|
|
2
|
-
import { createSignal, onCleanup, getOwner, runWithOwner, createMemo, createContext, useContext, untrack, createRenderEffect, on, startTransition,
|
|
2
|
+
import { createSignal, onCleanup, getOwner, runWithOwner, createMemo, createContext, useContext, untrack, createRenderEffect, on, startTransition, resetErrorBoundaries, createComponent, children, createRoot, Show, mergeProps, splitProps, createResource, sharedConfig, $TRACK } from 'solid-js';
|
|
3
3
|
import { createStore, reconcile } from 'solid-js/store';
|
|
4
4
|
|
|
5
5
|
function bindEvent(target, type, handler) {
|
|
@@ -570,6 +570,16 @@ function createRouterContext(integration, getBranches, base = "") {
|
|
|
570
570
|
return resolvePath(basePath, to);
|
|
571
571
|
}
|
|
572
572
|
};
|
|
573
|
+
const router = {
|
|
574
|
+
base: baseRoute,
|
|
575
|
+
location,
|
|
576
|
+
isRouting,
|
|
577
|
+
renderPath,
|
|
578
|
+
parsePath,
|
|
579
|
+
navigatorFactory,
|
|
580
|
+
beforeLeave,
|
|
581
|
+
submissions: createSignal(submissions)
|
|
582
|
+
};
|
|
573
583
|
function navigateFromRoute(route, to, options) {
|
|
574
584
|
// Untrack in case someone navigates in an effect - don't want to track `reference` or route paths
|
|
575
585
|
untrack(() => {
|
|
@@ -602,7 +612,12 @@ function createRouterContext(integration, getBranches, base = "") {
|
|
|
602
612
|
if (resolvedTo !== current || nextState !== state()) {
|
|
603
613
|
if (isServer) {
|
|
604
614
|
const e = getRequestEvent();
|
|
605
|
-
e && (e.response = Response
|
|
615
|
+
e && (e.response = new Response(null, {
|
|
616
|
+
status: 302,
|
|
617
|
+
headers: {
|
|
618
|
+
Location: resolvedTo
|
|
619
|
+
}
|
|
620
|
+
}));
|
|
606
621
|
setSource({
|
|
607
622
|
value: resolvedTo,
|
|
608
623
|
replace,
|
|
@@ -758,7 +773,7 @@ function createRouterContext(integration, getBranches, base = "") {
|
|
|
758
773
|
let actionRef = evt.submitter && evt.submitter.getAttribute("formaction") || evt.target.action;
|
|
759
774
|
if (actionRef && actionRef.startsWith("action:")) {
|
|
760
775
|
const data = new FormData(evt.target);
|
|
761
|
-
actions.get(actionRef.
|
|
776
|
+
actions.get(actionRef).call(router, data);
|
|
762
777
|
evt.preventDefault();
|
|
763
778
|
}
|
|
764
779
|
}
|
|
@@ -794,16 +809,7 @@ function createRouterContext(integration, getBranches, base = "") {
|
|
|
794
809
|
}
|
|
795
810
|
submissions = initFromFlash(location.query);
|
|
796
811
|
}
|
|
797
|
-
return
|
|
798
|
-
base: baseRoute,
|
|
799
|
-
location,
|
|
800
|
-
isRouting,
|
|
801
|
-
renderPath,
|
|
802
|
-
parsePath,
|
|
803
|
-
navigatorFactory,
|
|
804
|
-
beforeLeave,
|
|
805
|
-
submissions: createSignal(submissions)
|
|
806
|
-
};
|
|
812
|
+
return router;
|
|
807
813
|
}
|
|
808
814
|
function createRouteContext(router, parent, outlet, match, params) {
|
|
809
815
|
const {
|
|
@@ -841,7 +847,7 @@ function createRouteContext(router, parent, outlet, match, params) {
|
|
|
841
847
|
return route;
|
|
842
848
|
}
|
|
843
849
|
|
|
844
|
-
const _tmpl$ = /*#__PURE__*/template(`<a
|
|
850
|
+
const _tmpl$ = /*#__PURE__*/template(`<a>`);
|
|
845
851
|
const Router = props => {
|
|
846
852
|
let e;
|
|
847
853
|
const {
|
|
@@ -850,7 +856,7 @@ const Router = props => {
|
|
|
850
856
|
base
|
|
851
857
|
} = props;
|
|
852
858
|
const integration = source || (isServer ? staticIntegration({
|
|
853
|
-
value: url || (e = getRequestEvent()) && e.request.url || ""
|
|
859
|
+
value: url || (e = getRequestEvent()) && getPath(e.request.url) || ""
|
|
854
860
|
}) : pathIntegration());
|
|
855
861
|
const routeDefs = children(() => props.children);
|
|
856
862
|
const branches = createMemo(() => createBranches(props.root ? {
|
|
@@ -870,6 +876,10 @@ const Router = props => {
|
|
|
870
876
|
}
|
|
871
877
|
});
|
|
872
878
|
};
|
|
879
|
+
function getPath(url) {
|
|
880
|
+
const u = new URL(url);
|
|
881
|
+
return u.pathname + u.search;
|
|
882
|
+
}
|
|
873
883
|
function Routes(props) {
|
|
874
884
|
const matches = createMemo(() => getRouteMatches(props.branches, props.routerState.location.pathname));
|
|
875
885
|
const params = createMemoObject(() => {
|
|
@@ -960,7 +970,7 @@ function A(props) {
|
|
|
960
970
|
return props.end ? path === loc : loc.startsWith(path);
|
|
961
971
|
});
|
|
962
972
|
return (() => {
|
|
963
|
-
const _el$ = _tmpl
|
|
973
|
+
const _el$ = _tmpl$();
|
|
964
974
|
spread(_el$, mergeProps$1(rest, {
|
|
965
975
|
get href() {
|
|
966
976
|
return href() || props.href;
|
|
@@ -1006,8 +1016,8 @@ function Navigate(props) {
|
|
|
1006
1016
|
/**
|
|
1007
1017
|
* This is mock of the eventual Solid 2.0 primitive. It is not fully featured.
|
|
1008
1018
|
*/
|
|
1009
|
-
function createAsync(fn) {
|
|
1010
|
-
const [resource] = createResource(() => subFetch(fn), v => v);
|
|
1019
|
+
function createAsync(fn, options) {
|
|
1020
|
+
const [resource] = createResource(() => subFetch(fn), v => v, options);
|
|
1011
1021
|
return () => resource();
|
|
1012
1022
|
}
|
|
1013
1023
|
|
|
@@ -1102,10 +1112,9 @@ function cache(fn, name, options) {
|
|
|
1102
1112
|
version && cached[3].add(version);
|
|
1103
1113
|
if (cached[2] === "preload" && intent !== "preload") {
|
|
1104
1114
|
cached[0] = now;
|
|
1105
|
-
cached[1] = "then" in cached[1] ? cached[1].then(handleResponse) : handleResponse(cached[1]);
|
|
1106
|
-
cached[2] = intent;
|
|
1107
1115
|
}
|
|
1108
1116
|
if (!isServer && intent === "navigate") {
|
|
1117
|
+
"then" in cached[1] ? cached[1].then(handleResponse(false), handleResponse(true)) : handleResponse(false)(cached[1]);
|
|
1109
1118
|
startTransition(() => revalidateSignals(cached[3], cached[0])); // update version
|
|
1110
1119
|
}
|
|
1111
1120
|
|
|
@@ -1119,7 +1128,7 @@ function cache(fn, name, options) {
|
|
|
1119
1128
|
sharedConfig.context && sharedConfig.context.serialize(key, res);
|
|
1120
1129
|
}
|
|
1121
1130
|
if (intent !== "preload") {
|
|
1122
|
-
|
|
1131
|
+
"then" in res ? res.then(handleResponse(false), handleResponse(true)) : handleResponse(false)(res);
|
|
1123
1132
|
}
|
|
1124
1133
|
if (cached) {
|
|
1125
1134
|
cached[0] = now;
|
|
@@ -1131,26 +1140,28 @@ function cache(fn, name, options) {
|
|
|
1131
1140
|
}
|
|
1132
1141
|
} else cache.set(key, cached = [now, res, intent, new Set(version ? [version] : [])]);
|
|
1133
1142
|
return res;
|
|
1134
|
-
function
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
+
function handleResponse(error) {
|
|
1144
|
+
return v => {
|
|
1145
|
+
if (v instanceof Response && redirectStatusCodes.has(v.status)) {
|
|
1146
|
+
if (navigate) {
|
|
1147
|
+
startTransition(() => {
|
|
1148
|
+
let url = v.headers.get(LocationHeader);
|
|
1149
|
+
if (url && url.startsWith("/")) {
|
|
1150
|
+
navigate(url, {
|
|
1151
|
+
replace: true
|
|
1152
|
+
});
|
|
1153
|
+
} else if (!isServer && url) {
|
|
1154
|
+
window.location.href = url;
|
|
1155
|
+
}
|
|
1156
|
+
});
|
|
1157
|
+
}
|
|
1158
|
+
return;
|
|
1143
1159
|
}
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
return;
|
|
1150
|
-
}
|
|
1151
|
-
if (isServer) return v;
|
|
1152
|
-
setStore(key, reconcile(v, options));
|
|
1153
|
-
return store[key];
|
|
1160
|
+
if (error) throw error;
|
|
1161
|
+
if (isServer) return v;
|
|
1162
|
+
setStore(key, reconcile(v, options));
|
|
1163
|
+
return store[key];
|
|
1164
|
+
};
|
|
1154
1165
|
}
|
|
1155
1166
|
};
|
|
1156
1167
|
}
|
|
@@ -1189,12 +1200,23 @@ function useSubmission(fn, filter) {
|
|
|
1189
1200
|
}
|
|
1190
1201
|
};
|
|
1191
1202
|
}
|
|
1203
|
+
function useAction(action) {
|
|
1204
|
+
const router = useRouter();
|
|
1205
|
+
return action.bind(router);
|
|
1206
|
+
}
|
|
1192
1207
|
function action(fn, name) {
|
|
1193
1208
|
function mutate(variables) {
|
|
1194
1209
|
const p = fn(variables);
|
|
1195
1210
|
const [result, setResult] = createSignal();
|
|
1196
1211
|
let submission;
|
|
1197
1212
|
const router = this;
|
|
1213
|
+
async function handler(res) {
|
|
1214
|
+
const data = await handleResponse(res, router.navigatorFactory());
|
|
1215
|
+
data ? setResult({
|
|
1216
|
+
data
|
|
1217
|
+
}) : submission.clear();
|
|
1218
|
+
return data;
|
|
1219
|
+
}
|
|
1198
1220
|
router.submissions[1](s => [...s, submission = {
|
|
1199
1221
|
input: variables,
|
|
1200
1222
|
url,
|
|
@@ -1210,36 +1232,14 @@ function action(fn, name) {
|
|
|
1210
1232
|
retry() {
|
|
1211
1233
|
setResult(undefined);
|
|
1212
1234
|
const p = fn(variables);
|
|
1213
|
-
p.then(
|
|
1214
|
-
const keys = handleResponse(data, router.navigatorFactory());
|
|
1215
|
-
await revalidate(keys);
|
|
1216
|
-
data ? setResult({
|
|
1217
|
-
data
|
|
1218
|
-
}) : submission.clear();
|
|
1219
|
-
return data;
|
|
1220
|
-
}).catch(error => {
|
|
1221
|
-
setResult({
|
|
1222
|
-
data: error
|
|
1223
|
-
});
|
|
1224
|
-
});
|
|
1235
|
+
p.then(handler, handler);
|
|
1225
1236
|
return p;
|
|
1226
1237
|
}
|
|
1227
1238
|
}]);
|
|
1228
|
-
p.then(
|
|
1229
|
-
const keys = handleResponse(data, router.navigatorFactory());
|
|
1230
|
-
await revalidate(keys);
|
|
1231
|
-
data ? setResult({
|
|
1232
|
-
data
|
|
1233
|
-
}) : submission.clear();
|
|
1234
|
-
return data;
|
|
1235
|
-
}).catch(error => {
|
|
1236
|
-
setResult({
|
|
1237
|
-
data: error
|
|
1238
|
-
});
|
|
1239
|
-
});
|
|
1239
|
+
p.then(handler, handler);
|
|
1240
1240
|
return p;
|
|
1241
1241
|
}
|
|
1242
|
-
const url = fn.url || `action:${name}` || !isServer ? `action:${fn.name}` : "";
|
|
1242
|
+
const url = fn.url || name && `action:${name}` || (!isServer ? `action:${fn.name}` : "");
|
|
1243
1243
|
mutate.toString = () => {
|
|
1244
1244
|
if (!url) throw new Error("Client Actions need explicit names if server rendered");
|
|
1245
1245
|
return url;
|
|
@@ -1247,7 +1247,8 @@ function action(fn, name) {
|
|
|
1247
1247
|
if (!isServer) registerAction(url, mutate);
|
|
1248
1248
|
return mutate;
|
|
1249
1249
|
}
|
|
1250
|
-
function handleResponse(response, navigate) {
|
|
1250
|
+
async function handleResponse(response, navigate) {
|
|
1251
|
+
let data;
|
|
1251
1252
|
if (response instanceof Response && redirectStatusCodes.has(response.status)) {
|
|
1252
1253
|
const locationUrl = response.headers.get("Location") || "/";
|
|
1253
1254
|
if (locationUrl.startsWith("http")) {
|
|
@@ -1255,9 +1256,28 @@ function handleResponse(response, navigate) {
|
|
|
1255
1256
|
} else {
|
|
1256
1257
|
navigate(locationUrl);
|
|
1257
1258
|
}
|
|
1259
|
+
} else data = response;
|
|
1260
|
+
// TODO: handle keys
|
|
1261
|
+
await revalidate();
|
|
1262
|
+
return data;
|
|
1263
|
+
}
|
|
1264
|
+
|
|
1265
|
+
function redirect(url, init = 302) {
|
|
1266
|
+
let responseInit = init;
|
|
1267
|
+
if (typeof responseInit === "number") {
|
|
1268
|
+
responseInit = {
|
|
1269
|
+
status: responseInit
|
|
1270
|
+
};
|
|
1271
|
+
} else if (typeof responseInit.status === "undefined") {
|
|
1272
|
+
responseInit.status = 302;
|
|
1258
1273
|
}
|
|
1259
|
-
|
|
1260
|
-
|
|
1274
|
+
const headers = new Headers(responseInit.headers);
|
|
1275
|
+
headers.set("Location", url);
|
|
1276
|
+
const response = new Response(null, {
|
|
1277
|
+
...responseInit,
|
|
1278
|
+
headers: headers
|
|
1279
|
+
});
|
|
1280
|
+
return response;
|
|
1261
1281
|
}
|
|
1262
1282
|
|
|
1263
|
-
export { A, A as Link, A as NavLink, Navigate, Route, Router, mergeSearchString as _mergeSearchString, action, cache, createAsync, createBeforeLeave, createIntegration, createMemoryHistory, hashIntegration, memoryIntegration, normalizeIntegration, pathIntegration, revalidate, staticIntegration, useBeforeLeave, useHref, useIsRouting, useLocation, useMatch, useNavigate, useParams, useResolvedPath, useSearchParams, useSubmission, useSubmissions };
|
|
1283
|
+
export { A, A as Link, A as NavLink, Navigate, Route, Router, mergeSearchString as _mergeSearchString, action, cache, createAsync, createBeforeLeave, createIntegration, createMemoryHistory, hashIntegration, memoryIntegration, normalizeIntegration, pathIntegration, redirect, revalidate, staticIntegration, useAction, useBeforeLeave, useHref, useIsRouting, useLocation, useMatch, useNavigate, useParams, useResolvedPath, useSearchParams, useSubmission, useSubmissions };
|
package/dist/routing.js
CHANGED
|
@@ -217,6 +217,16 @@ export function createRouterContext(integration, getBranches, base = "") {
|
|
|
217
217
|
return resolvePath(basePath, to);
|
|
218
218
|
}
|
|
219
219
|
};
|
|
220
|
+
const router = {
|
|
221
|
+
base: baseRoute,
|
|
222
|
+
location,
|
|
223
|
+
isRouting,
|
|
224
|
+
renderPath,
|
|
225
|
+
parsePath,
|
|
226
|
+
navigatorFactory,
|
|
227
|
+
beforeLeave,
|
|
228
|
+
submissions: createSignal(submissions)
|
|
229
|
+
};
|
|
220
230
|
function navigateFromRoute(route, to, options) {
|
|
221
231
|
// Untrack in case someone navigates in an effect - don't want to track `reference` or route paths
|
|
222
232
|
untrack(() => {
|
|
@@ -249,7 +259,8 @@ export function createRouterContext(integration, getBranches, base = "") {
|
|
|
249
259
|
if (resolvedTo !== current || nextState !== state()) {
|
|
250
260
|
if (isServer) {
|
|
251
261
|
const e = getRequestEvent();
|
|
252
|
-
e &&
|
|
262
|
+
e &&
|
|
263
|
+
(e.response = new Response(null, { status: 302, headers: { Location: resolvedTo } }));
|
|
253
264
|
setSource({ value: resolvedTo, replace, scroll, state: nextState });
|
|
254
265
|
}
|
|
255
266
|
else if (beforeLeave.confirm(resolvedTo, options)) {
|
|
@@ -413,7 +424,7 @@ export function createRouterContext(integration, getBranches, base = "") {
|
|
|
413
424
|
let actionRef = (evt.submitter && evt.submitter.getAttribute("formaction")) || evt.target.action;
|
|
414
425
|
if (actionRef && actionRef.startsWith("action:")) {
|
|
415
426
|
const data = new FormData(evt.target);
|
|
416
|
-
actions.get(actionRef.
|
|
427
|
+
actions.get(actionRef).call(router, data);
|
|
417
428
|
evt.preventDefault();
|
|
418
429
|
}
|
|
419
430
|
}
|
|
@@ -451,16 +462,7 @@ export function createRouterContext(integration, getBranches, base = "") {
|
|
|
451
462
|
}
|
|
452
463
|
submissions = initFromFlash(location.query);
|
|
453
464
|
}
|
|
454
|
-
return
|
|
455
|
-
base: baseRoute,
|
|
456
|
-
location,
|
|
457
|
-
isRouting,
|
|
458
|
-
renderPath,
|
|
459
|
-
parsePath,
|
|
460
|
-
navigatorFactory,
|
|
461
|
-
beforeLeave,
|
|
462
|
-
submissions: createSignal(submissions)
|
|
463
|
-
};
|
|
465
|
+
return router;
|
|
464
466
|
}
|
|
465
467
|
export function createRouteContext(router, parent, outlet, match, params) {
|
|
466
468
|
const { base, location } = router;
|
package/package.json
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
"Ryan Turnquist"
|
|
7
7
|
],
|
|
8
8
|
"license": "MIT",
|
|
9
|
-
"version": "0.10.0-beta.
|
|
9
|
+
"version": "0.10.0-beta.3",
|
|
10
10
|
"homepage": "https://github.com/solidjs/solid-router#readme",
|
|
11
11
|
"repository": {
|
|
12
12
|
"type": "git",
|
|
@@ -37,17 +37,17 @@
|
|
|
37
37
|
"@types/jest": "^29.0.0",
|
|
38
38
|
"@types/node": "^20.9.0",
|
|
39
39
|
"babel-jest": "^29.0.1",
|
|
40
|
-
"babel-preset-solid": "^1.
|
|
40
|
+
"babel-preset-solid": "^1.8.6",
|
|
41
41
|
"jest": "^29.0.1",
|
|
42
42
|
"jest-environment-jsdom": "^29.2.1",
|
|
43
43
|
"prettier": "^2.7.1",
|
|
44
44
|
"rollup": "^3.7.5",
|
|
45
45
|
"solid-jest": "^0.2.0",
|
|
46
|
-
"solid-js": "^1.8.
|
|
46
|
+
"solid-js": "^1.8.6",
|
|
47
47
|
"typescript": "^5.2.2"
|
|
48
48
|
},
|
|
49
49
|
"peerDependencies": {
|
|
50
|
-
"solid-js": "^1.8.
|
|
50
|
+
"solid-js": "^1.8.6"
|
|
51
51
|
},
|
|
52
52
|
"jest": {
|
|
53
53
|
"preset": "solid-jest/preset/browser"
|