-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Description
Describe the bug
When I try to set or delete multiple cookies that share the same name (but exist on different paths) using the event.cookies API, then only the last call is retained.
I have found no work-around within SvelteKit, because the event.setHeaders API states:
You cannot add a
set-cookieheader withsetHeaders— use the cookies API instead.
Reproduction
A Server Load function like the following highlights the issue. When it runs, the resulting set-cookie headers only include the key cookie once, but it should be included twice.
I was not able to reproduce this on Stackblitz because they seem to be difficult about setting cookies. Here's how to reproduce it locally:
Code
routes/test/+page.server.js:
export const load = (event) => {
event.cookies.set('key', 'value', { path: `/foo/` });
event.cookies.set('key', 'value', { path: `/bar/` });
}routes/test/+page.svelte:
Hello worldReproduction
- Serve your app on localhost
- Open the network inspector in your browser
- Load the page at
localhost/test - Observe the response headers
Expected result
set-cookie: key=value; Path=/foo/; HttpOnly; SameSite=Lax
set-cookie: key=value; Path=/bar/; HttpOnly; SameSite=Lax
Actual result
set-cookie: key=value; Path=/bar/; HttpOnly; SameSite=Lax
Logs
System Info
n/aSeverity
serious, but I can work around it
Additional Information
It seems that SvelteKit treats cookies as "unique by name", whereas it should be treating cookies as "unique by (at least) path and name":
kit/packages/kit/src/runtime/server/cookie.js
Line 216 in 217fc24
| new_cookies[name] = { name, value, options: { ...options, path } }; |
It might be that other fields should be considered as well for creating a unique cookie identifier, such as the domain property.