kit icon indicating copy to clipboard operation
kit copied to clipboard

Cookie Management API

Open hibiscuscoffee opened this issue 3 years ago • 6 comments

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

hibiscuscoffee avatar Apr 28 '22 01:04 hibiscuscoffee

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.

Rich-Harris avatar Apr 28 '22 13:04 Rich-Harris

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".

Mlocik97 avatar Apr 28 '22 17:04 Mlocik97

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.

Rich-Harris avatar Apr 28 '22 18:04 Rich-Harris

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?

Mlocik97 avatar Apr 28 '22 18:04 Mlocik97

I was thinking along those lines, yeah. Perhaps something like

import { read, write, sign, unsign } from '@sveltejs/kit/cookie';

Rich-Harris avatar Apr 28 '22 20:04 Rich-Harris

We went against using this Cookie Store API in Astro for similar reasons.

matthewp avatar Aug 08 '22 16:08 matthewp

Since Svelte Kit now has a cookie API this issue can probably be closed?

ghost avatar Oct 31 '22 20:10 ghost