Next.js Discord

Discord Forum

Double render of server component

Answered
B33fb0n3 posted this in #help-forum
Open in Discord
This is maybe a bug report about this behavior: https://nextjs-forum.com/post/1144282281796714517#message-1145336998861742150

Let me conclude it:
I have the following component as page, located at app dir -> anotherpage -> [[...page]] -> page.js:

export const revalidate = 120;

export const getPageData = cache(async () => {
    console.log("getPageDataFetchCalled")
    // const {data} = await API.graphql({query: listTodos});
    return {data: false};
})

export default async function Page({params}) {
    const data = await getPageData();

    return (
        <>
            <h1>Hello World!</h1>
            <p>{params.page[0]}</p>
        </>
    )
}

Now the weird thing: you can see, that I call const data = await getPageData(); inside of the component. The component is a server component by default, but the console.log message inside the function is called twice. Maybe the whole component is rendered twice and because of that also the function is called twice.

The double call is only in production and only on revalidation.

But why and how can I prevent this?
Answered by B33fb0n3
@joulev just want to keep you up to date, because you facing the same problem. This is currently an expected behavior...
https://github.com/vercel/next.js/issues/55003#issuecomment-1708082777
View full answer

13 Replies

Any idea? 🤔
good question, i face this problem too but since it doesn't do any harm to me i don't really care about it; but you may want to make a bug report
@joulev good question, i face this problem too but since it doesn't do any harm to me i don't really care about it; but you may want to make a bug report
I just checked the open issues and found one that looks nearly the same. The solution says, that it's because of react 18 and that they prepare a new feature for conditional mounting and reusables states...
https://github.com/vercel/next.js/issues/47145
But the components is serverside rendered, so it shouldn't be in this case, isn't it?
@joulev the issue body is too long so i didn't read it, but the comment by skworden is not related to our issue yeah
I think so too. I just created a new issue. Let's see if something happens...
@joulev just want to keep you up to date, because you facing the same problem. This is currently an expected behavior...
https://github.com/vercel/next.js/issues/55003#issuecomment-1708082777
Answer
i personally dont get too impacted by it (i can live with it), but hope they can resolve this soon
Yeah, hope so too ^^
Mossyrose gall wasp
any fix on these behavior, im facing the same problem and the page doesnt show consistent value especially i need to generate random number at built time in my dynamic route, and the page value show different number at initial load vs navigating through the route
@B33fb0n3 that sounds like a differnet behavior than in this post mentioned. You might want to open a post in <#1007476603422527558> Maybe it's an intended or solveable thing
Mossyrose gall wasp
Its seems like the same behavior IMO because when i console log inside the dynamic route page and layout it show that log twice and it happened at built time (production)

In my use case i have to generate static page inside dynamic route and the page itself contain new Date function and Math.random(), and for example at first load of /blog/a page the date show 12:00:01 and when i navigate back to the page itself using <Link> component the value change 12:00:03

Been figuring this out for days.