I'm using next.js and the import { useRouter } from 'next/router'.
However on initial load, router.query.id is undefined. It quickly fills in, but that initial load is a killer.
I'm trying to figure out how to do it, and tried:
export async function getStaticProps({ params }) {
// params contains the post `id`.
// If the route is like /posts/1, then params.id is 1
// const res = await fetch(`https://.../posts/${params.id}`)
// const post = await res.json()
console.log(params)
// Pass post data to the page via props
return { props: { params } }
}
but this returns an error:
Error: getStaticPaths is required for dynamic SSG pages and is missing for '/app/d/[id]'.
I can't use getStaticPaths, since [id] is variable and can be any number of things.
So what's the best way to handle this?
getStaticPaths, since[id]is variable and can be any number of things."? If you mean that you don't know all theidahead of time and you want the pages to be static, you can use incremental static generation by specifyingfallback: truein the returned value ofgetStaticPaths.getStaticPropsfor data-fetching, you have to provide an array of paths usinggetStaticPathsfor pre-rendering the pages otherwise you will always get an error at build time.ids. That's correct. I don't actually care about data-fetching during build time. I just want to ensure that therouter.query.idis always there when the page loads.getServerSidePropswith some sort of caching strategy with a custom server so pages will be generated on a per-request basis.