@solidjs/router 1.0.0 → 2.0.0-next.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +606 -697
- package/dist/claims.d.ts +21 -0
- package/dist/claims.js +115 -0
- package/dist/data/action.d.ts +40 -11
- package/dist/data/action.js +303 -101
- package/dist/data/events.d.ts +8 -0
- package/dist/data/events.js +24 -23
- package/dist/data/index.d.ts +2 -4
- package/dist/data/index.js +2 -4
- package/dist/data/query.d.ts +1 -3
- package/dist/data/query.js +39 -22
- package/dist/data/serverForms.d.ts +1 -0
- package/dist/data/serverForms.js +5 -0
- package/dist/fs.d.ts +94 -0
- package/dist/fs.js +51 -0
- package/dist/index.d.ts +24 -3
- package/dist/index.js +1890 -1200
- package/dist/index.jsx +2 -2
- package/dist/lifecycle.d.ts +29 -4
- package/dist/lifecycle.js +40 -37
- package/dist/paths.d.ts +117 -0
- package/dist/paths.js +41 -0
- package/dist/routers/components.d.ts +12 -26
- package/dist/routers/components.jsx +68 -54
- package/dist/routers/factory.d.ts +121 -0
- package/dist/routers/factory.jsx +154 -0
- package/dist/routers/history.d.ts +26 -0
- package/dist/routers/history.js +180 -0
- package/dist/routers/index.d.ts +5 -11
- package/dist/routers/index.js +2 -6
- package/dist/routers/scrollRestoration.d.ts +10 -1
- package/dist/routers/scrollRestoration.js +49 -11
- package/dist/routing.d.ts +96 -52
- package/dist/routing.js +399 -167
- package/dist/server.d.ts +47 -0
- package/dist/server.js +156 -0
- package/dist/types.d.ts +148 -44
- package/dist/utils.d.ts +2 -0
- package/dist/utils.js +2 -0
- package/package.json +12 -7
- package/dist/components.d.ts +0 -31
- package/dist/components.jsx +0 -40
- package/dist/data/createAsync.d.ts +0 -32
- package/dist/data/createAsync.js +0 -96
- package/dist/data/response.d.ts +0 -4
- package/dist/data/response.js +0 -42
- package/dist/routers/HashRouter.d.ts +0 -9
- package/dist/routers/HashRouter.js +0 -41
- package/dist/routers/MemoryRouter.d.ts +0 -24
- package/dist/routers/MemoryRouter.js +0 -57
- package/dist/routers/Router.d.ts +0 -17
- package/dist/routers/Router.js +0 -59
- package/dist/routers/StaticRouter.d.ts +0 -6
- package/dist/routers/StaticRouter.js +0 -15
- package/dist/routers/createRouter.d.ts +0 -10
- package/dist/routers/createRouter.js +0 -41
package/dist/claims.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { RouterContext } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* The compiler claims every `a[href]` (and `form[action]`, which this handler
|
|
4
|
+
* ignores) at creation, and the runtime re-claims on `href` writes. This
|
|
5
|
+
* consumer gives each router-managed anchor the link-state vocabulary without
|
|
6
|
+
* a wrapper component:
|
|
7
|
+
*
|
|
8
|
+
* - `aria-current="page"` — the location matches the link exactly
|
|
9
|
+
* - `data-active` — exact or prefix match
|
|
10
|
+
* - `data-pending` — the link is the target of an in-flight navigation
|
|
11
|
+
*
|
|
12
|
+
* Elements are claimed at creation, so late mounts (`<Show>`, `<For>`,
|
|
13
|
+
* portals) are correct immediately. One render effect (owned by the router)
|
|
14
|
+
* subscribes to the location and sweeps a registry of claimed anchors —
|
|
15
|
+
* anchors themselves carry no reactive machinery, just a registry entry
|
|
16
|
+
* removed by their creating owner's cleanup. State is applied once at claim
|
|
17
|
+
* so it is correct before the next navigation; re-claims (an `href` write)
|
|
18
|
+
* are the same one-shot untracked refresh, reading the element's current
|
|
19
|
+
* `href` from the DOM.
|
|
20
|
+
*/
|
|
21
|
+
export declare function setupLinkClaims(router: RouterContext, explicitLinks?: boolean): void;
|
package/dist/claims.js
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { registerElementClaim } from "@solidjs/web";
|
|
2
|
+
import { createRenderEffect, getOwner, onCleanup, untrack } from "solid-js";
|
|
3
|
+
import { comparablePath } from "./utils.js";
|
|
4
|
+
/**
|
|
5
|
+
* The compiler claims every `a[href]` (and `form[action]`, which this handler
|
|
6
|
+
* ignores) at creation, and the runtime re-claims on `href` writes. This
|
|
7
|
+
* consumer gives each router-managed anchor the link-state vocabulary without
|
|
8
|
+
* a wrapper component:
|
|
9
|
+
*
|
|
10
|
+
* - `aria-current="page"` — the location matches the link exactly
|
|
11
|
+
* - `data-active` — exact or prefix match
|
|
12
|
+
* - `data-pending` — the link is the target of an in-flight navigation
|
|
13
|
+
*
|
|
14
|
+
* Elements are claimed at creation, so late mounts (`<Show>`, `<For>`,
|
|
15
|
+
* portals) are correct immediately. One render effect (owned by the router)
|
|
16
|
+
* subscribes to the location and sweeps a registry of claimed anchors —
|
|
17
|
+
* anchors themselves carry no reactive machinery, just a registry entry
|
|
18
|
+
* removed by their creating owner's cleanup. State is applied once at claim
|
|
19
|
+
* so it is correct before the next navigation; re-claims (an `href` write)
|
|
20
|
+
* are the same one-shot untracked refresh, reading the element's current
|
|
21
|
+
* `href` from the DOM.
|
|
22
|
+
*/
|
|
23
|
+
export function setupLinkClaims(router, explicitLinks) {
|
|
24
|
+
const basePath = router.base.path();
|
|
25
|
+
// per-element record; `current` remembers whether we set `aria-current`,
|
|
26
|
+
// so user-authored values (steppers, breadcrumbs) are never stripped
|
|
27
|
+
const claimed = new WeakMap();
|
|
28
|
+
const registry = new Set();
|
|
29
|
+
function isSvg(el) {
|
|
30
|
+
return el.namespaceURI === "http://www.w3.org/2000/svg";
|
|
31
|
+
}
|
|
32
|
+
/** The comparable pathname when the router manages this anchor, else `undefined`. */
|
|
33
|
+
function managedPath(a) {
|
|
34
|
+
if (explicitLinks && !a.hasAttribute("link"))
|
|
35
|
+
return;
|
|
36
|
+
const svg = isSvg(a);
|
|
37
|
+
// claims fire at creation while the element is still in the template's
|
|
38
|
+
// inert fragment, where the `href` property is not resolved — resolve the
|
|
39
|
+
// raw attribute against the live document instead
|
|
40
|
+
const href = svg ? a.href.baseVal : a.getAttribute("href");
|
|
41
|
+
const target = svg ? a.target.baseVal : a.target;
|
|
42
|
+
if (target || !href)
|
|
43
|
+
return;
|
|
44
|
+
const rel = (a.getAttribute("rel") || "").split(/\s+/);
|
|
45
|
+
if (a.hasAttribute("download") || rel.includes("external"))
|
|
46
|
+
return;
|
|
47
|
+
let url;
|
|
48
|
+
try {
|
|
49
|
+
url = new URL(href, document.baseURI);
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
if (url.origin !== window.location.origin ||
|
|
55
|
+
(basePath && url.pathname && !url.pathname.toLowerCase().startsWith(basePath.toLowerCase())))
|
|
56
|
+
return;
|
|
57
|
+
return comparablePath(url.pathname);
|
|
58
|
+
}
|
|
59
|
+
function linkState(a) {
|
|
60
|
+
// read reactive sources unconditionally so the owning effect stays
|
|
61
|
+
// subscribed even while the anchor is not router-managed
|
|
62
|
+
const loc = decodeURI(comparablePath(router.location.pathname));
|
|
63
|
+
const routing = router.isRouting();
|
|
64
|
+
const path = managedPath(a);
|
|
65
|
+
// the root path is a prefix of everything, so it only matches exactly —
|
|
66
|
+
// there is no per-anchor `end` opt-out like useLinkState has
|
|
67
|
+
const matches = (target) => path !== undefined && (target === path || (path !== "" && target.startsWith(path + "/")));
|
|
68
|
+
// effects observe the committed location during a transition, so the
|
|
69
|
+
// in-flight target comes from pendingTarget — readable here because the
|
|
70
|
+
// isRouting write flushes after the target is assigned
|
|
71
|
+
const pending = routing && !!router.pendingTarget && matches(decodeURI(comparablePath(router.pendingTarget.value)));
|
|
72
|
+
return { active: matches(loc), pending, exact: path !== undefined && loc === path };
|
|
73
|
+
}
|
|
74
|
+
function apply(a, rec, { active, pending, exact }) {
|
|
75
|
+
active ? a.setAttribute("data-active", "") : a.removeAttribute("data-active");
|
|
76
|
+
pending ? a.setAttribute("data-pending", "") : a.removeAttribute("data-pending");
|
|
77
|
+
if (exact !== rec.current) {
|
|
78
|
+
exact ? a.setAttribute("aria-current", "page") : a.removeAttribute("aria-current");
|
|
79
|
+
rec.current = exact;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
const refresh = (a, rec) => untrack(() => apply(a, rec, linkState(a)));
|
|
83
|
+
// The one subscription for every anchor: compute tracks the sources
|
|
84
|
+
// linkState derives from (the in-flight pendingTarget is readable in the
|
|
85
|
+
// effect phase because the isRouting write flushes after the target is
|
|
86
|
+
// assigned), the effect phase sweeps the registry untracked.
|
|
87
|
+
//
|
|
88
|
+
// `transparent` keeps the effect invisible to the hydration id scheme.
|
|
89
|
+
// This setup is client-only, so an id-consuming node here has no server
|
|
90
|
+
// counterpart and every subsequent hydration id would shift by one child
|
|
91
|
+
// slot — lazy-route lookups miss and hydration leaves server nodes
|
|
92
|
+
// unclaimed. (The option is honored by the runtime but missing from the
|
|
93
|
+
// published EffectOptions type, hence the cast.)
|
|
94
|
+
createRenderEffect(() => (router.location.pathname, router.isRouting()), () => registry.forEach(a => refresh(a, claimed.get(a))), { transparent: true });
|
|
95
|
+
onCleanup(registerElementClaim(node => {
|
|
96
|
+
if (node.nodeName.toUpperCase() !== "A")
|
|
97
|
+
return;
|
|
98
|
+
const a = node;
|
|
99
|
+
// re-claim (href changed): the claiming write runs inside another
|
|
100
|
+
// effect, so refresh without leaking subscriptions into it
|
|
101
|
+
const existing = claimed.get(a);
|
|
102
|
+
if (existing)
|
|
103
|
+
return refresh(a, existing);
|
|
104
|
+
const rec = { current: false };
|
|
105
|
+
claimed.set(a, rec);
|
|
106
|
+
// claims fire during component setup, so an owner is present in
|
|
107
|
+
// practice to bound the registry entry's lifetime; without one, state
|
|
108
|
+
// is still applied once at creation
|
|
109
|
+
if (getOwner()) {
|
|
110
|
+
registry.add(a);
|
|
111
|
+
onCleanup(() => registry.delete(a));
|
|
112
|
+
}
|
|
113
|
+
refresh(a, rec);
|
|
114
|
+
}));
|
|
115
|
+
}
|
package/dist/data/action.d.ts
CHANGED
|
@@ -1,17 +1,46 @@
|
|
|
1
|
-
import { JSX } from "
|
|
2
|
-
import type {
|
|
1
|
+
import { type JSX } from "@solidjs/web";
|
|
2
|
+
import type { RouterContext, Submission, NarrowResponse } from "../types.js";
|
|
3
3
|
export type Action<T extends Array<any>, U, V = T> = (T extends [FormData | URLSearchParams] | [] ? JSX.SerializableAttributeValue : unknown) & ((...vars: T) => Promise<NarrowResponse<U>>) & {
|
|
4
4
|
url: string;
|
|
5
5
|
with<A extends any[], B extends any[]>(this: (this: any, ...args: [...A, ...B]) => Promise<NarrowResponse<U>>, ...args: A): Action<B, U, V>;
|
|
6
|
+
onSubmit(hook: (...args: V extends Array<any> ? V : T) => void): Action<T, U, V>;
|
|
7
|
+
onSettled(hook: (submission: Submission<V extends Array<any> ? V : T, NarrowResponse<U>>) => void): Action<T, U, V>;
|
|
6
8
|
};
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
9
|
+
type ActionFactory = {
|
|
10
|
+
<T extends Array<any>, U = void>(fn: (...args: T) => Promise<U>, name?: string): Action<T, U>;
|
|
11
|
+
<T extends Array<any>, U = void>(fn: (...args: T) => Promise<U>, options?: {
|
|
12
|
+
name?: string;
|
|
13
|
+
}): Action<T, U>;
|
|
10
14
|
};
|
|
11
|
-
export declare
|
|
15
|
+
export declare const actions: Map<string, Action<any, any, any>>;
|
|
16
|
+
/**
|
|
17
|
+
* The document-delegation submit handler for router actions. Lives here —
|
|
18
|
+
* not in events.ts — so the router's event wiring holds no static reference
|
|
19
|
+
* to the action module; `installRouterIntegrations` slots it in when the
|
|
20
|
+
* first action is created on the client.
|
|
21
|
+
*/
|
|
22
|
+
export declare function handleFormAction(evt: SubmitEvent, router: RouterContext, actionBase: string): void;
|
|
23
|
+
/**
|
|
24
|
+
* Entry point for delegation's lazy fallback (data/events.ts): when no form
|
|
25
|
+
* handler was ever installed — no action module in the client graph at all —
|
|
26
|
+
* the router intercepts posts to server-action urls synchronously and loads
|
|
27
|
+
* this module to run them. The FormData was captured at submit time; only
|
|
28
|
+
* the enctype conversion and the generic invocation happen here.
|
|
29
|
+
*/
|
|
30
|
+
export declare function submitServerForm(router: RouterContext, url: string, form: HTMLFormElement, data: FormData): void;
|
|
31
|
+
export declare function useSubmissions<T extends Array<any>, U, V>(fn: Action<T, U, V>, filter?: (input: V) => boolean): Submission<V, NarrowResponse<U>>[];
|
|
12
32
|
export declare function useAction<T extends Array<any>, U, V>(action: Action<T, U, V>): (...args: Parameters<Action<T, U, V>>) => Promise<NarrowResponse<U>>;
|
|
13
|
-
export declare
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
33
|
+
export declare const action: ActionFactory;
|
|
34
|
+
/**
|
|
35
|
+
* Registers the router as the single-flight consumer of the server function
|
|
36
|
+
* transport. Subscribing is the opt-in: while registered, the transport
|
|
37
|
+
* sends the `X-Single-Flight` request header on mutations and delivers the
|
|
38
|
+
* folded payload here — fresh route data is seeded into the `query` cache
|
|
39
|
+
* and the envelope metadata (redirect `Location`, `X-Revalidate` keys) is
|
|
40
|
+
* applied, all before the action sees its plain return value. Called by the
|
|
41
|
+
* Router component on the client unless `singleFlight={false}`, which now
|
|
42
|
+
* simply means "never subscribe" — no consumer, no request header, no
|
|
43
|
+
* collection work on the server. Returns the unsubscribe function.
|
|
44
|
+
*/
|
|
45
|
+
export declare function setupFlightDataConsumer(router: RouterContext): () => void;
|
|
46
|
+
export {};
|
package/dist/data/action.js
CHANGED
|
@@ -1,9 +1,125 @@
|
|
|
1
|
-
import { $TRACK,
|
|
2
|
-
import { isServer } from "
|
|
3
|
-
import {
|
|
1
|
+
import { $TRACK, action as createSolidAction, createMemo, onCleanup, getOwner } from "solid-js";
|
|
2
|
+
import { isResponseEnvelope, isServer, REVALIDATE_HEADER } from "@solidjs/web";
|
|
3
|
+
import { createServerReference, decodeResponsePayload, subscribeFlightData } from "@solidjs/web/server-functions";
|
|
4
|
+
// The explicit /server specifier is safe here: the only call site is
|
|
5
|
+
// server-guarded, so client builds tree-shake the codec away.
|
|
6
|
+
import { decodeFlashCookie } from "@solidjs/web/server-functions/server";
|
|
7
|
+
import { provideFlashDecoder, provideFlightConsumer, useRouter } from "../routing.js";
|
|
8
|
+
import { setRouterFormHandler } from "./events.js";
|
|
4
9
|
import { mockBase, setFunctionName } from "../utils.js";
|
|
5
10
|
import { cacheKeyOp, hashKey, revalidate, query } from "./query.js";
|
|
11
|
+
const submitHooksSymbol = Symbol("routerActionSubmitHooks");
|
|
12
|
+
const settledHooksSymbol = Symbol("routerActionSettledHooks");
|
|
13
|
+
const invokeSymbol = Symbol("routerActionInvoke");
|
|
14
|
+
// Forms submitted through delegation are marked `aria-busy` while their
|
|
15
|
+
// action is in flight — the form half of the attribute vocabulary links get
|
|
16
|
+
// (`data-active`/`data-pending`). Style with `form[aria-busy] button { ... }`.
|
|
17
|
+
// A counter (not a boolean) keeps the attribute through overlapping
|
|
18
|
+
// submissions from the same form.
|
|
19
|
+
const busyForms = /* #__PURE__ */ new WeakMap();
|
|
20
|
+
function setFormBusy(form, delta) {
|
|
21
|
+
const count = (busyForms.get(form) || 0) + delta;
|
|
22
|
+
busyForms.set(form, count);
|
|
23
|
+
count > 0 ? form.setAttribute("aria-busy", "true") : form.removeAttribute("aria-busy");
|
|
24
|
+
}
|
|
6
25
|
export const actions = /* #__PURE__ */ new Map();
|
|
26
|
+
/**
|
|
27
|
+
* The document-delegation submit handler for router actions. Lives here —
|
|
28
|
+
* not in events.ts — so the router's event wiring holds no static reference
|
|
29
|
+
* to the action module; `installRouterIntegrations` slots it in when the
|
|
30
|
+
* first action is created on the client.
|
|
31
|
+
*/
|
|
32
|
+
export function handleFormAction(evt, router, actionBase) {
|
|
33
|
+
if (evt.defaultPrevented)
|
|
34
|
+
return;
|
|
35
|
+
let actionRef = evt.submitter && evt.submitter.hasAttribute("formaction")
|
|
36
|
+
? evt.submitter.getAttribute("formaction")
|
|
37
|
+
: evt.target.getAttribute("action");
|
|
38
|
+
if (!actionRef)
|
|
39
|
+
return;
|
|
40
|
+
const serverAction = !actionRef.startsWith("https://action/");
|
|
41
|
+
if (serverAction) {
|
|
42
|
+
// normalize server actions
|
|
43
|
+
const url = new URL(actionRef, mockBase);
|
|
44
|
+
actionRef = router.parsePath(url.pathname + url.search);
|
|
45
|
+
if (!actionRef.startsWith(actionBase))
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
if (evt.target.method.toUpperCase() !== "POST")
|
|
49
|
+
throw new Error("Only POST forms are supported for Actions");
|
|
50
|
+
// A registry miss on a server-action url is a direct bind whose module
|
|
51
|
+
// never loaded client-side (server components): the url is self-describing
|
|
52
|
+
// (`?id`, bound `?args`), so a generic invocation is synthesized from it —
|
|
53
|
+
// delegation alone is sufficient, the no-JS path stays a no-JS fallback.
|
|
54
|
+
// Client-only actions (`https://action/`) are their module's JS by
|
|
55
|
+
// definition, so a miss there falls through to native submission.
|
|
56
|
+
const handler = actions.get(actionRef) || (serverAction && createServerFormAction(actionRef));
|
|
57
|
+
if (handler) {
|
|
58
|
+
evt.preventDefault();
|
|
59
|
+
const data = new FormData(evt.target, evt.submitter);
|
|
60
|
+
handler.call({ r: router, f: evt.target }, evt.target.enctype === "multipart/form-data"
|
|
61
|
+
? data
|
|
62
|
+
: new URLSearchParams(data));
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Synthesizes a router action for a server-rendered action url. The url
|
|
67
|
+
* carries everything an invocation needs — the function id and any bound
|
|
68
|
+
* `.with()` arguments (plain JSON in `?args`, which the server prepends for
|
|
69
|
+
* natural-encoding bodies exactly as it does for no-JS posts) — so the
|
|
70
|
+
* FormData is posted to it verbatim through the server-function transport:
|
|
71
|
+
* submissions, `aria-busy`, redirects, revalidation, and single-flight all
|
|
72
|
+
* flow through the normal action machinery. Registered under the url, so
|
|
73
|
+
* repeat submits reuse it (and a later real registration overrides it).
|
|
74
|
+
*/
|
|
75
|
+
function createServerFormAction(url) {
|
|
76
|
+
const id = new URL(url, mockBase).searchParams.get("id");
|
|
77
|
+
if (!id)
|
|
78
|
+
return undefined;
|
|
79
|
+
// typecheck resolves the server half of the dual module; this path only
|
|
80
|
+
// runs in the browser, where the client transport's signature applies
|
|
81
|
+
const stub = createServerReference(id, undefined, url);
|
|
82
|
+
const caller = Object.assign((form) => stub(form), { url });
|
|
83
|
+
return actionImpl(caller);
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Entry point for delegation's lazy fallback (data/events.ts): when no form
|
|
87
|
+
* handler was ever installed — no action module in the client graph at all —
|
|
88
|
+
* the router intercepts posts to server-action urls synchronously and loads
|
|
89
|
+
* this module to run them. The FormData was captured at submit time; only
|
|
90
|
+
* the enctype conversion and the generic invocation happen here.
|
|
91
|
+
*/
|
|
92
|
+
export function submitServerForm(router, url, form, data) {
|
|
93
|
+
const handler = actions.get(url) || createServerFormAction(url);
|
|
94
|
+
// no `?id` — not the server function convention; nothing can run it,
|
|
95
|
+
// resubmit natively (submit() bypasses the delegated handler)
|
|
96
|
+
if (!handler)
|
|
97
|
+
return form.submit();
|
|
98
|
+
handler.call({ r: router, f: form }, form.enctype === "multipart/form-data" ? data : new URLSearchParams(data));
|
|
99
|
+
}
|
|
100
|
+
// Wires the action layer into the router's slots exactly once, triggered by
|
|
101
|
+
// the first action creation. Not an import side effect — with
|
|
102
|
+
// `sideEffects: false`, module evaluation only happens when action() is
|
|
103
|
+
// actually used, which is precisely when the wiring is wanted: no action in
|
|
104
|
+
// the graph means no form interception, no single-flight subscription (the
|
|
105
|
+
// server is never asked to collect), and no flash cookies to decode. On the
|
|
106
|
+
// server, actions are created at module scope, so the flash decoder is
|
|
107
|
+
// always installed before useSubmission can read the submissions signal.
|
|
108
|
+
let integrationsInstalled = false;
|
|
109
|
+
function installRouterIntegrations() {
|
|
110
|
+
if (integrationsInstalled)
|
|
111
|
+
return;
|
|
112
|
+
integrationsInstalled = true;
|
|
113
|
+
if (isServer) {
|
|
114
|
+
// Server-only: initSubmissions only decodes during SSR, so client builds
|
|
115
|
+
// tree-shake the codec (which now lives behind the runtime's server entry).
|
|
116
|
+
provideFlashDecoder(decodeFlashCookie);
|
|
117
|
+
}
|
|
118
|
+
else {
|
|
119
|
+
setRouterFormHandler(handleFormAction);
|
|
120
|
+
provideFlightConsumer(setupFlightDataConsumer);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
7
123
|
export function useSubmissions(fn, filter) {
|
|
8
124
|
const router = useRouter();
|
|
9
125
|
const subs = createMemo(() => router.submissions[0]().filter(s => s.url === fn.base && (!filter || filter(s.input))));
|
|
@@ -11,8 +127,6 @@ export function useSubmissions(fn, filter) {
|
|
|
11
127
|
get(_, property) {
|
|
12
128
|
if (property === $TRACK)
|
|
13
129
|
return subs();
|
|
14
|
-
if (property === "pending")
|
|
15
|
-
return subs().some(sub => !sub.result);
|
|
16
130
|
return subs()[property];
|
|
17
131
|
},
|
|
18
132
|
has(_, property) {
|
|
@@ -20,130 +134,190 @@ export function useSubmissions(fn, filter) {
|
|
|
20
134
|
}
|
|
21
135
|
});
|
|
22
136
|
}
|
|
23
|
-
export function useSubmission(fn, filter) {
|
|
24
|
-
const submissions = useSubmissions(fn, filter);
|
|
25
|
-
return new Proxy({}, {
|
|
26
|
-
get(_, property) {
|
|
27
|
-
if (submissions.length === 0 && (property === "clear" || property === "retry"))
|
|
28
|
-
return () => { };
|
|
29
|
-
return submissions[submissions.length - 1]?.[property];
|
|
30
|
-
}
|
|
31
|
-
});
|
|
32
|
-
}
|
|
33
137
|
export function useAction(action) {
|
|
34
138
|
const r = useRouter();
|
|
35
139
|
return (...args) => action.apply({ r }, args);
|
|
36
140
|
}
|
|
37
|
-
|
|
38
|
-
function
|
|
141
|
+
function actionImpl(fn, options = {}) {
|
|
142
|
+
async function invoke(variables, current) {
|
|
39
143
|
const router = this.r;
|
|
40
144
|
const form = this.f;
|
|
41
|
-
const
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
145
|
+
const submitHooks = current[submitHooksSymbol];
|
|
146
|
+
const settledHooks = current[settledHooksSymbol];
|
|
147
|
+
// Single-flight opt-in is no longer per call: the router's registered
|
|
148
|
+
// flight-data consumer (see setupFlightDataConsumer) makes the transport
|
|
149
|
+
// send the request header itself, so the mutation is just called.
|
|
150
|
+
const runMutation = () => fn(...variables);
|
|
151
|
+
const run = createSolidAction(async function* (context) {
|
|
152
|
+
context.optimistic?.();
|
|
153
|
+
try {
|
|
154
|
+
const value = await context.call();
|
|
155
|
+
yield;
|
|
156
|
+
return { error: false, value };
|
|
157
|
+
}
|
|
158
|
+
catch (error) {
|
|
159
|
+
yield;
|
|
160
|
+
return { error: true, value: error };
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
form && setFormBusy(form, 1);
|
|
164
|
+
let settled;
|
|
165
|
+
let response;
|
|
166
|
+
// The transport consumer is awaited before a single-flight mutation
|
|
167
|
+
// resolves, so a counter delta over the call tells whether this action's
|
|
168
|
+
// metadata was already applied. Overlapping mutations can cross-attribute
|
|
169
|
+
// a run (skipping one default revalidation another pass just covered) —
|
|
170
|
+
// a far smaller window than predicting from the function's identity,
|
|
171
|
+
// which misses every response the server returned without flight data.
|
|
172
|
+
const flightApplicationsBefore = flightApplications;
|
|
173
|
+
try {
|
|
174
|
+
settled = await settleActionResult(run({
|
|
175
|
+
call: runMutation,
|
|
176
|
+
optimistic: submitHooks.size
|
|
177
|
+
? () => {
|
|
178
|
+
for (const hook of submitHooks.values())
|
|
179
|
+
hook(...variables);
|
|
57
180
|
}
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
181
|
+
: undefined
|
|
182
|
+
}));
|
|
183
|
+
response = await handleResponse(settled.value, settled.error, router.navigatorFactory(), flightApplications !== flightApplicationsBefore);
|
|
184
|
+
}
|
|
185
|
+
finally {
|
|
186
|
+
form && setFormBusy(form, -1);
|
|
187
|
+
}
|
|
188
|
+
let submission;
|
|
189
|
+
submission = {
|
|
190
|
+
input: variables,
|
|
191
|
+
url,
|
|
192
|
+
result: response && response.data,
|
|
193
|
+
error: response && response.error,
|
|
194
|
+
clear() {
|
|
195
|
+
router.submissions[1](entries => entries.filter(entry => entry !== submission));
|
|
196
|
+
},
|
|
197
|
+
retry() {
|
|
198
|
+
submission.clear();
|
|
199
|
+
return current[invokeSymbol].call({ r: router, f: form }, variables, current);
|
|
200
|
+
}
|
|
201
|
+
};
|
|
202
|
+
// Book-keeping is intentional: only outcomes worth showing or retrying
|
|
203
|
+
// (a result or an error) enter the submissions list, so the typical void
|
|
204
|
+
// mutation leaves nothing behind. Settled hooks still see every
|
|
205
|
+
// completion — void, metadata-only, and redirects included — one
|
|
206
|
+
// `onSettled` per invocation (#580).
|
|
207
|
+
response && router.submissions[1](entries => [...entries, submission]);
|
|
208
|
+
for (const hook of settledHooks.values())
|
|
209
|
+
hook(submission);
|
|
210
|
+
if (response) {
|
|
211
|
+
if (response.error && !form)
|
|
212
|
+
throw response.error;
|
|
213
|
+
return response.data;
|
|
68
214
|
}
|
|
69
|
-
|
|
70
|
-
...s,
|
|
71
|
-
(submission = {
|
|
72
|
-
input: variables,
|
|
73
|
-
url,
|
|
74
|
-
get result() {
|
|
75
|
-
return result()?.data;
|
|
76
|
-
},
|
|
77
|
-
get error() {
|
|
78
|
-
return result()?.error;
|
|
79
|
-
},
|
|
80
|
-
get pending() {
|
|
81
|
-
return !result();
|
|
82
|
-
},
|
|
83
|
-
clear() {
|
|
84
|
-
router.submissions[1](v => v.filter(i => i !== submission));
|
|
85
|
-
},
|
|
86
|
-
retry() {
|
|
87
|
-
setResult(undefined);
|
|
88
|
-
const p = fn(...variables);
|
|
89
|
-
return p.then(handler(), handler(true));
|
|
90
|
-
}
|
|
91
|
-
})
|
|
92
|
-
]);
|
|
93
|
-
return p.then(handler(), handler(true));
|
|
215
|
+
return undefined;
|
|
94
216
|
}
|
|
95
217
|
const o = typeof options === "string" ? { name: options } : options;
|
|
96
218
|
const name = o.name || (!isServer ? String(hashString(fn.toString())) : undefined);
|
|
97
219
|
const url = fn.url || (name && `https://action/${name}`) || "";
|
|
98
|
-
|
|
220
|
+
const wrapped = toAction(invoke, url);
|
|
99
221
|
if (name)
|
|
100
|
-
setFunctionName(
|
|
101
|
-
return
|
|
222
|
+
setFunctionName(wrapped, name);
|
|
223
|
+
return wrapped;
|
|
102
224
|
}
|
|
103
|
-
|
|
225
|
+
export const action = actionImpl;
|
|
226
|
+
function toAction(invoke, url, boundArgs = [], base = url, submitHooks = new Map(), settledHooks = new Map()) {
|
|
227
|
+
const fn = function (...args) {
|
|
228
|
+
return invoke.call(this, [...boundArgs, ...args], fn);
|
|
229
|
+
};
|
|
104
230
|
fn.toString = () => {
|
|
105
231
|
if (!url)
|
|
106
232
|
throw new Error("Client Actions need explicit names if server rendered");
|
|
107
233
|
return url;
|
|
108
234
|
};
|
|
109
235
|
fn.with = function (...args) {
|
|
110
|
-
const newFn = function (...passedArgs) {
|
|
111
|
-
return fn.call(this, ...args, ...passedArgs);
|
|
112
|
-
};
|
|
113
|
-
newFn.base = fn.base;
|
|
114
236
|
const uri = new URL(url, mockBase);
|
|
115
237
|
uri.searchParams.set("args", hashKey(args));
|
|
116
|
-
|
|
238
|
+
const next = toAction(invoke, (uri.origin === "https://action" ? uri.origin : "") + uri.pathname + uri.search, [...boundArgs, ...args], base, submitHooks, settledHooks);
|
|
239
|
+
return next;
|
|
240
|
+
};
|
|
241
|
+
fn.onSubmit = function (hook) {
|
|
242
|
+
const id = Symbol("actionOnSubmitHook");
|
|
243
|
+
submitHooks.set(id, hook);
|
|
244
|
+
getOwner() && onCleanup(() => submitHooks.delete(id));
|
|
245
|
+
return this;
|
|
246
|
+
};
|
|
247
|
+
fn.onSettled = function (hook) {
|
|
248
|
+
const id = Symbol("actionOnSettledHook");
|
|
249
|
+
settledHooks.set(id, hook);
|
|
250
|
+
getOwner() && onCleanup(() => settledHooks.delete(id));
|
|
251
|
+
return this;
|
|
117
252
|
};
|
|
118
253
|
fn.url = url;
|
|
254
|
+
fn.base = base;
|
|
255
|
+
fn[submitHooksSymbol] = submitHooks;
|
|
256
|
+
fn[settledHooksSymbol] = settledHooks;
|
|
257
|
+
fn[invokeSymbol] = invoke;
|
|
258
|
+
installRouterIntegrations();
|
|
119
259
|
if (!isServer) {
|
|
120
260
|
actions.set(url, fn);
|
|
121
261
|
// Only remove the registration if it still belongs to this instance —
|
|
122
262
|
// a re-created action (e.g. a new `.with()` binding after revalidation)
|
|
123
263
|
// may have registered itself under the same URL since.
|
|
124
|
-
getOwner() &&
|
|
264
|
+
getOwner() &&
|
|
265
|
+
onCleanup(() => actions.get(url) === fn && actions.delete(url));
|
|
125
266
|
}
|
|
126
267
|
return fn;
|
|
127
268
|
}
|
|
128
269
|
const hashString = (s) => s.split("").reduce((a, b) => ((a << 5) - a + b.charCodeAt(0)) | 0, 0);
|
|
129
|
-
async function
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
if (
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
if (response.headers.has("X-Single-Flight")) {
|
|
140
|
-
data = data._$value;
|
|
141
|
-
delete custom._$value;
|
|
142
|
-
flightKeys = Object.keys(custom);
|
|
143
|
-
}
|
|
270
|
+
async function settleActionResult(result) {
|
|
271
|
+
const value = result;
|
|
272
|
+
if (value && typeof value.then === "function") {
|
|
273
|
+
return result.then(value => value);
|
|
274
|
+
}
|
|
275
|
+
if (value && typeof value.next === "function") {
|
|
276
|
+
const iterator = value;
|
|
277
|
+
let next = await iterator.next();
|
|
278
|
+
while (!next.done) {
|
|
279
|
+
next = await iterator.next();
|
|
144
280
|
}
|
|
145
|
-
|
|
146
|
-
|
|
281
|
+
return next.value;
|
|
282
|
+
}
|
|
283
|
+
return result;
|
|
284
|
+
}
|
|
285
|
+
// Invocation count of the flight-data consumer. An action compares it across
|
|
286
|
+
// its mutation call to learn whether the transport already applied this
|
|
287
|
+
// response's metadata (and so the default revalidation pass must not run
|
|
288
|
+
// again and wipe the freshly seeded cache).
|
|
289
|
+
let flightApplications = 0;
|
|
290
|
+
/**
|
|
291
|
+
* Registers the router as the single-flight consumer of the server function
|
|
292
|
+
* transport. Subscribing is the opt-in: while registered, the transport
|
|
293
|
+
* sends the `X-Single-Flight` request header on mutations and delivers the
|
|
294
|
+
* folded payload here — fresh route data is seeded into the `query` cache
|
|
295
|
+
* and the envelope metadata (redirect `Location`, `X-Revalidate` keys) is
|
|
296
|
+
* applied, all before the action sees its plain return value. Called by the
|
|
297
|
+
* Router component on the client unless `singleFlight={false}`, which now
|
|
298
|
+
* simply means "never subscribe" — no consumer, no request header, no
|
|
299
|
+
* collection work on the server. Returns the unsubscribe function.
|
|
300
|
+
*/
|
|
301
|
+
export function setupFlightDataConsumer(router) {
|
|
302
|
+
return subscribeFlightData((data, { response }) => {
|
|
303
|
+
flightApplications++;
|
|
304
|
+
return applyResponseMetadata(response, router.navigatorFactory(), data);
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
/**
|
|
308
|
+
* Applies a server function response's integration metadata: `X-Revalidate`
|
|
309
|
+
* keys invalidate, `Location` navigates (hard for absolute urls), flight
|
|
310
|
+
* data seeds the query cache, and matching entries revalidate. Shared by
|
|
311
|
+
* the flight-data consumer and the action response path (which still sees
|
|
312
|
+
* metadata-bearing responses when no flight data was collected).
|
|
313
|
+
*/
|
|
314
|
+
function applyResponseMetadata(metadata, navigate, flightData) {
|
|
315
|
+
let keys;
|
|
316
|
+
if (metadata) {
|
|
317
|
+
if (metadata.headers.has(REVALIDATE_HEADER))
|
|
318
|
+
keys = metadata.headers.get(REVALIDATE_HEADER).split(",");
|
|
319
|
+
if (metadata.headers.has("Location")) {
|
|
320
|
+
const locationUrl = metadata.headers.get("Location") || "/";
|
|
147
321
|
if (locationUrl.startsWith("http")) {
|
|
148
322
|
window.location.href = locationUrl;
|
|
149
323
|
}
|
|
@@ -152,15 +326,43 @@ async function handleResponse(response, error, navigate) {
|
|
|
152
326
|
}
|
|
153
327
|
}
|
|
154
328
|
}
|
|
155
|
-
else if (error)
|
|
156
|
-
return { error: response };
|
|
157
|
-
else
|
|
158
|
-
data = response;
|
|
159
329
|
// invalidate
|
|
160
330
|
cacheKeyOp(keys, entry => (entry[0] = 0));
|
|
161
331
|
// set cache
|
|
162
|
-
|
|
332
|
+
flightData && Object.keys(flightData).forEach(k => query.set(k, flightData[k]));
|
|
163
333
|
// trigger revalidation
|
|
164
|
-
|
|
334
|
+
revalidate(keys, false);
|
|
335
|
+
}
|
|
336
|
+
async function handleResponse(response, error, navigate, metadataHandled) {
|
|
337
|
+
let data;
|
|
338
|
+
let flightData;
|
|
339
|
+
let metadata;
|
|
340
|
+
if (isResponseEnvelope(response)) {
|
|
341
|
+
// client-only respond(): the value rides in memory beside the metadata
|
|
342
|
+
data = response.value;
|
|
343
|
+
metadata = response.response;
|
|
344
|
+
}
|
|
345
|
+
else if (response instanceof Response) {
|
|
346
|
+
metadata = response;
|
|
347
|
+
// responses the transport hands over whole (redirects, revalidation)
|
|
348
|
+
// carry a codec-encoded body the router decodes itself. With the
|
|
349
|
+
// flight-data consumer registered single-flight payloads never reach
|
|
350
|
+
// this path, but a manually opted-in call (no consumer) still can —
|
|
351
|
+
// the runtime splits its own envelope shape.
|
|
352
|
+
if (response.body) {
|
|
353
|
+
const payload = await decodeResponsePayload(response);
|
|
354
|
+
data = payload.value;
|
|
355
|
+
flightData = payload.flightData;
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
else if (error)
|
|
359
|
+
return { error: response };
|
|
360
|
+
else
|
|
361
|
+
data = response;
|
|
362
|
+
// The transport consumer applies metadata before returning a server
|
|
363
|
+
// function's unwrapped value. Do not treat that value as a second plain
|
|
364
|
+
// action response and invalidate the freshly seeded query cache again.
|
|
365
|
+
if (!metadataHandled || metadata || flightData)
|
|
366
|
+
applyResponseMetadata(metadata, navigate, flightData);
|
|
165
367
|
return data != null ? { data } : undefined;
|
|
166
368
|
}
|