hoangvvo / next-connect

The TypeScript-ready, minimal router and middleware layer for Next.js, Micro, Vercel, or Node.js http/http2
https://www.npmjs.com/package/next-connect
MIT License
1.62k stars 65 forks source link

Can next-connect run on Nextjs middleware? #203

Closed gyto closed 1 year ago

gyto commented 1 year ago

As of nextjs v12.2.0 release, Vercel introduces a stable version of middleware.

I was wondering can next-connect be used in middleware itself. I did not try it yet, but it would be great to see some examples. Currently I have getServerSideProps extend next-connect authentication for every server-side pages. and it nightmare to control them because I already have 20+ pages for them. So I decided to switch to middleware to just match a single route and make everything in one spot.

I will test it later tonight and post my example here.

hoangvvo commented 1 year ago

It can. This is what the new edgeRouter is for: https://github.com/hoangvvo/next-connect#nextjs-edge-api-routes-beta. But instead of calling .handler(), you can call .run(). Will put together a quick example.

hoangvvo commented 1 year ago

I added an example to README: https://github.com/hoangvvo/next-connect#nextjs-middleware

gyto commented 1 year ago

Thanks @hoangvvo, I'll play around when I needed :)