Giter VIP home page Giter VIP logo

Comments (6)

balazsorban44 avatar balazsorban44 commented on May 22, 2024 1

Correct, this is now confirmed not to be a bug in edge-runtime. I forwarded this to the responsible Vercel team. (Vercel itself does not use the edge-runtime package, but the intention is that the implementation is identical).

next-auth will have to update to getSetCookie as well, and once it's fixed on Vercel, everything should work as expected.

I'm closing this issue then. Thanks!

from edge-runtime.

Pieparker avatar Pieparker commented on May 22, 2024 1

@balazsorban44 Is there anywhere that interested parties can follow along with the progress of the bug fix (a public issue, or a commitment that an update will be shared somewhere)?

from edge-runtime.

balazsorban44 avatar balazsorban44 commented on May 22, 2024

Hi, from the looks of it, I believe that this works as-per spec:

https://github.com/edenstrom/vercel-set-cookie-header/blob/c91a21a0e90c282b2144b84f7524902a2c9ea853/src/app/api/edge/route.ts#L11

Here, .get() will always return a string, see https://fetch.spec.whatwg.org/#dom-headers-get

.getSetCookie() will return an array instead, and should be used when trying to access a list of set-cookie headers. 🤔

splitCookiesString is essentially doing the same as .getSetCookie(), but is not built into the platform, while .getSetCookie() is a web standard now, and should be preferred. See https://fetch.spec.whatwg.org/#dom-headers-getsetcookie

from edge-runtime.

balazsorban44 avatar balazsorban44 commented on May 22, 2024

The spec also says that .set():

Replaces the value of the first header whose name is name with value and removes any remaining headers whose name is name.

So you might need to do the for-each loop as in https://github.com/edenstrom/vercel-set-cookie-header/blob/c91a21a0e90c282b2144b84f7524902a2c9ea853/src/app/api/parseEdge/route.ts#L21 instead of https://github.com/edenstrom/vercel-set-cookie-header/blob/c91a21a0e90c282b2144b84f7524902a2c9ea853/src/app/api/edge/route.ts#L18

Combining this info, I think the correct way to write this would be:

import { NextRequest, NextResponse } from "next/server"

export const runtime = "edge"

export const GET = async (req: NextRequest) => {
  const url = `${req.nextUrl.origin}/api/cookie`
  const sessionResponse = await fetch(url, { credentials: "include" })
  const setCookies = sessionResponse.headers.getSetCookie() ?? []
  const response = NextResponse.json({
    route: "success. Returns multiple Set-Cookie headers"
  })

  for (const cookie in setCookies) {
    response.cookies.set("set-cookie", cookie)
  }

  return response
}

from edge-runtime.

balazsorban44 avatar balazsorban44 commented on May 22, 2024

Related microsoft/TypeScript#55270

from edge-runtime.

edenstrom avatar edenstrom commented on May 22, 2024

The spec also says that .set():

Replaces the value of the first header whose name is name with value and removes any remaining headers whose name is name.

So you might need to do the for-each loop as in edenstrom/vercel-set-cookie-header@c91a21a/src/app/api/parseEdge/route.ts#L21 instead of edenstrom/vercel-set-cookie-header@c91a21a/src/app/api/edge/route.ts#L18

Combining this info, I think the correct way to write this would be:

import { NextRequest, NextResponse } from "next/server"

export const runtime = "edge"

export const GET = async (req: NextRequest) => {
  const url = `${req.nextUrl.origin}/api/cookie`
  const sessionResponse = await fetch(url, { credentials: "include" })
  const setCookies = sessionResponse.headers.getSetCookie() ?? []
  const response = NextResponse.json({
    route: "success. Returns multiple Set-Cookie headers"
  })

  for (const cookie in setCookies) {
    response.cookies.set("set-cookie", cookie)
  }

  return response
}

Hey! Yeah, you're right that it's according to spec. Just a weird use-case when it's server-to-server. 😅

Sadly there's no support for .getSetCookie in Vercel Edge Runtime yet. I've added a new route + button for that in the reproduction above. Returns 500 with the error TypeError: K.headers.getSetCookie is not a function. Works locally, but not deployed. (I guess the local version is a Node-based wrapper?)


Related:

The code in the edge-compatible next-auth should probably handle this use-case as well then? Wait for .getSetCookie or go with @edge-runtime/cookies?

https://github.com/nextauthjs/next-auth/blob/ffd055339b408475ef9acd780a71d85762e06311/packages/next-auth/src/lib/index.ts#L207-L208

from edge-runtime.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.