Skip to content

Commit 7afef91

Browse files
add more docs for response helpers (#958)
Co-authored-by: Sarah <[email protected]>
1 parent 5ae084d commit 7afef91

File tree

8 files changed

+183
-58
lines changed

8 files changed

+183
-58
lines changed

src/routes/solid-router/reference/data-apis/data.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
"create-async.mdx",
77
"create-async-store.mdx",
88
"query.mdx",
9-
"response-helpers.mdx",
109
"use-submission.mdx",
1110
"use-submissions.mdx"
1211
]

src/routes/solid-router/reference/data-apis/response-helpers.mdx

Lines changed: 0 additions & 56 deletions
This file was deleted.
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
{
22
"title": "Reference",
3-
"pages": ["components", "data-apis", "preload-functions", "primitives"]
3+
"pages": [
4+
"components",
5+
"data-apis",
6+
"response-helpers",
7+
"preload-functions",
8+
"primitives"
9+
]
410
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"title": "Response Helpers",
3+
"pages": ["json.mdx", "redirect.mdx", "reload.mdx", "revalidate.mdx"]
4+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
title: json
3+
---
4+
5+
Returns JSON data from an action while also providing options for controlling revalidation of cache data on the route.
6+
7+
```ts title="/actions/get-completed-todos.ts" {7}
8+
import { action, json } from "@solidjs/router";
9+
import { fetchTodo } from "../fetchers";
10+
11+
const getCompletedTodos = action(async () => {
12+
const completedTodos = await fetchTodo({ status: 'complete' });
13+
14+
return json(completedTodos, { revalidate: getTodo.keyFor(id) });
15+
});
16+
```
17+
18+
Also read [action](/solid-router/reference/data-apis/action) and [revalidate](/solid-router/reference/response-helpers/revalidate).
19+
20+
## Type Signature
21+
22+
```typescript
23+
interface ResponseOptions {
24+
revalidate?: string | string[];
25+
}
26+
27+
json<T>(data: T, opt?: ResponseOptions): CustomResponse<T>;
28+
```
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
---
2+
title: redirect
3+
---
4+
5+
Redirects to the next route.
6+
When done over a server RPC (Remote Procedure Call), the redirect will be done through the server.
7+
By default the status code of a `redirect()` is `302 - FOUND`, also known as a temporary redirect.
8+
9+
Other useful redirect codes:
10+
11+
| Code | Description |
12+
| ---- | ----------- |
13+
| `301` | Moved Permanently |
14+
| `307` | Temporary Redirect |
15+
| `308` | Permanent redirect |
16+
17+
<Callout type="tip" name="Redirect Methods">
18+
307 and 308 won't allow the browser to change the method of the request. If you want to change the method, you should use 301 or 302.
19+
</Callout>
20+
21+
A common use-case for throwing a redirect is when a user is not authenticated and needs to be sent to the login page or another public route.
22+
23+
```js title="/queries/get-user.ts" {7}
24+
import { query, redirect } from "@solidjs/router";
25+
import { getCurrentUser } from "../auth";
26+
27+
const getUser = query(() => {
28+
const user = await getCurrentUser();
29+
30+
if (!user) throw redirect("/login");
31+
32+
return user;
33+
}, "get-user")
34+
```
35+
36+
## Single-Flight Mutations
37+
38+
When using `redirect` during a Server Action, the redirect will be done through the server.
39+
The response value will automatically send data for the destination route, avoiding a subsequent roundtrip to load the data from the target route.
40+
41+
This is useful when redirecting the user to a different route once a mutation is done.
42+
43+
```ts title="/actions/add-user.ts" {3,6}
44+
import { action, redirect } from "@solidjs/router";
45+
46+
const addUser = action(async (user: User) => {
47+
await postUser(user);
48+
49+
return redirect("/users");
50+
});
51+
```
52+
53+
The `addUser` action will redirect the user to the `/users` route once the user has been added to the database.
54+
The response from the form action will send the updated data for the `/users` route without the developer needing to revalidate or reload.
55+
56+
## Throw vs Return
57+
58+
Both `throw` and `return` can be used to redirect the user to a different route.
59+
For general usage `throw` is recommended as it immediately stops the execution of the current action and redirects the user.
60+
61+
When returning from a nested method, the parent method will continue to execute, which can lead to unexpected behavior.
62+
63+
### TypeScript Signature
64+
65+
```typescript
66+
interface ResponseOptions {
67+
revalidate?: string | string[];
68+
} | number
69+
70+
function redirect(url: string, opts = 302): CustomResponse<never>;
71+
```
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
title: reload
3+
---
4+
5+
Reload is a response helper built on top of [revalidate](/solid-router/response-helpers/revalidate).
6+
It will receive a cache key, or an array of cache keys, to invalidate those queries, and cause them to fire again.
7+
8+
```ts title="/actions/update-todo.ts"{4}
9+
import { action, reload } from "@solidjs/router";
10+
import { putTodo, getTodo } from "../db";
11+
12+
const updateTodo = action(async (todo: Todo) => {
13+
await putTodo(todo.id, todo);
14+
15+
return reload({ revalidate: getTodo.keyFor(id) });
16+
});
17+
```
18+
19+
The code snippet above uses the cache-key from a user-defined query (`getTodo`).
20+
To better understand how queries work, check the [query](/solid-router/reference/data-apis/query) documentation.
21+
22+
## TypeScript Signature
23+
24+
```ts
25+
interface ResponseOptions {
26+
revalidate?: string | string[];
27+
}
28+
29+
reload(opt?: ResponseOptions): CustomResponse<never>;
30+
```
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
---
2+
title: revalidate
3+
---
4+
5+
Revalidate will receive the either a cache key or an array of cache keys and invalidate those queries.
6+
7+
<Callout type="tip" name="Firing Queries Again">
8+
If you intend to re-fire the queries immediately, check the [reload](/solid-router/reference/response-helpers/reload) helper.
9+
</Callout>
10+
11+
The code below will revalidate the `getTodo` query with the cache key.
12+
13+
```ts title="/actions/update-todo.ts"{4}
14+
import { action, revalidate } from "@solidjs/router";
15+
16+
const updateTodo = action(async (todo: Todo) => {
17+
await putTodo(todo.id, todo);
18+
19+
return revalidate(getTodo.keyFor());
20+
});
21+
```
22+
23+
To better understand how queries work, check the [query](/solid-router/reference/data-apis/query) documentation.
24+
25+
## Force Revalidation
26+
27+
The `revalidate` function also accepts a second parameter to force the revalidation of the cache data.
28+
29+
```ts title="/actions/update-todo.ts"{4}
30+
import { action, revalidate } from "@solidjs/router";
31+
32+
const updateTodo = action(async (todo: Todo) => {
33+
await putTodo(todo.id, todo);
34+
35+
return revalidate(getTodo.keyFor(), true);
36+
});
37+
```
38+
39+
## TypeScript Signature
40+
41+
```ts
42+
function revalidate(key?: string | string[] | void, force?: boolean): Promise<void>;
43+
```

0 commit comments

Comments
 (0)