Slow initial load on SSG webpages with App Router #77286
Replies: 3 comments 3 replies
-
Can you show some examples/snippets of your current implementation? I'm not sure what you're currently doing, but I'd recommend pre-building all pages during build time to avoid generating routes on-demand. If you have thousands of routes, you could pre-build only the most visited pages and generate less frequently visited routes on-demand as a trade-off to reduce your overall build times. |
Beta Was this translation helpful? Give feedback.
-
Thank you for helping. Build time is not a concern. The website isn't huge for now, but even if it is, we want every page built and ready. Here's a few example of what we're doing: Some pages use generateStaticParamsexport const dynamicParams = false; // or false, to 404 on unknown paths
// export const dynamic = "force-static";
export async function generateStaticParams() {
const generatedParams = await Promise.all(
locales.map(async (locale: string) => {
const posts = await getAllThingsToDoPostData({ locale });
return posts.map((post: IThingsToDoSinglePost) => {
return {
locale: locale,
citySlug: strToSlug(post.sidebarDestination.city),
countrySlug: strToSlug(post.sidebarDestination.country),
};
});
})
);
return generatedParams.flat();
} When creating pagesIn the function building the content of the page, we call functions using async/await. const post = await getSingleThingsToDoPostData({
slug: thingsToDoSlug,
locale: locale,
}); We request Next.js to force cache for all our requestsconst res = await fetch(API_URL, {
method: "POST",
[…]
cache: "force-cache",
next: {
revalidate: 3600, // 1 hour
},
}); We checked and we don't receive a Edit: We've done testing, and it looks like while the page gets faster after the first load, it's only for a while. After an undetermined amount of time, the cache appears to be invalidated and page load gets slower once again. |
Beta Was this translation helpful? Give feedback.
-
OK, we've solved it. Setting export const dynamic = 'error' to all files and then fixing all the errors that appeared did the trick. We then tested the website using next build & next start and everything seems to be static! We're alright now 😀 |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Summary
Hello,
I have a Next.js in production (15.1.5). My website uses App Router and I'd like it to be as fast as possible for SEO reasons. On one of our previous website, we were using Gatsby and we blissfully enjoyed the amazing performance of a fully static website. But this dream has come to an end with Next.js.
Here's an extract of the build log of our website:
Every page labeled as SSG has a slow initial load, ranging from 1 to 15 seconds depending on the complexity of the page. That initial load destroys our performance metrics, and if Google were to stumble upon a page that loads that slow, it will surely penalize SEO.
We need our pages to be cached during build time. But they are making API calls that need to be cached themselves. And these calls should be invalidated when their own cache expires (we've defined different values depending on the request). So a mix of SSG and ISR if you would.
Basically, I have the exact same problem as the OP of this Reddit post. And as you can see, there haven't been many satisfying answers besides crawling the website to trigger this initial delay.
While I'm definitely considering this solution, I'd like to know if I'm doing anything wrong. Does anyone have any idea on what I should be doing to avoid that issue?
I can provide more details if needed. Thank you in advance!
Additional information
No response
Example
No response
Beta Was this translation helpful? Give feedback.
All reactions