question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cookie can't be set together with redirect

See original GitHub issue

Environment

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:closed
  • Created a year ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
pi0commented, Apr 11, 2022

You are right. useCookie has a differed behavior that is not handling redirect. @danielroe will work on a fix shortly 😃

1reaction
Lanayxcommented, Apr 11, 2022

@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)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Sending browser cookies during a 302 redirect - Stack Overflow
Details: We sent HTTP 302 redirect with Set-Cookie header with "SameSite=Strict" policy and Location pointing at a different path of the same domain....
Read more >
Can not set a cookie and then redirect · Issue #4416 - GitHub
I am trying set a cookie on http://localhost:3000 which is the redirect URL assigned by the Env variable FRONTEND_URL .
Read more >
696204 - Cookies are ignored on 302 redirects - Monorail
A cookie is being set but ignored for some reason when they get set on a 302 redirect. If we call the /profile...
Read more >
Using .htaccess to set a cookie and 301 redirect
I tried adding the cookie after the 301. Ether way the 301 works but no cookie is passed. This is redirecting from maine-horse-trails.com...
Read more >
Cookies set with SameSite=strict are not sent in redirects
The situation is as follows: 1. User follows a link from domaina to domainb/first.html 2. domainb/first.html sets a samesite=strict cookie and redirects the ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found