NextJS rewrite not working with SSR

I seem to have found a bug when using NextJS rewrites in combination with SSR, when also using a catch-all directory path that uses ISR.

The situation: I have a folder in my /pages directory called [...pages]/index.js. The page component in that file uses getStaticProps (in combination with getStaticPaths) to render every url using ISR. This works like expected on Netlify (and on Vercel).

For specific URLs however, I want to use SSR because I need the query params for that URL. The way I do this is by adding that specific URL as a rewrite to next.config.js (eg: /render/ssr) and rewriting that to another path that also exists in /pages (eg: /dynamic-pages/[...pages]/index.js). Locally and on Vercel this specific URL renders SSR and I have access to the context.query. On Netlify however it does seem to render the component in /dynamic-pages/[...pages]/index.js but I don’t have access to any of the query params. Also when I look in the function logs I see that the logs for this page (which should appear in “Next.js SSR Handler” logs) appear in “Next.js ISR handler” logs.

I have created a demo project that illustrates the problem, you can find the git repository here: GitHub - terrabythia/netlify-rewrite-test: Testing netlify and nextjs rewrites in combination with ISR

I have deployed the project to Netlify and Vercel (exactly the same). When you go to any url you see the (correct) page that says “rendered using ISR” (netlify: https://netlify-rewrite-test-2.netlify.app/any/url, vercel: https://netlify-rewrite-test-eta.vercel.app/any/url). However, when you go to the url that is defined in next.js’ rewrites, you see that the netlify version does not contain any query params (https://netlify-rewrite-test-2.netlify.app/render/ssr?test=true) while the vercel version does (https://netlify-rewrite-test-eta.vercel.app/render/ssr?test=true). And again, the logs for the Netlify page that should be rendered using SSR appear in the ISR function logs.

Can anyone on the Netlify team verify that this is indeed a bug in Netlify?

update: just as a test I wanted to deploy the exact same app to another service than Vercel only (since it’s the creator of NextJS). I’ve deployed the app to render.com, which gives me the same (correct) results as Vercel (see: https://netlify-rewrite-test.onrender.com/any/url and https://netlify-rewrite-test.onrender.com/render/ssr?test=true). Netlify does seem to be the odd one out.

Would be great if this can be fixed on Netlify…

Hiya,

Thanks for sharing this information would you mind filing an issue in our repo so our devs can investigate? [Bug]: ISR Working Locally But Not Working on Netify · Issue #2127 · netlify/next-runtime · GitHub

Hi Sam,

Sure, I have created a new issue on the repo here: [Bug]: ISR in combination with nextjs rewrites not working · Issue #2129 · netlify/next-runtime · GitHub

2 Likes