kit
kit copied to clipboard
Cookie Management API
Describe the problem
Astro opened an RFC to introduce the new Cookie Store API to simplify setting, parsing and deleting cookies in routes: https://github.com/withastro/rfcs/discussions/182 I think it’s actually a good idea. Maybe we can implement this in Svelte Kit’s endpoints too.
Describe the proposed solution
The Cookie Store API has the advantage that it’s now the new standard API to work with cookies in the browser and that it works similar to the APIs used to get and set headers, which Svelte Kit is already using.
Alternatives considered
Don’t change anything and just let users set, parse and delete cookies by manually setting and reading headers, which is quite cumbersome.
Importance
nice to have
Additional Information
No response
I do like the idea of having a simple way to parse and serialize cookies built in (mainly so that it can be documented and people don't have to know about packages like cookie and cookie-signature, etc), but I don't love this API if I'm honest. It's deeply weird that await cookieStore.get(...) reads from the request but await cookieStore.set(...) writes to the response, and it would be a major conceptual break to have a way of manipulating the return value from an endpoint outside of the return itself.
Also, someone who wasn't familiar with the browser API would rightly wonder why these methods were async, when every other map-like interface is synchronous.
we can maybe expose cookie same way as fetch?
Btw I agree cookieStore.get() is ugly... I find getters and setters for this too "anoying".
we can maybe expose cookie same way as fetch?
What does this mean — making it global? We're not going to do that — fetch is global because it's a standard that's available in modern runtimes. And there's not much to be gained by adding it to event when it could just be imported as a normal module.
ok, so it will be importable from kit? like:
import { cookie } from '@sveltejs/kit/cookie';
for example, and kit will include it, so we don't need install it?
I was thinking along those lines, yeah. Perhaps something like
import { read, write, sign, unsign } from '@sveltejs/kit/cookie';
We went against using this Cookie Store API in Astro for similar reasons.
Since Svelte Kit now has a cookie API this issue can probably be closed?