-
-
Notifications
You must be signed in to change notification settings - Fork 5k
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
docs: fix issue in cookie passing example #28223
Conversation
This splits them without choking on commas that are within a single set-cookie field-value, such as in the Expires portion.
Run & review this pull request in StackBlitz Codeflow. |
@@ -466,13 +466,13 @@ Be very careful before proxying headers to an external API and just include head | |||
If you want to pass on/proxy cookies in the other direction, from an internal request back to the client, you will need to handle this yourself. | |||
|
|||
```ts [composables/fetch.ts] | |||
import { appendResponseHeader, H3Event } from 'h3' | |||
import { appendResponseHeader, H3Event, splitCookiesString } from 'h3' |
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.
Please use splitSetCookieString
from cookie-es
directly
|
||
export const fetchWithCookie = async (event: H3Event, url: string) => { | ||
/* Get the response from the server endpoint */ | ||
const res = await $fetch.raw(url) | ||
/* Get the cookies from the response */ | ||
const cookies = (res.headers.get('set-cookie') || '').split(',') | ||
const cookies = splitCookiesString(res.headers.get('set-cookie') || '') |
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.
We can instead also use native headers.getSetCookie that returns properly splitter cookies (please double check if works within current nitro v1 env)
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.
works well!
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.
Even nicer! Sorry I was to late to the party to safe you the work, as I see it's already fixed.
Sorry, didn't spot your comments @pi0! |
Use H3's
splitCookiesString
function to split the set-cookie header without choking on commas that are within a single set-cookie field-value, such as in the Expires portion.