fix(nuxt): add cookies on server side fetching `useCookie`
๐ 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.
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.
marking this as draft until we implement via https://github.com/unjs/h3/issues/509