Skip to content
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

add new cookies API #6593

Merged
merged 8 commits into from
Sep 6, 2022
Merged

add new cookies API #6593

merged 8 commits into from
Sep 6, 2022

Conversation

Rich-Harris
Copy link
Member

@Rich-Harris Rich-Harris commented Sep 5, 2022

closes #6540

  • docs
  • update tests to read cookies with this API instead of from request headers

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpm changeset and following the prompts. All changesets should be patch until SvelteKit 1.0

@changeset-bot
Copy link

changeset-bot bot commented Sep 5, 2022

🦋 Changeset detected

Latest commit: cc6c56f

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@sveltejs/kit Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@Rich-Harris Rich-Harris changed the title add new cookies API - add new cookies API Sep 5, 2022
Copy link
Member

@dummdidumm dummdidumm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good, but still wondering if we should allow cookies in setHeaders. If you set some simple cookies, you don't need to reach for that, and it may have this "oh why can't I use the platform" feeling to it. More comments inline, but nothing blocking this from merging.

}
throw new Error(
`Use \`event.cookie.set(name, value, options)\` instead of \`event.setHeaders\` to set cookies`
);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of throwing here, couldn't we invoke something like cookies.set(cookie.parse(value)) here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could, but no-one wants to write this...

import * as cookie from 'cookie';

export const actions = {
  default: async ({ setHeaders }) => {
    // ...
    setHeaders({
      'set-cookie': cookie.serialize('answer', 42, {
        httpOnly: true
      })
    });
   }
};

...when they could write this:

export const actions = {
  default: async ({ cookies }) => {
    // ...
    cookies.set('answer', 42);
   }
};

I for one would be grateful for an error message that said 'you should delete a bunch of code, and you can probably remove the cookie dependency'.

It also feels very weird if you use setHeaders followed by cookies.get — it doesn't feel like it should work, and yet it does. Always better to have one way to do things.


for (const cookie of new_cookies) {
if (cookies.includes(cookie)) {
throw new Error(`"${key}" header already has cookie with same value`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this check is no longer done in the new version - any drawbacks omitting it and we should add it back, or is it fine like this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's harmless (if pointless) to duplicate cookies, the later one wins. To recreate this check we'd have to serialize eagerly, but it would only catch this fairly weird edge case — would probably make more sense to prevent different cookies, but I didn't want to get into questions about what to do with domain or path don't match, so I figured I'd just rely on people not being daft

});
}
```
You cannot add a `set-cookie` header with `setHeaders` — use the [`cookies`](/docs/types#sveltejs-kit-cookies) API in a server-only `load` function instead.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes me wonder if we should have a section "inputs exclusive to isomorphic load" and "inputs exlusive to server-only load" in here (can be done in a separate PR)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably yeah, since we haven't actually introduced RequestEvent by this stage

cookies.push(cookie);
}
throw new Error(
`Use \`event.cookie.set(name, value, options)\` instead of \`event.setHeaders\` to set cookies`

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should't this be event.cookies.set?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep. fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add a cookies interface to RequestEvent and LoadServerEvent
4 participants