Cookie can't be set together with redirect
See original GitHub issueEnvironment
Nuxt CLI v3.0.0-27491748.3186841 11:06:50
RootDir: C:\Work\Test 11:06:56
Nuxt project info: 11:06:56
- Operating System:
Windows_NT
- Node Version:
v16.13.1
- Nuxt Version:
3.0.0-27491748.3186841
- Package Manager:
npm@8.1.2
- Builder:
vite
- User Config:
css
,build
,buildModules
,vite
,typescript
- Runtime Modules:
-
- Build Modules:
@pinia/nuxt@0.1.8
Reproduction
When using this code and performing SSR request (when this code is executed on server side)
useCookie('myCookie').value = '123';
await navigateTo('/test');
cookie “myCookie” is not set
Describe the bug
Setting cookie together with redirecting doesn’t work when executing SSR request
Additional context
A bit context why is this needed. When using external auth server it redirect to callback url together with the token (for example http://localhost:3000/loggedIn?accessToken=abcd
). I want to write down this token to the cookie and redirect user further (for example http://localhost:3000/dashboard
), so I need both set cookie and redirect functionality
Logs
On simple pages it just silently fails, in more complex case I see the following warning:
[nitro] [dev] [unhandledRejection] Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
Issue Analytics
- State:
- Created a year ago
- Comments:7 (4 by maintainers)
Top GitHub Comments
You are right.
useCookie
has a differed behavior that is not handling redirect. @danielroe will work on a fix shortly 😃@pi0 in my case the navigation is the last operation, that’s why I perceived it as a bug (I expected cookie header to be set at the point navigation starts)