31
loading...
This website collects cookies to deliver better user experience
Article originally published on Tinloof.
getServerSideProps
to fetch pages data from the CMS at every request.export default function HomePageContainer({ data }) {
return (
<Layout>
<HomePage data={data} />
</Layout>
);
}
// Called on the server after each request
export async function getServerSideProps() {
try {
const data = await fetchHomeDataFromCMS();
return {
props: { data },
};
} catch (error) {
console.error("Error fetching homepage data", error);
}
}
getServerSideProps
to get data before rendering. Next.js made these pages static by default.getStaticProps
that allows to fetch the data and render the page at build time. This would create static pages that load instantly.export default function HomePageContainer({ data }) {
return (
<Layout>
<HomePage data={data} />
</Layout>
);
}
// Called at build time
export async function getStaticProps() {
try {
const data = await fetchHomeDataFromCMS();
return {
props: { data },
};
} catch (error) {
console.error("Error fetching homepage data", error);
}
}
Note: if you prefer video format, here's a video explaining this section.
getStaticProps
is run by the Next.js server in the background.getStaticProps
is different from the previous run because the CMS data changed, the stale page is replaced by an updated one.revalidate
property to 3600
to revalidate the page every hour.export default function HomePageContainer({ data }) {
return (
<Layout>
<HomePage data={data} />
</Layout>
);
}
// Called at build and run-time
export async function getStaticProps() {
try {
const data = await fetchHomeDataFromCMS();
return {
props: { data },
// Revalidates the page every hour
revalidate: 60 * 60,
};
} catch (error) {
console.error("Error fetching homepage data", error);
}
}
/[category]
), we were able to generate a static page for each possible parameter by using the getStaticPaths
method:import categories from "../categories";
export default function CategoryPageContainer({ data }) {
return (
<Layout>
<CategoryPage data={data} />
</Layout>
);
}
export async function getStaticProps({ params: { category } }) {
try {
const data = await fetchCategoryDataFromCMS(category);
return {
props: { data },
revalidate: 1,
};
} catch (error) {
console.error("Error fetching homepage data", error);
}
}
export async function getStaticPaths() {
const categories = await fetchCategoriesFromCMS();
return {
paths: categories.map((category) => ({
params: { category },
})),
};
}
meta-data
tags required to show a card preview snippet on the social media platforms.meta-data
tags since they are only added once the modal appears in the client.meta-data.
The rest of the page is rendered client-side.export default function PostPage({ postData }) {
const [homeData, setHomeData] = React.useState({});
React.useEffect(() => {
fetchHomeDataFromCMS().then(setHomeData);
}, []);
return (
<>
<Layout>{!homeData ? null : <HomePage data={homeData} />}</Layout>
<PostModal data={postData} />
</>
);
}
export async function getStaticProps({ params: { postId } }) {
const postData = await fetchPostDataFromCMS(postId);
try {
return {
props: { postData },
revalidate: 60 * 60,
};
} catch (error) {
console.error("Error fetching post data", error);
// Fallback to 404 page in case of error
return { notFound: true };
}
}
// Nothing is generated at build time
export async function getStaticPaths() {
return {
paths: [],
fallback: "blocking",
};
}
fallback
to blocking
in getStaticPaths
to only return the page once it has finished loading. You can read more about the other fallback
possibilities Next.js offers here.meta-data
tags are available immediately in the HTML response.