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

App router: Components in root layout rerender on navigation #52558

Open
1 task done
jk1x8 opened this issue Jul 11, 2023 · 15 comments
Open
1 task done

App router: Components in root layout rerender on navigation #52558

jk1x8 opened this issue Jul 11, 2023 · 15 comments
Labels
bug Issue was opened via the bug report template. Navigation Related to Next.js linking (e.g., <Link>) and navigation.

Comments

@jk1x8
Copy link

jk1x8 commented Jul 11, 2023

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: 18.16.0
      npm: N/A
      Yarn: N/A
      pnpm: N/A
    Relevant Packages:
      next: 13.4.10-canary.3
      eslint-config-next: N/A
      react: 18.2.0
      react-dom: 18.2.0
      typescript: 5.1.6
    Next.js Config:
      output: N/A

Which area(s) of Next.js are affected? (leave empty if unsure)

App Router, Routing (next/router, next/navigation, next/link)

Link to the code that reproduces this issue or a replay of the bug

https://github.com/jk1x8/App-router-issue

To Reproduce

  1. Clone the repo https://github.com/jk1x8/App-router-issue
  2. After installing dependencies run npm run build and npm start, then open the app and navigate between pages. You will see (in the console) that navbar component that is placed in root layout rerenders on every route change.
  3. Run npm run dev, open app and you will see no rerenders of navbar during navigation, in dev mode components placed in root layout don't change.

Describe the Bug

Client components in root layout are re-rendering on every navigation change in production mode. When React.memo is added it's even worse, the not only rerender but loose their state on route change. In dev mode everything is fine, they don't re-render when the page is changed. I have a client-component Navbar that is shared accros the whole app
image
and should not rerender on page change. In dev mode it work's fine but there are problems after building the app. React StrictMode is disabled.
image

Expected Behavior

Components, even client components placed in layout should not rerender on navigation.

Which browser are you using? (if relevant)

Chrome

How are you deploying your application? (if relevant)

No response

@jk1x8 jk1x8 added the bug Issue was opened via the bug report template. label Jul 11, 2023
@github-actions github-actions bot added the Navigation Related to Next.js linking (e.g., <Link>) and navigation. label Jul 11, 2023
@isoceles
Copy link

I'm noticing the same behavior.

@jondashkyle
Copy link

jondashkyle commented Sep 27, 2023

Likewise, also encountering this. Seems to directly contradict the purpose of the new app and layout convention!

Worth noting: using browser history doesn’t produce this error. It’s only when clicking a <Link />.

@adrimonasterios
Copy link

For anyone with the same problem. I was able to fix my issue with this article. Basically the useSearchParams hook on static pages makes those pages to be deopted into client side rendering.

@benokif
Copy link

benokif commented Nov 29, 2023

Issue still happening with the latest canary 14.0.4-canary.25.

For anyone with the same problem. I was able to fix my issue with this article. Basically the useSearchParams hook on static pages makes those pages to be deopted into client side rendering.

There is no useSearchParams in the layout of the issue repo.

@renchris
Copy link

I am also facing this issue on the latest stable Next version 14.0.4

@M7ilan
Copy link

M7ilan commented Dec 29, 2023

I have the same issue with the latest stable version, but instead of using Component I use parallel routes here's more if you want to try the problem https://nextjs-forum.com/post/1189895822251405382

@jk1x8 I tested the testing app you linked and I don't see any problems, the navbar render once on page reload but in navigation it dosn't re-render.

@phips28
Copy link

phips28 commented Feb 2, 2024

We have a similar issue:

An input field has the onChange => and there we set the value to the search param (router.replace).
After that the input loses focus, and I need to click into the input field again.

@moshedelivops
Copy link

Same issue in my navbar using:

'use client'
import { useRouter } from 'next/navigation'
import Link from 'next/link'

strict mode off

@afu-codemonday

This comment has been minimized.

@Mood-al
Copy link

Mood-al commented Apr 10, 2024

same issue and i tried to reprouce it on sandbox and same thing.
the client component is getting rerendered on link navigation.
here is the example : https://codesandbox.io/p/devbox/next-playground-h96q9t

@dev-nasrullah
Copy link

Same issue. When I try removing useEffects it works like dev

@mehrdad-shokri
Copy link

Facing the same problem, removed all usePathname, useParams, useSearchParams, still layout rerenders on the same page segment change

@felipedeboni
Copy link

I encountered a similar issue involving a component utilizing 'use client' and React.memo. Upon removing the memo, it worked as expected.

Unfortunately, identifying the issue required several hours of troubleshooting. Next team should take care of it, or at the very least, add some note to their docs.

@MyNameIsTakenOMG
Copy link

MyNameIsTakenOMG commented Jul 15, 2024

same here, I noticed this issue because I have a component inside which there is a useEffect hook in the root layout, and when I switched routes, the hook would get triggered which means the whole component would get re-rendered.

However, this just happened when I first visited those routes, but after it worked as expected. So, this is the default behavior or what? Because I was thinking the root layout component would just stay there, while only the {children} would get rendered

@ezbeazy
Copy link

ezbeazy commented Aug 27, 2024

Same here. Using useEffect or useState in a "use client" component, imported into layout.js, causes full reload of page/layout when navigating to root (app/page.js). But does not re-render navigating to or between sub-routes (about/page.js or contact/page.js).

project
└─ app
   ├─ about
   │  └─ page.js
   ├─ contact
   │  └─ page.js
   ├─ layout.js
   ├─ page.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue was opened via the bug report template. Navigation Related to Next.js linking (e.g., <Link>) and navigation.
Projects
None yet
Development

No branches or pull requests