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.

pass on cookies from server-side API calls on SSR response

See original GitHub issue

Environment


  • 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 : image

Directly by making a request to the API with Postman : image

Thanks !

Additional context

No response

Logs

No response

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
danielroecommented, Feb 13, 2022

I think there might be some misunderstanding here about the flow of cookies.

  1. Initial request (REQ1 and RES1) comes in from client, arrives at our Nitro server.

  2. 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.

  3. 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:

    If you want to allow the headers set on the response to useFetch to be added to the response from SSR, you’ll have to handle that yourself. (You could even write your own composable to do this.)

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.

2reactions
Atinuxcommented, Dec 21, 2021
Read more comments on GitHub >

github_iconTop 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 >

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