-
Notifications
You must be signed in to change notification settings - Fork 27.6k
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
[NEXT-1220] layout.js rendering twice in production #49115
Comments
Hey Josh, I can't open the sandbox, happy to take a look. Without looking at the code I'm assuming what you're seeing is the preloading behavior, which will only end up with a single render but it's not guaranteed that React only ever renders once anyway because of Suspense boundaries.
All expensive operations should leverage |
Hi @timneutkens! Ahh that's super helpful, thanks for the context! In case it's useful, here's the code from that codesandbox, in the root export default function RootLayout({ children }) {
console.log("Render", Date.now());
return (
<html lang="en">
<body>{children}</body>
</html>
);
} I see two logs firing 1-2 milliseconds apart when refreshing the page. RE: preloading behavior, do you happen to know where I can learn more about it? Tried googling but I couldn't find anything. Although I realize all this stuff is in beta / docs might not exist yet. |
I have same issue
|
I'm facing the exact same issue! i tried removing everything and kept it as basic code exactly as you provided, but it keeps rendering twice (not because of strict mode, i have turned this off already). I'm using next-auth, but i did remove the session provider and everything else that could affect this behavior. any luck finding a resolution to your issue? |
@joshwcomeau I was wondering if you did find your issue.. 👀 |
This is caused by the root not-found boundary being applied by default, so it renders the layout around the not-found boundary in order for it to render correctly when notFound() is called in the root layout under certain conditions. This can be improved in the future so I've synced it into our issue tracker. |
Any updates on this? |
This comment was marked as spam.
This comment was marked as spam.
Introduce a new way to search for `not-found` component that based on the request pathname and current loader tree of that route. And we search the proper not-found in the finall catch closure of app rendering, so that we don't have to pass down the root layout to app-router to create the extra error boundary. This ensures the root layout doesn't have duplicated rendering for normal requests Fixes NEXT-1220 Fixes #49115
I think the PR that fixed this (#52589) may have removed the layout in our Copying my comment over here if it's helpful for anyone: Hmm... wonder if this is causing the layout to be missing on the Before ( After ( |
Opened an issue to report the problem of |
This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
Verify canary release
Provide environment information
Operating System: Platform: darwin Arch: arm64 Version: Darwin Kernel Version 22.4.0: Mon Mar 6 20:59:28 PST 2023; root:xnu-8796.101.5~3/RELEASE_ARM64_T6000 Binaries: Node: 16.17.0 npm: 8.15.0 Yarn: 1.22.19 pnpm: N/A Relevant packages: next: 13.3.5-canary.3 eslint-config-next: 13.3.4 react: 18.2.0 react-dom: 18.2.0
Which area(s) of Next.js are affected? (leave empty if unsure)
App directory (appDir: true)
Link to the code that reproduces this issue
https://codesandbox.io/p/sandbox/layout-render-twice-u24kxo
To Reproduce
Describe the Bug
The root
layout.js
will render twice on the server for every request, when using dynamic routing (const dynamic = 'force-dynamic';
). This happens even though it's a Server Component (no"use client"
directive).My first thought was that this was Strict Mode related, but the double-render occurs even when disabling Strict Mode in
next.config.js
, as well as when serving a production build.This is happening in the root
layout.js
, as well as any components rendered withinlayout.js
. It does not seem to be happening inpage.js
.Expected Behavior
Each request should generate a single render, so that any expensive operations (eg. database calls) only fire once.
EDIT: with further testing, I notice that these two renders are done in parallel, so it wouldn't actually slow the response, but it does cause problems if there are side effects (eg. a hit counter that registers every render).
Which browser are you using? (if relevant)
No response
How are you deploying your application? (if relevant)
No response
NEXT-1220
The text was updated successfully, but these errors were encountered: