-
Notifications
You must be signed in to change notification settings - Fork 337
Add a note about using redirect with server functions
#1386
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Add a note about using redirect with server functions
#1386
Conversation
|
|
✅ Deploy Preview for solid-docs ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
| Once a response starts streaming, its headers (including the status code and cookies) are immediately sent to the client and **cannot be modified**. | ||
|
|
||
| Any server-side operation that changes headers, such as performing a redirect (`3xx` status) or using APIs like `useSession` that modify cookies, must happen **before** streaming begins. | ||
| Otherwise, you'll encounter errors like: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| Once a response starts streaming, its headers (including the status code and cookies) are immediately sent to the client and **cannot be modified**. | |
| Any server-side operation that changes headers, such as performing a redirect (`3xx` status) or using APIs like `useSession` that modify cookies, must happen **before** streaming begins. | |
| Otherwise, you'll encounter errors like: | |
| Once streaming begins, response headers (including status and cookies) are sent and cannot be changed. | |
| Any header-modifying logic, such as redirects or APIs that set cookies, must run before streaming starts, or an error will occur. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I updated it with two changes:
- I kept
useSessionas an example. It's a very common source of the error. - I mentioned that this only happens when the logic is executed on the server.
|
|
||
| **"Cannot set headers after they are sent to the client."** | ||
|
|
||
| To avoid this, disable streaming for queries that may modify headers by enabling the [`deferStream`](/solid-router/reference/data-apis/create-async#deferstream) option. | ||
|
|
||
| ```tsx | ||
| const user = createAsync(() => getCurrentUserQuery(), { deferStream: true }); | ||
| ``` | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| **"Cannot set headers after they are sent to the client."** | |
| To avoid this, disable streaming for queries that may modify headers by enabling the [`deferStream`](/solid-router/reference/data-apis/create-async#deferstream) option. | |
| ```tsx | |
| const user = createAsync(() => getCurrentUserQuery(), { deferStream: true }); | |
| ``` | |
| To avoid this, disable streaming for queries that may modify headers by enabling the [`deferStream`](/solid-router/reference/data-apis/create-async#deferstream): | |
| ```tsx | |
| const user = createAsync(() => getCurrentUserQuery(), { deferStream: true }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should keep the error message. It's a common and vague message, and people might try to search it in the docs.
This is complementary to #1385.