next.js: middleware rewrite to a SSR page ignored

What version of Next.js are you using?

12.0.2

What version of Node.js are you using?

14

What browser are you using?

Chrome

What operating system are you using?

macOS

How are you deploying your application?

Vercel

Describe the Bug

Rewrites in middleware only work if the URL is to an SSG page. When the rewritten URL points to an SSR or ISR page, it is ignored and works like NextResponse.next(). It does not show the content of the rewritten URL.

Am I not supposed to rewrite to an ISR or SSR page?

Expected Behavior

/hello to rewrite to /bye should show the content of /bye while maintaining the requested URL, /hello. However, it does not have an effect if /bye is an SSR or ISR page.

To Reproduce

in _middleware.ts NextResponse.rewrite('url-to-ssr-or-isr-page')

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 3
  • Comments: 15 (6 by maintainers)

Most upvoted comments

I am currently experiencing the same issue on a project i’m working on. The rewrite happens correctly when running locally on dev server. But no rewrite happens when deployed to Vercel.

Wrong button 😄

And it works on local but not on Vercel production.

I tried to reproduce the wrong behavior with this minimal setup.

to my eyes, in the latest Next.js version it’s working as expected, can you confirm @arclogos132 ?

Would you mind including a reproduction of this? It should work with SSR pages 🙏

Another weird behavior is that rewrite works fine when clicked through a Next.js’s <Link> or router.push(), but it doesn’t when directly accessed through typing on a browser’s address/search bar.