Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Next.js: getStaticProps and getStaticPaths with dynamic routes to generate static files

Tags:

next.js

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.

like image 219
Samuel Goldenbaum Avatar asked Apr 05 '20 03:04

Samuel Goldenbaum


People also ask

What is getStaticPaths in next JS?

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 .

Is Nextjs good for static?

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?

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.

When should I use getstaticpaths vs getstaticprops?

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

What are getstaticprops and getserversideprops?

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.

What do you like most about 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.


1 Answers

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)

like image 129
Nikolai Kiselev Avatar answered Oct 01 '22 00:10

Nikolai Kiselev