pass on cookies from server-side API calls on SSR response
See original GitHub issueEnvironment
- Operating System:
Linux
- Node Version:
v16.13.1
- Nuxt Version:
3.0.0-27333874.279bfdc
- Package Manager:
yarn@1.22.17
- Bundler:
Vite
- User Config:
-
- Runtime Modules:
-
- Build Modules:
-
Reproduction
Currently no reproduction link is up to date with the latest version of Nuxt3 so I can’t help you
Describe the bug
Hello Nuxt 3 !
I have a problem, I try to make a session manager with redis (like express-session) but here I will try to make it in h3/nitro version, I don’t know if it is possible ?
setCookie
of h3 does not send the cookie to the client during the server side rendering.
However, it does send it if a request is executed directly on the api path.
server/api/hello.js
import { setCookie } from 'h3'
export default (req, res) => {
setCookie(res, "sessionId", "new session id");
return 'you are now connected ! (in reality not)';
}
hello.vue
const { data } = await useFetch("/api/hello", {
headers: useRequestHeaders(),
});
Browser that executes the request to the API in SSR :
Directly by making a request to the API with Postman :
Thanks !
Additional context
No response
Logs
No response
Issue Analytics
- State:
- Created 2 years ago
- Reactions:2
- Comments:9 (3 by maintainers)
Top Results From Across the Web
How pass cookie throw node server to REST API in Angular ...
So, in my node server I add cookie throw cookie-parser: let cookieParser = require('cookie-parser'); app. use(cookieParser());
Read more >Access COOKIES in NextJS from server side. | Reactjs | SSR
Accessing cookies in NextJS from server side could be tricky for ... reach out to me you can send me email or simply...
Read more >Next.js — Handling Cookies in getServerSideProps
I want to fetch my userds data on the server before rendering the page, meaning Idll first need to validate the access token,...
Read more >Server requests with cookies solved? : r/sveltejs - Reddit
Simply take the cookie off the request and pass it on in the headers. I was debugging my backend and noticed the cookies...
Read more >Using cookies - Postman Learning Center
Using the cookie manager ... To manage cookies in Postman, open a request, then select Cookies (under Send). ... The Manage Cookies window...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I think there might be some misunderstanding here about the flow of cookies.
Initial request (REQ1 and RES1) comes in from client, arrives at our Nitro server.
Whilst rendering the HTML for this, our server performs an internal fetch to another endpoint. We create a synthetic REQ2 and RES2 that this endpoint interacts with. It sets a cookie on RES2.
The internal fetch returns RES2 and we extract the data, then render the rest of the HTML. Now we’re ready to return a response to the user.
We don’t want to set all cookies on this response from all requests we made as part of the rendering cycle. They could include logged in state from protected API calls we’ve made, for example, but it is possible to set it manually. As I said previously:
I’ve created an example reproduction with an example composable to show how you might ‘pass on’ a cookie from a local fetch to the final SSR response.
I created a small sandbox, maybe it could help @pi0 @danielroe
https://stackblitz.com/edit/nuxt-starter-zi9z7u?file=pages%2Findex.vue