Skip to content

Commit 4633f0a

Browse files
committed
Add a note about using redirect with server functions
1 parent e53d4f0 commit 4633f0a

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

src/routes/solid-start/building-your-application/data-fetching.mdx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,19 @@ const getCurrentUserQuery = query(async (id: string) => {
4141
In this example, the `getCurrentUserQuery` retrieves the session data, and if an authenticated user exists, it gets their information from the database and returns it.
4242
Otherwise, it redirects the user to the login page.
4343
All of these operations are performed completely on the server regardless of how the query is called.
44+
45+
:::caution[Redirects and streaming don't mix]
46+
HTTP redirects work by setting a `3xx` status code on the response.
47+
Once a response has started streaming, its headers (including the status code) are already sent to the client and cannot be changed, even if the stream hasn't finished yet.
48+
49+
If a server function attempts to redirect after streaming has begun, you'll encounter the common error:
50+
51+
**"Cannot set headers after they are sent to the client."**
52+
53+
To avoid this, disable streaming for queries that may perform a redirect by enabling the [`deferStream`](/solid-router/reference/data-apis/create-async#deferstream) option.
54+
55+
```tsx
56+
const user = createAsync(() => getCurrentUserQuery(), { deferStream: true });
57+
```
58+
59+
:::

0 commit comments

Comments
 (0)