Skip to content
Advertisement

Getting TypeError on prerendering page in npm run build

I am prerendering this page on my Next.js application:

JavaScript

Using getStaticProps()

JavaScript

Using getStaticPaths()

JavaScript

When I run it locally it works fine but when I try to deploy it with npm run build it gives an error just for the title property only:

JavaScript

This is the part that confuses me as I don’t understand why the error is only on 1 property (postData.title) of the query and everything else loads fine.

I am using GraphQL to create the query:

JavaScript

I import this function through an api.js file and use the data in the getStaticProps() function.

Any help on this would be highly appreciated, I looked for solutions online but couldn’t find any that worked. Thanks!

Advertisement

Answer

When handling a dynamic page such as /blog/[slug].jsx you need to use getStaticPaths in addition to getStaticProps and router.isFallback? as you’re using in the returned jsx above.

getStaticPaths catches incoming possible paths — its behavior however is dependent on a fallback key (which can have the values true, false, or "blocking")

Blocking is identical to Server Side Rendering so it generates static HTML on demand for paths not returned by getStaticPaths. This is cached for future use so that this on demand generation only happens once per path not handled by your getStaticPaths function. If you set getStaticPaths to true then a small subset of dynamic paths will be rendered at build time and a loading indicator will be displayed to the user if a path they navigate to isn’t rendered during the initial build. Using true is useful for large e-commerce sites or sites with a large number of dynamic paths so that the build process doesn’t take a ridiculously long time to complete. Setting getStaticPaths to false will result in any path not rendered during the build process to result in a 404 error if a user navigates to it. Depending on your needs, any of the above methods could be most appropriate. That said, it is important to note that "blocking" does not require the use of router.isFallback at all. I also suggest looking into the benefits of utilizing revalidate with getStaticProps.

Here is an example of using getStaticPaths to catch incoming dynamic paths:

JavaScript

There are many approaches to filtering getStaticPaths, you can also use GetStaticPathsContext to catch incoming locales as well as the default locale (if applicable).

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement