You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit c827716
Browse filesBrowse the repository at this point in the historyBrowse files
docs(router): formAction rename, useAction deprecation, router-owned single flight
Update the Solid 2.0 docs for @solidjs/router 2.0.0-next.41
(solidjs/solid-router#688):
- Router `action`/`Action` are now `formAction`/`FormAction`; the old names
are documented as deprecated aliases. Core solid-js `action` is unchanged.
- `useAction` is deprecated with no replacement: call the server function
(or a core `action` around it) directly.
- A mounted router applies any server function's revalidation, redirect,
reload and flight data, including direct calls from event handlers.
- Every POST server function call is a mutation that revalidates on-screen
queries by default; `respond(value, { revalidate: "" })` opts out, and
`query()`/`GET()` reads are exempt.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CLHVghj7oAvSMqnq9WRKdv
Copy file name to clipboardExpand all lines: src/routes/(3)building-apps/(3)server-functions/(0)index.mdx
+4-4Lines changed: 4 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -43,7 +43,7 @@ Search the client bundle for `database` and it is not there.
43
43
The component did not change; `getProduct` returns a promise on both sides, so the [Async reactivity](/concepts/async-reactivity) rules apply to it unchanged.
44
44
45
45
Server functions work without a router.
46
-
Solid Router's `query()` and `action()` add caching, submissions, and revalidation on top of them, and [Data loading and mutations](/routing/solid-router/data) covers that layer.
46
+
Solid Router's `query()` and `formAction()` add caching, submissions, and revalidation on top of them, and [Data loading and mutations](/routing/solid-router/data) covers that layer.
47
47
48
48
## Enable server functions
49
49
@@ -197,9 +197,9 @@ The `X-Server-Function-Instance` header the client runtime sends identifies the
197
197
198
198
### The call resolves to a `Response` object instead of data
199
199
200
-
The function returned `redirect()` or `reload()`, and the caller was plain code rather than a router action.
200
+
The function returned `redirect()` or `reload()`, and no router was subscribed to the transport when it was called.
201
201
The client transport hands responses that carry navigation or revalidation metadata back whole, so the integration that owns navigation can apply them.
202
-
Call the function through Solid Router's `action()`, or return a value and let the caller decide where to go; [Mutations and responses](/building-apps/server-functions/mutations-and-responses#redirect-the-caller) covers both.
202
+
Call the function with Solid Router mounted, which applies the metadata for a direct call or a form action alike, or return a value and let the caller decide where to go; [Mutations and responses](/building-apps/server-functions/mutations-and-responses#redirect-the-caller) covers both.
203
203
204
204
### The build fails with `server functions cannot capture non-top-level variables`
205
205
@@ -239,4 +239,4 @@ Call the function from a render, a middleware, or another server function, or pr
239
239
-[Reads, streams, and live data](/building-apps/server-functions/reads-and-live-data): declare a read that HTTP caches can store, and keep a stock level connected when another shopper changes it.
240
240
-[Arguments and security](/building-apps/server-functions/arguments-and-security): what a caller can send, what the transport can encode, and how to validate and authorize every request.
241
241
-[Mutations and responses](/building-apps/server-functions/mutations-and-responses): return a redirect, a reload, or a 400 from a cart mutation, and control what a thrown error reveals.
242
-
-[Data loading and mutations](/routing/solid-router/data): wrap the same functions in `query()` and `action()` for caching, submissions, and revalidation.
242
+
-[Data loading and mutations](/routing/solid-router/data): wrap the same functions in `query()` and `formAction()` for caching, submissions, and revalidation.
Copy file name to clipboardExpand all lines: src/routes/(3)building-apps/(3)server-functions/(3)mutations-and-responses.mdx
+36-16Lines changed: 36 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -78,9 +78,9 @@ Return a redirect when it is the successful outcome, and throw it when it is an
78
78
Both reach the caller the same way.
79
79
80
80
What the browser does next depends on who made the call.
81
-
A Solid Router `action()` navigates to `/goodbye` without a page load.
81
+
With Solid Router mounted, a form action or a direct `deleteAccount()` call from an event handler navigates to `/goodbye` without a page load.
82
82
An HTML form post without JavaScript is answered with the redirect itself, 302 by default, and the browser follows it.
83
-
Plain code that called`deleteAccount()`directly receives the `Response` object as the resolved value, because the transport hands navigation metadata back whole for the caller to apply.
83
+
Code that calls`deleteAccount()`with no router subscribed to the transport receives the `Response` object as the resolved value, because the transport hands navigation metadata back whole for the caller to apply.
84
84
85
85
The target can be a string or an `Href` produced by a router's typed paths helper; a branded `Href` redirects to its logical path.
86
86
Any other object throws `redirect() expects a string URL or an Href-branded value`.
Call `addToCart("mug", 1)`through a router action and the cart query refetches when the call settles; nothing else on the page is touched.
104
+
Call `addToCart("mug", 1)`with Solid Router mounted, from a form action or straight from an event handler, and the cart query refetches when the call settles; nothing else on the page is touched.
105
105
Omit `revalidate` to ask for every cached read to refresh.
106
106
107
107
Core carries the keys in an `X-Revalidate` header and does not define a cache.
108
108
Solid Router reads the header against its query cache; [What revalidates after a mutation](/routing/solid-router/data#what-revalidates-after-a-mutation) explains the matching.
109
109
`respond()` and `redirect()` accept the same `revalidate` option, for a mutation that also returns a value or changes location.
110
110
111
+
Solid Router treats every `POST` call as a mutation, and `POST` is the default.
112
+
A call that declares no `revalidate` keys revalidates every query on screen, the same default a form action has.
113
+
Reads are exempt when they go through the router's `query()`, which declares the function `GET`, or when the function is declared with `GET()`.
114
+
A `POST` that does not change anything, such as a log call or a search sent on each keystroke, declares an empty `revalidate` so it touches nothing:
115
+
116
+
```ts
117
+
// src/data/telemetry.ts
118
+
import { respond } from"@solidjs/web";
119
+
120
+
exportasyncfunction logEvent(name:string) {
121
+
"use server";
122
+
123
+
awaitanalytics.record(name);
124
+
returnrespond(null, { revalidate: "" });
125
+
}
126
+
```
127
+
128
+
`revalidate: ""` declares no keys, the same as `revalidate: []`; return `respond(value, { revalidate: "" })` when the call also returns a value.
129
+
111
130
## Handle thrown errors
112
131
113
132
A returned or thrown `Response` or envelope is control flow, and the handler preserves its status, headers, and value.
With the `Avoid` version the HTTP response is a 400, and the promise in the browser resolves as if the write had worked; a router action records it as a successful submission with `{ issues }` as its result.
190
+
With the `Avoid` version the HTTP response is a 400, and the promise in the browser resolves as if the write had worked; a router form action records it as a successful submission with `{ issues }` as its result.
172
191
The status on a returned envelope is for HTTP callers; the throw is what tells the caller something failed.
173
192
:::
174
193
175
194
## Add router submissions
176
195
177
-
Solid Router's `action()` turns a server function into something a form can post to and the router can track:
196
+
Solid Router's `formAction()` turns a server function into something a form can post to and the router can track:
178
197
179
198
```ts
180
199
// src/data/cart.ts
181
-
import { action } from"@solidjs/router";
200
+
import { formAction } from"@solidjs/router";
182
201
import { reload } from"@solidjs/web";
183
202
184
203
exportasyncfunction addToCart(form:FormData) {
@@ -197,19 +216,19 @@ export async function addToCart(form: FormData) {
Render `<form method="post" action={addToCartAction}>` and the form posts to the function's URL before JavaScript loads; after hydration the router intercepts the submit, calls the function over the transport, and applies the `reload`.
204
223
The router marks the form `aria-busy` while it runs and, once it settles, records a submission whose `error` is the thrown envelope's value, which the [Forms guide](/guides/forms) reads to show messages next to the fields.
205
224
206
225
Keep the server function a named export and wrap it separately, as above, so a test or an API route can call `addToCart` without the form.
207
-
When the function exists only for the form, write the body inline, `action(async (form: FormData) => { "use server"; ... })`; the [Data loading and mutations](/routing/solid-router/data#mutate-with-actions) page uses that shape throughout, and the two behave the same.
226
+
When the function exists only for the form, write the body inline, `formAction(async (form: FormData) => { "use server"; ... })`; the [Data loading and mutations](/routing/solid-router/data#mutate-with-actions) page uses that shape throughout, and the two behave the same.
208
227
209
228
:::note[Which action]
210
-
`action` here is from `@solidjs/router`.
211
-
`solid-js`also exports an[`action`](/reference/solid-js/lifecycle-actions/action), which runs a generator as a reactive transaction and has no URL; [Mutations](/concepts/mutations) covers it.
212
-
A form needs the router's.
229
+
`formAction`is from `@solidjs/router`; earlier releases called it `action`, and that name remains as a deprecated alias.
230
+
`solid-js` exports its own[`action`](/reference/solid-js/lifecycle-actions/action), which runs a generator as a reactive transaction and has no URL; [Mutations](/concepts/mutations) covers it.
231
+
A form needs the router's`formAction`.
213
232
:::
214
233
215
234
When the router registers its single-flight integration, the mutation response can carry refreshed route data alongside the result; the server-function runtime treats that payload as opaque.
@@ -229,8 +248,9 @@ A returned envelope resolves the caller with its value whatever the status; `thr
229
248
230
249
### The redirect comes back as a `Response` object
231
250
232
-
The function was called directly rather than through a router action, and the transport hands responses that carry a `Location` or `X-Revalidate` header back whole.
233
-
Call the function through `action()`, or return a value and navigate in the caller.
251
+
No router was subscribed to the transport when the function was called, so it handed the response that carries a `Location` or `X-Revalidate` header back whole.
252
+
That happens outside a mounted Solid Router, or with `singleFlight: false` set on it.
253
+
Call the function under a mounted router, or return a value and navigate in the caller.
234
254
235
255
### `redirect()` throws a `TypeError` about an Href-branded value
236
256
@@ -240,12 +260,12 @@ Pass a string path, or the value a router's typed paths helper returns.
240
260
## Recap
241
261
242
262
- Return a plain value when the caller needs data and nothing else; use `respond()` to add a status or headers to it.
243
-
- Return or throw `redirect(path)`; a router action navigates, a form post follows the redirect, and direct code receives the `Response`.
244
-
- Return `reload({ revalidate })` after a write so the router refetches the named reads.
263
+
- Return or throw `redirect(path)`; a mounted router navigates, a form post follows the redirect, and code with no router receives the `Response`.
264
+
- Return `reload({ revalidate })` after a write so the router refetches the named reads; a `POST` with no keys revalidates every query on screen, and `respond(value, { revalidate: "" })` opts out.
245
265
- Throw `respond(value, { status })` for a failure the caller should see; a returned envelope resolves the caller whatever its status.
246
266
- A plain thrown value becomes `Internal Server Error` in production; `markSafeError()` opts one `Error` out of that.
247
267
- The development build is selected by the `development` export condition, not by `NODE_ENV`.
248
-
- Wrap a named server function with the router's `action()` for forms and submissions; write it inline only when nothing else calls it.
268
+
- Wrap a named server function with the router's `formAction()` for forms and submissions; write it inline only when nothing else calls it.
Copy file name to clipboardExpand all lines: src/routes/(3)building-apps/(3)server-functions/(5)progressive-enhancement.mdx
+9-9Lines changed: 9 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,12 +12,12 @@ A server function has a URL, so the second version is available to every mutatio
12
12
The server-function runtime recognizes a browser form post at that URL, decodes the body as one `FormData` argument, and answers in a way the browser can follow with no script.
13
13
14
14
Application code reaches this through a router.
15
-
Solid Router's `action()` wraps the server function in a value that serializes to that URL, so the same `<form action={...}>` works before hydration and is taken over by the router after it.
15
+
Solid Router's `formAction()` wraps the server function in a value that serializes to that URL, so the same `<form action={...}>` works before hydration and is taken over by the router after it.
16
16
Most applications need only the [router action](#post-a-form-through-a-router-action) section; the rest of the page is the transport underneath, for forms outside the router and for integrations that build their own form helpers.
17
17
18
18
## Post a form through a router action
19
19
20
-
Declare a server function that takes one `FormData` argument, and wrap it with `action` from `@solidjs/router`:
20
+
Declare a server function that takes one `FormData` argument, and wrap it with `formAction` from `@solidjs/router`:
21
21
22
22
```ts
23
23
// src/data/cart.ts
@@ -39,10 +39,10 @@ export async function addToCart(form: FormData) {
@@ -118,14 +118,14 @@ Whether TypeScript knows about `url` depends on how the function was declared:
118
118
- A bare `"use server"` function keeps its declared function type.
119
119
`url` is present at runtime and absent from the type, so `addToCart.url` is a type error.
120
120
121
-
For a form post, use a router action rather than the URL.
121
+
For a form post, use a router form action rather than the URL.
122
122
An integration that must read the address from a bare declaration narrows it to `ServerFunction` from `@solidjs/web/server-functions` after checking `isServerFunction(fn)`.
123
123
124
124
## Bind leading arguments
125
125
126
126
The function id lives in the URL path.
127
127
An integration can add JSON-safe bound arguments in the reserved `args` query parameter, while the submitted form remains the final `FormData` argument.
128
-
Solid Router's `action.with(...)` does this for application code; with a `removeFromCart(productId: string, form: FormData)` action, the product id is bound and the form supplies the rest:
128
+
Solid Router's `formAction(...).with(...)` does this for application code; with a `removeFromCart(productId: string, form: FormData)` form action, the product id is bound and the form supplies the rest:
@@ -167,7 +167,7 @@ A host can configure `createNoJSHandler()` with a base path or supply another `h
167
167
### `Property 'url' does not exist` on a server function
168
168
169
169
The function is a bare `"use server"` declaration, whose type is the function's own.
170
-
Pass it through the router's `action()` for a form, or, in integration code, narrow it to `ServerFunction` after `isServerFunction(fn)`.
170
+
Pass it through the router's `formAction()` for a form, or, in integration code, narrow it to `ServerFunction` after `isServerFunction(fn)`.
171
171
172
172
### `Bound arguments in an action url must be JSON-safe`
173
173
@@ -188,7 +188,7 @@ Solid Router reads the cookie on the next server render and records the submissi
188
188
## Recap
189
189
190
190
- Put every mutation behind `<form method="post" action={...}>` so the browser can submit it before the client bundle runs.
191
-
- Wrap the server function with the router's `action()`; it serializes to the function's plain-HTTP URL and takes over after hydration.
191
+
- Wrap the server function with the router's `formAction()`; it serializes to the function's plain-HTTP URL and takes over after hydration.
192
192
- Pending state, `useSubmissions`, and `.onSubmit` optimistic writes are layers the hydrated page adds; the HTML and the server function stay the same.
193
193
- A browser form post follows a returned redirect; any other outcome is answered with a 303 back to the page and a flash cookie, which the router turns into a submission on the next render.
194
194
- Use `method="get"` and `fn.url` only for a `GET()` read; the function receives the fields as `URLSearchParams`.
@@ -199,5 +199,5 @@ Solid Router reads the cookie on the next server render and records the submissi
199
199
## Next steps
200
200
201
201
-[Forms](/guides/forms): the checkout address form, working before hydration and adding inline messages and an optimistic address list after it.
202
-
-[Data loading and mutations](/routing/solid-router/data): what `action()` adds on top of this transport, including `.with()`, submissions, and revalidation.
202
+
-[Data loading and mutations](/routing/solid-router/data): what `formAction()` adds on top of this transport, including `.with()`, submissions, and revalidation.
203
203
-[Sessions and auth](/building-apps/sessions-and-auth): the sign-in form, the most common no-JavaScript submission in a storefront.
0 commit comments