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
Return JSON data from actions with cache revalidation control. Configure how
15
+
Return JSON data from actions with query revalidation control. Configure how
16
16
route data updates after mutations for optimal performance.
17
17
---
18
18
19
-
Returns JSON data from an action while also providing options for controlling revalidation of cache data on the route.
19
+
The `json` function returns a [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response) object that contains the provided data.
20
+
It is intended for sending JSON data from a [query](/solid-router/reference/data-apis/query) or [action](/solid-router/concepts/actions) while also allowing configuration of query revalidation.
20
21
21
-
```ts title="/actions/get-completed-todos.ts" {7}
22
-
import { action, json } from"@solidjs/router";
23
-
import { fetchTodo } from"../fetchers";
22
+
This works both in client and server (e.g., using a server function) environments.
Also read [action](/solid-router/reference/data-apis/action) and [revalidate](/solid-router/reference/response-helpers/revalidate).
30
+
## Type
33
31
34
-
## Type Signature
32
+
```ts
33
+
function json<T>(
34
+
data:T,
35
+
init: {
36
+
revalidate?:string|string[];
37
+
headers?:HeadersInit;
38
+
status?:number;
39
+
statusText?:string;
40
+
} = {}
41
+
):CustomResponse<T>;
42
+
```
43
+
44
+
## Parameters
45
+
46
+
### `data`
47
+
48
+
-**Type:**`T`
49
+
-**Required:** Yes
50
+
51
+
The data to be serialized as JSON in the response body.
52
+
It must be a value that can be serialized with [`JSON.stringify`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify).
The `ResponseOptions` extens the types from the native [`ResponseInit`](https://developer.mozilla.org/en-US/docs/Web/API/Response/Response#options) interface.
Copy file name to clipboardExpand all lines: src/routes/solid-router/reference/response-helpers/redirect.mdx
+73-56Lines changed: 73 additions & 56 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,89 +10,106 @@ tags:
10
10
- routing
11
11
- authorization
12
12
- forms
13
-
version: '1.0'
13
+
version: "1.0"
14
14
description: >-
15
15
Redirect users between routes with proper status codes. Handle authentication
16
16
flows, form submissions, and protected route access.
17
17
---
18
18
19
-
Redirects to the next route.
20
-
When done over a server RPC (Remote Procedure Call), the redirect will be done through the server.
21
-
By default the status code of a `redirect()` is `302 - FOUND`, also known as a temporary redirect.
19
+
The `redirect` function returns a [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response) object that instructs the router to navigate to a different route when returned or thrown from a [query](/solid-router/reference/data-apis/query) or [action](/solid-router/concepts/actions).
22
20
23
-
Other useful redirect codes:
21
+
This works both in client and server (e.g., using a server function) environments.
24
22
25
-
| Code | Description |
26
-
| ---- | ----------- |
27
-
|`301`| Moved Permanently |
28
-
|`307`| Temporary Redirect |
29
-
|`308`| Permanent redirect |
23
+
## Import
30
24
31
-
:::tip[Redirect Methods]
32
-
307 and 308 won't allow the browser to change the method of the request.
33
-
If you want to change the method, you should use 301 or 302.
34
-
:::
25
+
```ts
26
+
import { redirect } from"@solidjs/router";
27
+
```
35
28
36
-
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.
29
+
## Type
37
30
38
-
```js title="/queries/get-user.ts" {7}
39
-
import { query, redirect } from"@solidjs/router";
40
-
import { getCurrentUser } from"../auth";
41
-
42
-
constgetUser=query(() => {
43
-
constuser=awaitgetCurrentUser();
44
-
45
-
if (!user) throwredirect("/login");
46
-
47
-
return user;
48
-
}, "get-user")
31
+
```ts
32
+
function redirect(
33
+
url:string,
34
+
init?:
35
+
|number
36
+
| {
37
+
revalidate?:string|string[];
38
+
headers?:HeadersInit;
39
+
status?:number;
40
+
statusText?:string;
41
+
}
42
+
):CustomResponse<never>;
49
43
```
50
44
51
-
## Single-Flight Mutations
45
+
## Parameters
52
46
53
-
When using `redirect` during a Server Action, the redirect will be done through the server.
54
-
The response value will automatically send data for the destination route, avoiding a subsequent roundtrip to load the data from the target route.
47
+
### `url`
55
48
56
-
This is useful when redirecting the user to a different route once a mutation is done.
function redirect(url:string, init?:number|RouterResponseInit):CustomResponse<never>;
86
-
```
70
+
-**Type:**`number`
71
+
-**Required:** No
87
72
88
-
The `RouterResponseInit` type extends the native [`ResponseInit`](https://developer.mozilla.org/en-US/docs/Web/API/Response/Response#options) interface.
73
+
The HTTP status code for the redirect.
74
+
Defaults to [`302 Found`)](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/302).
89
75
90
-
## Revalidate Query Keys
76
+
## Examples
91
77
92
-
You can pass query keys to the redirect helper to revalidate them.
0 commit comments