Skip to content

Commit e4ebfae

Browse files
amirhhashemikodiakhq[bot]LadyBluenotes
authored
Add a note about using redirect with server functions (#1386)
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com> Co-authored-by: Sarah <[email protected]>
1 parent 49c8691 commit e4ebfae

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,17 @@ 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[Modifying headers after streaming]
46+
Once streaming begins, response headers (including status and cookies) are sent and cannot be changed.
47+
Any header-modifying logic within a server function, such as redirects or APIs like `useSession` that set cookies, must run before streaming starts;
48+
otherwise, this error will occur:
49+
`Cannot set headers after they are sent to the client.`
50+
51+
To avoid this, disable streaming for queries that may modify headers by enabling the [`deferStream`](/solid-router/reference/data-apis/create-async#deferstream) option.
52+
53+
```tsx
54+
const user = createAsync(() => getCurrentUserQuery(), { deferStream: true });
55+
```
56+
57+
:::

0 commit comments

Comments
 (0)