Server components run twice during build and re-validation phase. #52934
Replies: 4 comments 7 replies
-
I think one collects HTML, the other collects RSC Props, or whatever that format is called. If you create a second page ( import Link from "next/link";
export default async function Page() {
const d = Date.now();
console.log("This is a server component running.", d);
return (
<>
Test component {d}
<br />
<Link href="/about">About</Link>
</>
);
} And // app/about/page.tsx
import Link from "next/link";
export default function About() {
return (
<>
<h1>About</h1>
<br />
<Link href="/">Home</Link>
</>
);
} And now build and run the app. Generating static pages (0/6)
This is a server component running. 1689853369870
This is a server component running. 1689853369912 Navigating directly to Test component <!-- -->1689853369870<br><a href="/about">About</a You can see it in the network tab when you arrive to 7:["Test component ",1689853369912,["$","br",null,{}],["$","$L8",null,{"href":"/about","children":"About"}]] That timestamp is the second log I got while building. |
Beta Was this translation helpful? Give feedback.
-
Any further update on this? The issue is with the data fetch, with two different renders, we can indeed have two different version although they are invoked very close to each other. And its also expensive, if we have a good range of dynamic parameters. Two data calls per SSG page. |
Beta Was this translation helpful? Give feedback.
-
This to me is a serious issue and should definitely be looked at. In the example below, I replace the data fetching by a random number. export const dynamicParams = false;
export const revalidate = 3600;
export const generateStaticParams = async () => [
{ collection: "one" },
{ collection: "two" },
];
export default async function Page({
params,
}: {
params: { collection: "one" | "two" };
}) {
const collection = params.collection;
console.log("Rendering my-page", collection);
const randomNumber = Math.random();
console.log("randomNumber", randomNumber);
return (
<div>
<h1>Page {collection}</h1>
<p>{randomNumber}</p>
<Link href="/my-page/one">One</Link>
<Link href="/my-page/two">Two</Link>
</div>
);
} Build log
If I navigate to page one then I see So no matter what I do will always get two sets of results/pages depending on where the routing originated at. |
Beta Was this translation helpful? Give feedback.
-
Same issue here with the following setup: "dependencies": {
"next": "15.0.0-rc.0",
"react": "19.0.0-rc-f994737d14-20240522",
"react-dom": "19.0.0-rc-f994737d14-20240522"
}, I created a test API which returned a random number. According to docs, the request is expected to be triggered only once when used separately both in a layout and in a page(with the same layout); but that's not what happens. The generated numbers in the layout and in the page are different. IDK if this is an issue with React or Next though. |
Beta Was this translation helpful? Give feedback.
-
Hi,
This topic is regarding the unclarity around the behaviour of app router's rendering which is noticed and mentioned on github issues(references at the bottom) quite a lot of time. Unfortunately there doesn't seem to be any definitive answers to it.
Before mentioning what this issue is, let me mention that this issue cannot be solved with disabling reactStrictMode.
The issue:
During the build and revalidation phase, all types of pages (SSG, SSR and ISR) have their code run twice.
This is specific to server components. Conversion to client components removes this issue.
A simple console.log statement of server component proves this.
Whenever you do 'npm run build', you can see each server component's log statement twice on the screen.
Code to test: (app/server_component/page.tsx)
Terminal output during build:
The timestamp for both the logs are different meaning they were called at different times.
As soon as "use client" directive is used, this issue seems to go away. This points that this is only related to server components.
The same behaviour is observed during revalidation of a server component(their code runs twice).
Last checked that this behaviour still exists with 13.4.10.
It would be really helpful if we could know that whether this is an expected behaviour of server components and what is the reason behind the behaviour.
Similar issues:
#45551
#44966
#44655
#43158
#48722
Beta Was this translation helpful? Give feedback.
All reactions