I am finding the docs a little ambiguous. Given a list franchises, I would like to render out the relevant franchise detail pages at build time to avoid hitting the CMS/API at run time, as these don't change frequently.
However, it seems that even if the relevant pages are generated at build time using getStaticPaths
, they still require the calls in getStaticProps
to be executed at runtime. This defeats the purpose of generating static pages.
import {withLayout} from '../../components/layout';
import {getFranchises} from '../api/franchises';
const Franchise = (props) => {
console.info(props);
return (
<>
<h1>{props.name}</h1>
</>
);
};
export async function getStaticProps({params}) {
let data = await getFranchises();
let franchise = data.find(x => x.id === params.id);
return {
props: franchise
}
}
export async function getStaticPaths() {
let data = await getFranchises();
// Get the paths we want to pre-render based on posts
const paths = data.map(post => ({
params: {id: post.id},
}));
// We'll pre-render only these paths at build time.
return {paths, fallback: false}
}
export default withLayout(Franchise);
Perhaps, I am doing something wrong, but I really am looking for some docs/demo on how to generate static pages at build time using next build
that uses data from API at build time and does not require any further calls to populate props at runtime.
If a page has Dynamic Routes and uses getStaticProps , it needs to define a list of paths to be statically generated. When you export a function called getStaticPaths (Static Site Generation) from a page that uses dynamic routes, Next. js will statically pre-render all the paths specified by getStaticPaths .
Next. js also became one of the most popular Static Site Generators. In other words, it's one of the best frameworks now for building superfast and SEO-friendly Jamstack websites.
What is getStaticPaths () in Nextjs? - DEV Community What is getStaticPaths () in Nextjs? In the last article, we learn how to use getStaticProps () in the Next.js application. In Next.js, we can pre-render a page during build time. We generate all the HTML code and data in advance. And the data is later cached by the server.
You should use getStaticPaths if you’re statically pre-rendering pages that use dynamic routes and: The page must be pre-rendered (for SEO) and be very fast — getStaticProps generates HTML and JSON files, both of which can be cached by a CDN for performance
Using GetServerSideProps allows you to improve your SEO as in this method the data is rendered before it reaches the client. As the data is refreshed every time the user loads the page, they can view the updated information at all times. This concludes our discussion of the GetStaticProps and GetServerSideProps data fetching methods in Next.js.
I have read a few blogs and have found Next.js very intriguing in terms of its added functionality such as server-side rendering and generating static websites. To be honest, I have already started liking it enough and wanted to build a quick application using Next.js’ data fetching techniques.
Both getStaticProps
and getStaticPaths
run only at build time. That is the purpose of these functions. You are using it correctly.
In development mode next dev
, however, getStaticProps
and getStaticPaths
run on every request.
getStaticProps (Static Generation)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With