Confusion with generateStaticParams()
Answered
Treeing Cur posted this in #help-forum
Treeing CurOP
Hello guys, i just wanted some clarification regarding generateStaticParams(), even after reading the documentation i still cant get a grasp of it, currently the way im doing it is like this:
im not sure if the way im going about it is correct, also is it possible to include other variables in the params? for example, instead of previous way, i do it like this?:
import SingleAlbum from "@/components/album/SingleAlbum"
import { SingleAlbumInfo, Slug } from "@/typings"
import { fetchSlugs } from "@/utils/fetchAlbumSlugs"
import { fetchAlbumsWithImages } from "@/utils/fetchAlbums"
export async function generateStaticParams() {
const slugs: Slug[] = await fetchSlugs()
return slugs.map((slug) => ({
params: {
slug: slug.current
}
}))
}
const Albumpage = async ({ params }: any) => {
const album: SingleAlbumInfo = await fetchAlbumsWithImages(params.slug)
return (
<>
<SingleAlbum album={album}/>
</>
)
}
export default Albumpageim not sure if the way im going about it is correct, also is it possible to include other variables in the params? for example, instead of previous way, i do it like this?:
return slugs.map((slug) => ({
params: {
slug: slug.current
album: album
}
}))Answered by B33fb0n3
the generateStaticParams exists to pre define the url. So instead of having a dynamic segment like [id], you can define all pre rendered pages for that part. Like that these pages are directly build at build time. So yes, you can add more dynamic parts to it and yes, you can also pre define these dynamic parts via generateStaticParams
5 Replies
@Treeing Cur Hello guys, i just wanted some clarification regarding generateStaticParams(), even after reading the documentation i still cant get a grasp of it, currently the way im doing it is like this:
import SingleAlbum from "@/components/album/SingleAlbum"
import { SingleAlbumInfo, Slug } from "@/typings"
import { fetchSlugs } from "@/utils/fetchAlbumSlugs"
import { fetchAlbumsWithImages } from "@/utils/fetchAlbums"
export async function generateStaticParams() {
const slugs: Slug[] = await fetchSlugs()
return slugs.map((slug) => ({
params: {
slug: slug.current
}
}))
}
const Albumpage = async ({ params }: any) => {
const album: SingleAlbumInfo = await fetchAlbumsWithImages(params.slug)
return (
<>
<SingleAlbum album={album}/>
</>
)
}
export default Albumpage
im not sure if the way im going about it is correct, also is it possible to include other variables in the params? for example, instead of previous way, i do it like this?:
return slugs.map((slug) => ({
params: {
slug: slug.current
album: album
}
}))
the generateStaticParams exists to pre define the url. So instead of having a dynamic segment like [id], you can define all pre rendered pages for that part. Like that these pages are directly build at build time. So yes, you can add more dynamic parts to it and yes, you can also pre define these dynamic parts via generateStaticParams
Answer
Treeing CurOP
I see i see
thanks!
@Treeing Cur thanks!
happy to help. Please mark solution