Understanding static site generation in NextJS 15 (App router)
Unanswered
Cuban Crocodile posted this in #help-forum
Cuban CrocodileOP
EDIT: Just created a project from scratch with the same code and it's working as expected, I'll pay a look on what differences (probably some libraries) are making my project to work in a different way
I'm doing a migration of some of my static pages to the new App Router and I'm facing some 'issues', I don't fully get how the static pages works on the new router, for example one of my pages is connected to Contentful and I followed the tutorials to achieve SSG, in the build the page is labeled as "(Static) prerendered as static content" but logging my "getData" function the page is still doing a call on the first load of the page, after the first load the page seems to be cached and I don't get the log again.
I also did a very simple page in order to test and I get the same behavior, is this normal for my scenario? Should I verify if SSG is working in a different way (not with the logs)?
This is the test page that I created to test, the getData function is called at build time generating the html in the .nexjs folder but also in the first load of the page:
Any comment will be highly appreciated, thanks in advance!
I'm doing a migration of some of my static pages to the new App Router and I'm facing some 'issues', I don't fully get how the static pages works on the new router, for example one of my pages is connected to Contentful and I followed the tutorials to achieve SSG, in the build the page is labeled as "(Static) prerendered as static content" but logging my "getData" function the page is still doing a call on the first load of the page, after the first load the page seems to be cached and I don't get the log again.
I also did a very simple page in order to test and I get the same behavior, is this normal for my scenario? Should I verify if SSG is working in a different way (not with the logs)?
This is the test page that I created to test, the getData function is called at build time generating the html in the .nexjs folder but also in the first load of the page:
async function getData() {
console.log("[TESTPAGE] getData"); // This is called on build time and on the first load of the page
const aboutPageData = await fetch(
"http://jsonplaceholder.typicode.com/posts",
{ cache: "force-cache" },
).then((response) => response.json());
return aboutPageData;
}
export default async function listPage() {
const data = await getData();
if (!data) {
return { notFound: true };
} else {
return (
<div>
{data.map((post: any) => (
<div key={post.id}>
<h2>{post.title}</h2>
<p>{post.body}</p>
</div>
))}
</div>
);
}
}
Any comment will be highly appreciated, thanks in advance!
9 Replies
Have you tried forcing the static generation?
export const dynamic = “force-static”
export const dynamic = “force-static”
Cuban CrocodileOP
Yes, @luis_llanes , I forgot to add that in my message, I get the same behavior adding "force-static"
Cuban CrocodileOP
Just created a project from scratch with the same code and it's working as expected, I'll pay a look on what differences (probably some libraries) are making my project to work in a different way
Is the Next.js version the same in both projects? I ask because when talking about caching behavior is important, the defaults have changed
also worth mentioning whether you have
dynamicIO
or some other experimental features enabled in one project but not in anotherasync stuff works different ways with
dynamicIO
enabledCuban CrocodileOP
I was looking in the differences between my apps and I just found that the project that I'm migrating is still working with React 18, I think that could be the problem, I'll update that and check again, thanks guys!
Cuban CrocodileOP
Well, I updated to React 19 and my only difference is that in my project I'm working with the app and pages router at the same time, I did a cleanup of the layout component in case something there was working in a weird way but even with a simple layout every page calls the data on the first load even being generated as static pages, I'm not sure what else can I try
Cuban CrocodileOP
Well, I finally found the problem:
The root cause was a next-i18next config in my nextjs config file, this library is required in for my pages in the Pages router but is generating a problem for the app router. For the pages router the build is storing the pages pregrenerated inside an "en" folder, this is not happening for the app router, so when I load a page for first time is generating this "en" folder with the new html file (the other file is outside this folder) and thats the reason why I get a new request for the data, I'll try to fix the issue but probably I'll create a new question with more accurate information
The root cause was a next-i18next config in my nextjs config file, this library is required in for my pages in the Pages router but is generating a problem for the app router. For the pages router the build is storing the pages pregrenerated inside an "en" folder, this is not happening for the app router, so when I load a page for first time is generating this "en" folder with the new html file (the other file is outside this folder) and thats the reason why I get a new request for the data, I'll try to fix the issue but probably I'll create a new question with more accurate information