question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SSG pages re-render if middleware is being used

See original GitHub issue

Verify 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

  1. Clone the repo
  2. Run npm run build
  3. Run npm run start
  4. In Chrome DevTools, go to Network tab -> Network conditions
  5. Turn on Disable cache, also set Network throttling to Slow 3G to see the issue clearly
  6. Open the project in chrome
  7. Once the page loads, quickly scroll down, once the page is loaded once, it will load again and will automatically scroll to top
  8. In the console, you can see render _app is logged twice
  9. Also if you run the project using npm run dev, you can check the chrome console to see the re-render
  10. A live version of the repo is available here: https://next-middleware-rerender.vercel.app/

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:13
  • Comments:22 (12 by maintainers)

github_iconTop GitHub Comments

9reactions
11koukoucommented, Jul 6, 2022

This should be addressed with high priority

4reactions
shudingcommented, Jul 7, 2022

Hi all! I’m going to verify if it’s safe to re-land #37431 and also add a test for this specific issue.

Hi @ijjk the PR above adds a validation on top of the existing feature to prevent this from happening. What is the root cause for the issue? Is it possible to have the root cause fixed instead?

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found