nuxt icon indicating copy to clipboard operation
nuxt copied to clipboard

fix(nuxt): add cookies on server side fetching `useCookie`

Open kyumoon opened this issue 2 years ago โ€ข 3 comments

๐Ÿ”— Linked issue

resolves https://github.com/nuxt/nuxt/issues/22631

โ“ Type of change

  • [ ] ๐Ÿ“– Documentation (updates to the documentation, readme or JSdoc annotations)
  • [x] ๐Ÿž Bug fix (a non-breaking change that fixes an issue)
  • [ ] ๐Ÿ‘Œ Enhancement (improving an existing functionality like performance)
  • [ ] โœจ New feature (a non-breaking change that adds functionality)
  • [ ] ๐Ÿงน Chore (updates to the build process or auxiliary tools and libraries)
  • [ ] โš ๏ธ Breaking change (fix or feature that would cause existing functionality to change)

๐Ÿ“š Description

i set cookie using useCookie in plugins/{plugin}.ts ( update token to new value when token is expired ) and get cookie value using useCookie in pages/{page}.vue , it returns undefined because useCookie get value from event.node.req.headers.cookie, this value not set until next request

i think, 'cookie' should be accessible before the next request.

reproduce: https://github.com/kyumoon/nuxt-cookie-issue/blob/main/plugins/auth.server.ts

๐Ÿ“ Checklist

  • [ ] I have linked an issue or discussion.
  • [ ] I have updated the documentation accordingly.

kyumoon avatar Jul 04 '23 16:07 kyumoon

Thanks for this PR!

I think it makes sens that useCookie can read both request cookies and also to response cookies since in Nuxt this is a two-way (read + write) utility.

Partially implementing cookie handling logic and serialization in Nuxt is probably not the best idea, however. Especially since h3 updates optimized internals for header handling.

I think it would be best if we introduce parseResponseCookies + getResponseCookie composables in unjs/h3. Then we can use getResponseCookie first and then fallback to getCookie.

An alternative method would be caching cookies state in memory of nuxtApp and check it first place.

pi0 avatar Jul 12 '23 15:07 pi0

marking this as draft until we implement via https://github.com/unjs/h3/issues/509

danielroe avatar Sep 05 '23 12:09 danielroe