31
loading...
This website collects cookies to deliver better user experience
export async function getServerSideProps(context){
const res = await fetch(`https://...`)
const data = await res.json()
return {
props {}
}
}
getServerSideProps
, the data is fetched and rendered on each request - this takes place at the server. It is Server Side Rendered. I don't fully understand how that happens, so I won't try to explain it any further.getStaticProps
fetches all the data and serves it to your frontend using this function:export async function getStaticProps(){
const res = await fetch(`https://...`)
const data = await res.json()
return {
props { data }
}
}
getStaticPaths
to fetch the individual paths and render the pages matching them:export async function getStaticPaths() {
return {
paths: [
{ params: { ... } }
],
fallback: false,
};
}
getStaticPaths
fetches the paths(slugs) for all the pages you want to be made so Next knows how many pages it has to build and what content should be in each. The params from getStaticPaths
are passed to getStatcProps
to build the individual pages.NOTE: It is also possible to use getStaticPaths with getServerSideProps
getStaticProps
and get getStaticPaths
used in this way, work well for your static site they are not without their issues.getStaticProps
, if any of the existing content is changed or updated, nothing happens. Because the pages have already been built, any modifications will not reflect or render.getStaticProps
:export async function getStaticProps(){
return {
props: data,
revalidate: 1
}
}
revalidate
tells Next to wait a set number of seconds before regenerating the page content. This way, if we do make changes, they will be rendered. Keeping our site up to date. Gotcha: revalidate works on a page by page basis and only for pages that already exist.
return {
paths: [
{ params: { ... } }
],
fallback: false,
};
Blocking is beyond the scope of what I am trying to explain here - I just not in the mood for more confusion. Here are the docs
export async function getStaticPaths() {
return {
paths: [
{ params: { ... } }
],
fallback: true,
};
}
fallback
to true
, we ensure that any new pages we make are rendered and a visitor is not met with a 404 page unless that page truly doesn't exist.Gotcha: Whilst Next is trying to generate the new page, nothing is being shown onscreen. To avoid your visitor thinking that nothing is happening, add a skeleton page - as a placeholder - at the top of your page render:
export default function PostDetails({ post }) {
if (!post) return <Skeleton />
}
getServerSideProps
does.
getStaticProps
does by default.fallback
to true
in getStatcPaths
.