Coder Social home page Coder Social logo

Comments (3)

lfades avatar lfades commented on April 27, 2024 1

@a6software I see. The cors function in the example expects a response to attach headers to, so not sending it one will end up with an error. You could change the implementation to work on a different way, this is just a general pattern we have for the examples. NextResponse.next() is also returning a Response object.

but I guess could be another middleware (not tried that yet)

Next.js won't allow nested middlewares in the coming future, only a root one.

To be honest, it's a bit weird that most of the examples in this repo return a response directly from the middleware, as I suspect that's not what most people are after doing.

You either return a response (which can be NextResponse.next()) or undefined, the example in the repo do both 👍

from examples.

lfades avatar lfades commented on April 27, 2024

Hi @rvmelkonian 👋

I just tested the demo locally and it's working for me, I don't know what exactly to look for, can you share more details about the issue? 🙏

Thank you!

from examples.

a6software avatar a6software commented on April 27, 2024

At a guess you've done what I did, which is to take the example middleware and try to make it not do what the example does.

export function middleware(req: NextRequest) {
  // `cors` also takes care of handling OPTIONS requests
  return cors(
    req,
    new Response(JSON.stringify({ message: 'Hello World!' }), {
      status: 200,
      headers: { 'Content-Type': 'application/json' },
    })
  )
}

But we don't want to return a response directly here, we want to call through to the next thing in the chain - which in my case is my actual API endpoint, but I guess could be another middleware (not tried that yet).

export async function middleware(req: NextRequest) {
  return cors(req, NextResponse.next());
}

That should fix it - at least it has done for me.

Why the TypeError: Cannot read properties of undefined (reading 'set') is because:

https://github.com/vercel/examples/blob/main/edge-functions/cors/lib/cors.ts#L101

Whatever you are passing as the res is not actually a response object... at least, it isn't an object with a Headers object at res.headers at any rate.

To be honest, it's a bit weird that most of the examples in this repo return a response directly from the middleware, as I suspect that's not what most people are after doing.

Still, very appreciative of the code - saved me a job :) thanks

from examples.

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.