kit: `cookies.delete` not working when using form `enhance`
Describe the bug
Using enhance
on a <form>
and throwing redirect
in the form action causes cookies.delete
to not work in the page server load function.
Reproduction
https://github.com/s3812497/sveltekit-enhance-bug
Reproduction:
- Click the enhanced form button.
- User is redirected.
- Cookie is not deleted.
Expected:
- Click the enhanced form button.
- User is redirected.
- Cookie is deleted in
+page.server
Logs
No response
System Info
System:
OS: macOS 12.5.1
CPU: (8) arm64 Apple M1 Pro
Memory: 1.56 GB / 32.00 GB
Shell: 5.8.1 - /bin/zsh
Binaries:
Node: 16.17.0 - ~/.nvm/versions/node/v16.17.0/bin/node
npm: 8.15.0 - ~/.nvm/versions/node/v16.17.0/bin/npm
Browsers:
Chrome: 104.0.5112.79
Firefox: 104.0.1
Firefox Developer Edition: 105.0
Safari: 15.6.1
Safari Technology Preview: 16.0
npmPackages:
@sveltejs/adapter-auto: next => 1.0.0-next.71
@sveltejs/kit: next => 1.0.0-next.472
svelte: ^3.44.0 => 3.50.0
vite: ^3.1.0 => 3.1.0
Severity
serious, but I can work around it
Additional Information
No response
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 1
- Comments: 15 (14 by maintainers)
Commits related to this issue
- [fix] harmonize cookie path and add dev time warnings - Warnings will be logged to the console if it's detected that there's likely misusage of the path parameter. - More documentation - Fix mismatch... — committed to sveltejs/kit by dummdidumm 2 years ago
- [fix] harmonize cookie path and add dev time warnings (#7416) * [fix] harmonize cookie path and add dev time warnings - Warnings will be logged to the console if it's detected that there's likely ... — committed to sveltejs/kit by dummdidumm 2 years ago
- fix session cookie not being deleted after logout Context: https://github.com/sveltejs/kit/issues/6609#issuecomment-1259529045 — committed to ellicodan/sveltekit-medusa-client by danielcelliot 10 months ago
Mhm then maybe this is a bug with deleting cookies after all…
@s3812497 i haven’t used the new
cookies.delete
function, but i am able to usecookies.set
to remove a cookie:It works as expected.
@s3812497 Yep. #6622 fixed something relatively obvious, but there’s something else going on here. This issue should be reopened.
I dug into this again and am seeing a difference between client and server where I don’t know how to best handle it:
/page
directly, setting a cookie in its load function, that cookie is scoped to the/
path/page/
directly (note the trailing slash; set throughtrailingSlash: 'always'
), setting a cookie in its load function, that cookie is scoped to the/page
path/page
through a client-side navigation, setting a cookie in its load function, that cookie is scope to the/page
pathReason for the difference in behavior: it seems that the browser goes back to the last slash and uses that as the path. This means
/page
path ->/
/page/
path ->/path
/page/__data.json
->/path
I’m not sure about the best way forward here.
__data.json
mismatch)?basePath || '/'
after all? FWIW some frameworks also do this, .NET for example https://learn.microsoft.com/en-us/dotnet/api/system.net.cookie.path?view=net-6.0#remarks . PHP / Nuxt3 behave as we do currentlyI think I lean towards a combinations of option 1 and 2