Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[🐛 Bug]: Returned NextResponse from middleware is ignored #283

Closed
1 task
bharatari opened this issue May 27, 2023 · 3 comments · Fixed by #284
Closed
1 task

[🐛 Bug]: Returned NextResponse from middleware is ignored #283

bharatari opened this issue May 27, 2023 · 3 comments · Fixed by #284
Labels
bug Something isn't working

Comments

@bharatari
Copy link

next-on-pages environment related information

System:
Platform: linux
Arch: x64
Version: #1 SMP Fri Jan 27 02:56:13 UTC 2023
CPU: (12) x64 12th Gen Intel(R) Core(TM) i7-1255U
Memory: 8 GB
Shell: /bin/bash
Binaries:
Node: 18.15.0
Yarn: 1.22.19
npm: 9.5.0
pnpm: 8.3.1
Package Manager Used: npm
Relevant Packages:
@cloudflare/next-on-pages: 1.0.1
vercel: N/A
next: 13.4.4

Description

When a new NextResponse is returned from middleware, the response seems to be ignored. Instead of rendering what is returned from middleware, the route itself is rendered.

next dev (expected behavior):
image

wrangler (incorrect):
image

As of Next 13.1, this form of returning a new NextResponse from middleware should be valid:
https://nextjs.org/docs/app/building-your-application/routing/middleware#producing-a-response

Reproduction

Here is a repo demonstrating the issue: https://github.com/bharatari/middleware-error. I generated the app with create-next-app, added the edge runtime config to the page and added a middleware file.

If you run this app with wrangler, you'll see that the route is rendered instead of the response returned from the middleware.

With next dev, you'll see the middleware response rendered (as expected).

Pages Deployment Method

None

Pages Deployment ID

No response

Additional Information

Additional context in case it is helpful (although not necessary to debug the issue):

I noticed this while using the Next.js middleware from clerk/nextjs (an authentication provider). The middleware returns an interstitial page to refresh an expired JWT token, but with next-on-pages, this interstitial page isn't rendered even when it's returned from middleware. The app goes on to render the route even though authentication hasn't actually completed yet.

Would you like to help?

  • Would you like to help fixing this bug?
@james-elicx
Copy link
Contributor

Thank you very much for providing a reproduction and opening this bug report.

It looks like we didn't quite implement a certain scenario properly with how we handled middleware 🙁

I've just opened a PR that should address it (#284), and I would welcome you to try out the prerelease listed in that PR and check that it fixes your problem.

@bharatari
Copy link
Author

Hi @james-elicx, I tested the prerelease and it fixes the issue I was seeing. Thank you so much for the quick fix here!

@bilcus
Copy link

bilcus commented Jun 5, 2023

Thank you for the fix! Can confirm that the prerelease works:

npm install --save-dev https://prerelease-registry.devprod.cloudflare.dev/next-on-pages/runs/5123565621/npm-package-next-on-pages-284
npx @cloudflare/next-on-pages

(and adjusting the Pages build configuration)


This issue currently prevents Clerk from functioning at all on Cloudflare with errors like:

Error: Clerk: auth() was called but it looks like you aren't using `authMiddleware` in your middleware file. Please use `authMiddleware` and make sure your middleware matcher is configured correctly and it matches this route or page. See https://clerk.com/docs/quickstarts/get-started-with-nextjs

And various unauthorized and server errors during login flows. It can be tested using their started repo https://github.com/clerkinc/clerk-nextjs-starter

Hope this gets merged and released soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants