Comments (3)
@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.
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.
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)
- nextjs-flask example broken HOT 6
- Clarification on the "@acme-" Prefix in Multi-Zones Packages
- postgres-pgvector demo site not working HOT 1
- Web3 Authentication example is insecure HOT 1
- Next.js + Flask fails in production HOT 2
- Microfrontends example: Module not found: Can't resolve './example.module.css'
- feat(storybook): introduce to `/solution/monorepo`
- Bug: Wrong header in `https://app-dir-i18n.vercel.app/` HOT 1
- nextjs-flask: The flask api endpoint is not working. HOT 2
- Your location is undefined HOT 5
- PostgreSQL on docker and Next.js locally Error: connect ECONNREFUSED ::1:443 HOT 1
- Node.js Serverless Function template doesn't work (gives a 404) HOT 2
- Update deployment GitHub workflows
- bug
- Domain API Example broken
- Flask3 example doesn't work with additional dependencies
- Sonic example doesn't work
- https://flask2-python-template.vercel.app/ HOT 2
- postgres-drizzle got slow
- How to run Flask WSGI in Multithreaded Mode
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from examples.