SSG pages re-render if middleware is being used
See original GitHub issueVerify canary release
- I verified that the issue exists in the latest Next.js canary release
Provide environment information
Operating System: Platform: win32 Arch: x64 Version: Windows 10 Home Binaries: Node: 14.18.0 npm: N/A Yarn: N/A pnpm: N/A Relevant packages: next: 12.2.1-canary.2 eslint-config-next: 12.2.0 react: 18.2.0 react-dom: 18.2.0
What browser are you using? (if relevant)
Chrome 103.0.5060.66
How are you deploying your application? (if relevant)
next start, Vercel
Describe the Bug
By using the new stable middleware.js at the root of the project, all pages will render twice. With the old _middleware.js, there was no similar problem. Please note that if you rename or remove middleware.js from root, it works without any issues.
Expected Behavior
Pages render only once
Link to reproduction
https://github.com/imangm/nextjs-middleware-rerender
To Reproduce
- Clone the repo
- Run
npm run build
- Run
npm run start
- In Chrome DevTools, go to Network tab -> Network conditions
- Turn on Disable cache, also set Network throttling to Slow 3G to see the issue clearly
- Open the project in chrome
- Once the page loads, quickly scroll down, once the page is loaded once, it will load again and will automatically scroll to top
- In the console, you can see render _app is logged twice
- Also if you run the project using
npm run dev
, you can check the chrome console to see the re-render - A live version of the repo is available here: https://next-middleware-rerender.vercel.app/
Issue Analytics
- State:
- Created a year ago
- Reactions:13
- Comments:22 (12 by maintainers)
Top Results From Across the Web
Advanced Features: Middleware - Next.js
Middleware allows you to run code before a request is completed, then based on the incoming request, you can modify the response by...
Read more >Error: getStaticPaths is required for dynamic SSG pages and ...
You're rendering a dynamic route so use getServerSideProps() instead of getStaticProps().
Read more >Incremental Static Regeneration with Next.js - LogRocket Blog
Next.js v9.5 introduces Incremental Static Regeneration, a hybrid version of SSG and SSR, which can regenerate static pages during runtime.
Read more >Incremental Static Generation - Patterns.dev
Static Generation (SSG) addresses most of the concerns of SSR and CSR but is ... The 404 error page was shown here as...
Read more >SSR vs SSG in Next.js – a practical overview for CTOs and devs
your page is much faster than a server-side rendered one because the entire logic was executed at build time; your backend serves only...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This should be addressed with high priority
Hi all! I’m going to verify if it’s safe to re-land #37431 and also add a test for this specific issue.
The reason why it was happening in the first place is we can’t easily tell if there’s gonna be a query or not, when accessing to certain routes. If there’s a query, a re-render to update the route state is necessary to avoid hydration mismatch issues.