Skip to content

Commit c827716

Browse files
committed
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
1 parent d8a6c27 commit c827716

17 files changed

Lines changed: 264 additions & 157 deletions

‎src/routes/(3)building-apps/(3)server-functions/(0)index.mdx‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Search the client bundle for `database` and it is not there.
4343
The component did not change; `getProduct` returns a promise on both sides, so the [Async reactivity](/concepts/async-reactivity) rules apply to it unchanged.
4444

4545
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.
4747

4848
## Enable server functions
4949

@@ -197,9 +197,9 @@ The `X-Server-Function-Instance` header the client runtime sends identifies the
197197

198198
### The call resolves to a `Response` object instead of data
199199

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.
201201
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.
203203

204204
### The build fails with `server functions cannot capture non-top-level variables`
205205

@@ -239,4 +239,4 @@ Call the function from a render, a middleware, or another server function, or pr
239239
- [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.
240240
- [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.
241241
- [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.

‎src/routes/(3)building-apps/(3)server-functions/(3)mutations-and-responses.mdx‎

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ Return a redirect when it is the successful outcome, and throw it when it is an
7878
Both reach the caller the same way.
7979

8080
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.
8282
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.
8484

8585
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.
8686
Any other object throws `redirect() expects a string URL or an Href-branded value`.
@@ -101,13 +101,32 @@ export async function addToCart(productId: string, quantity: number) {
101101
}
102102
```
103103

104-
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.
105105
Omit `revalidate` to ask for every cached read to refresh.
106106

107107
Core carries the keys in an `X-Revalidate` header and does not define a cache.
108108
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.
109109
`respond()` and `redirect()` accept the same `revalidate` option, for a mutation that also returns a value or changes location.
110110

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+
export async function logEvent(name: string) {
121+
"use server";
122+
123+
await analytics.record(name);
124+
return respond(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+
111130
## Handle thrown errors
112131

113132
A returned or thrown `Response` or envelope is control flow, and the handler preserves its status, headers, and value.
@@ -168,17 +187,17 @@ return respond({ issues }, { status: 400 });
168187
throw respond({ issues }, { status: 400 });
169188
```
170189

171-
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.
172191
The status on a returned envelope is for HTTP callers; the throw is what tells the caller something failed.
173192
:::
174193

175194
## Add router submissions
176195

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:
178197

179198
```ts
180199
// src/data/cart.ts
181-
import { action } from "@solidjs/router";
200+
import { formAction } from "@solidjs/router";
182201
import { reload } from "@solidjs/web";
183202

184203
export async function addToCart(form: FormData) {
@@ -197,19 +216,19 @@ export async function addToCart(form: FormData) {
197216
return reload({ revalidate: "cart" });
198217
}
199218

200-
export const addToCartAction = action(addToCart);
219+
export const addToCartAction = formAction(addToCart);
201220
```
202221

203222
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`.
204223
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.
205224

206225
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.
208227

209228
:::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`.
213232
:::
214233

215234
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
229248

230249
### The redirect comes back as a `Response` object
231250

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.
234254

235255
### `redirect()` throws a `TypeError` about an Href-branded value
236256

@@ -240,12 +260,12 @@ Pass a string path, or the value a router's typed paths helper returns.
240260
## Recap
241261

242262
- 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.
245265
- Throw `respond(value, { status })` for a failure the caller should see; a returned envelope resolves the caller whatever its status.
246266
- A plain thrown value becomes `Internal Server Error` in production; `markSafeError()` opts one `Error` out of that.
247267
- 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.
249269

250270
## Next steps
251271

‎src/routes/(3)building-apps/(3)server-functions/(5)progressive-enhancement.mdx‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ A server function has a URL, so the second version is available to every mutatio
1212
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.
1313

1414
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.
1616
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.
1717

1818
## Post a form through a router action
1919

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`:
2121

2222
```ts
2323
// src/data/cart.ts
@@ -39,10 +39,10 @@ export async function addToCart(form: FormData) {
3939

4040
```tsx
4141
// src/pages/Product.tsx
42-
import { action } from "@solidjs/router";
42+
import { formAction } from "@solidjs/router";
4343
import { addToCart } from "../data/cart";
4444

45-
const addToCartAction = action(addToCart);
45+
const addToCartAction = formAction(addToCart);
4646

4747
<form method="post" action={addToCartAction}>
4848
<input type="hidden" name="productId" value={props.id} />
@@ -118,14 +118,14 @@ Whether TypeScript knows about `url` depends on how the function was declared:
118118
- A bare `"use server"` function keeps its declared function type.
119119
`url` is present at runtime and absent from the type, so `addToCart.url` is a type error.
120120

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.
122122
An integration that must read the address from a bare declaration narrows it to `ServerFunction` from `@solidjs/web/server-functions` after checking `isServerFunction(fn)`.
123123

124124
## Bind leading arguments
125125

126126
The function id lives in the URL path.
127127
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:
129129

130130
```tsx
131131
<form method="post" action={removeFromCart.with(line.productId)}>
@@ -167,7 +167,7 @@ A host can configure `createNoJSHandler()` with a base path or supply another `h
167167
### `Property 'url' does not exist` on a server function
168168

169169
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)`.
171171

172172
### `Bound arguments in an action url must be JSON-safe`
173173

@@ -188,7 +188,7 @@ Solid Router reads the cookie on the next server render and records the submissi
188188
## Recap
189189

190190
- 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.
192192
- Pending state, `useSubmissions`, and `.onSubmit` optimistic writes are layers the hydrated page adds; the HTML and the server function stay the same.
193193
- 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.
194194
- 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
199199
## Next steps
200200

201201
- [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.
203203
- [Sessions and auth](/building-apps/sessions-and-auth): the sign-in form, the most common no-JavaScript submission in a storefront.

0 commit comments

Comments
 (0)