How to generate SSG in next 14 with app router
Answered
Asiatic Lion posted this in #help-forum
Asiatic LionOP
I have some pages, which fetches data from database. The database data doesn't change. I am using server components. So how do I implement SSG here.
#help-forum
#help-forum
19 Replies
@Asiatic Lion I have some pages, which fetches data from database. The database data doesn't change. I am using server components. So how do I implement SSG here.
<#1007476603422527558>
unless you are in a dynamic segment (e.g.
if you are in a dynamic segment, you need
app/[slug]/page.tsx), static rendering (SSG) should be the default. https://nextjs.org/docs/app/building-your-application/rendering/server-components#static-rendering-defaultif you are in a dynamic segment, you need
generateStaticParams (and likely [export const dynamicParams = false](https://nextjs.org/docs/app/api-reference/file-conventions/route-segment-config#dynamicparams) too)Asiatic LionOP
@joulev but unfortunately I don't have default static rendering. I am fetching data from strapi database. It takes a lot of time to switch between pages and shows the loading page UI.
@Asiatic Lion <@484037068239142956> but unfortunately I don't have default static rendering. I am fetching data from strapi database. It takes a lot of time to switch between pages and shows the loading page UI.
you have to show your code, with this much info i can't guess anything
Asiatic LionOP
import { getData } from "@/lib/getData";
const About = async () => {
const data = await getData();
return (
<>
{data.map((item) => {
return (...);
})}
</>
);
};
export default About;@joulev this is the about page code. I am fetching some images and data which is shown here. When I navigate to home to "/about" page it takes a lot of time. If it was SSG I believe it would be instantaneous. For time delay I added a loading state.
@Asiatic Lion `
import { getData } from "@/lib/getData";
const About = async () => {
const data = await getData();
return (
<>
{data.map((item) => {
return (...);
})}
</>
);
};
export default About;
`
<@484037068239142956> this is the about page code. I am fetching some images and data which is shown here. When I navigate to home to "/about" page it takes a lot of time. If it was SSG I believe it would be instantaneous. For time delay I added a loading state.
this
/about looks good. is this dev mode or prod mode?Asiatic LionOP
@joulev prod anything I am missing so the SSG not working ?
@Asiatic Lion <@484037068239142956> prod anything I am missing so the SSG not working ?
do you use auth in your app or something like that?
@joulev do you use auth in your app or something like that?
Asiatic LionOP
No no. I will create a sandbox and share here later. If you could help.
sure, do share
English Lop
On static and dynamic routes just using
will enable SSG.
If you want to prerender the dynamic routes at build time then you'll have to pass the slugs also, like
You can verify this by checking
generateStaticParams() {
return []
}will enable SSG.
If you want to prerender the dynamic routes at build time then you'll have to pass the slugs also, like
export async function generateStaticParams() {
const posts = await fetch('https://.../posts').then((res) => res.json())
return posts.map((post) => ({
slug: post.slug,
}))
}You can verify this by checking
X-Nextjs-Cache: response headers.@joulev sure, do share
Asiatic LionOP
Hey @joulev Sorry for late response, I am sharing the repo and url as well please do see
https://ssg-my-app.vercel.app/
https://github.com/joseph-9900/ssg-my-app
https://ssg-my-app.vercel.app/
https://github.com/joseph-9900/ssg-my-app
Do this means already the pages are in static ? Then when is the use of
generateStaticParams()@Asiatic Lion Do this means already the pages are in static ? Then when is the use of `generateStaticParams()`
yes they are all already static. the app you posted above is fully static as expected
@Asiatic Lion Do this means already the pages are in static ? Then when is the use of `generateStaticParams()`
generateStaticParams() is when you use a dynamic segment, like
app/[foo]/page.tsx@joulev yes they are all already static. the app you posted above is fully static as expected
Asiatic LionOP
I jst added the api to check. How can I change this to static. And how to change the static to SSG. I saw in pages router it's being done.
Saw this in an youtube video using pages router. How to achieve the SSG on app router.
@Asiatic Lion I jst added the api to check. How can I change this to static. And how to change the static to SSG. I saw in pages router it's being done.
POST route handlers cannot be static. they are supposed to be run every request since they depend on the request.
@Asiatic Lion Saw this in an youtube video using pages router. How to achieve the SSG on app router.
you are already doing that. everything is already static.
Answer
Asiatic LionOP
Ohhh thankyou so much ❤️