question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Setting fallback to true errors build

See original GitHub issue

Bug report

Describe the bug

Just updated to 9.4 to use getStaticPaths and getStaticProps because I’m trying to get around this issue #3294 but now I get the following errors when I do yarn build.

Error occurred prerendering page "/blog/post/[id]". Read more: https://err.sh/next.js/prerender-error:TypeError: Cannot read property '0' of undefined

Setting fallback to false gives me no build errors.

const Post = ({posts}) => {
    const post = posts[0];
    const router = useRouter();

    if(router.isFallback) {
        return (
            <Layout>
                <CenterContent>
                    Loading ...
                </CenterContent>
                <Footer />
            </Layout>
        )
    } 

    return (
            <Layout>
                <CenterContent>
                    <article>
                        <Header>
                            <h1>{post.title.rendered}</h1>
                        </Header>
                        {post.better_featured_image
                            ?   <Figure>
                                    <ImageResponsive {...post.better_featured_image} />
                                </Figure>
                            :   null}

                        <Main dangerouslySetInnerHTML={{__html: post.content.rendered}} />
                        
                    </article>
                </CenterContent>
                <Footer />
            </Layout>
    );
}

export async function getStaticPaths() {
    const res = await fetch(${process.env.blogApiLive}/posts);
    const posts = await res.json();
    const paths = posts.map(post => /blog/post/${post.slug});
    
    return { paths, fallback: true }
}

export async function getStaticProps(context) {
    const res = await fetch(${process.env.blogApiLive}/posts?slug=${context.params.id});
    const posts = await res.json();

    return { props: {posts} }
}

Am I missing something?

On a side note, in this release, fetch is now included but when I run yarn build without something like node-fetch or isomorphic-unfetch I get this error

ReferenceError: fetch is not defined at getStaticPaths (C:\projects\originnext\.next\server\static\DuTV4FL6_E6-lnBspZfBz\pages\blog\post\[id].js:2790:15

So it seems like a package to do fetch is still required?

System information

  • OS: [Windows]
  • Browser (if applies) [e.g. chrome, safari]
  • Version of Next.js: [e.g. 9.4.0]
  • Version of Node.js: [e.g. 10.16.3]

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:12 (2 by maintainers)

github_iconTop GitHub Comments

9reactions
ryanditjiacommented, Oct 18, 2020

@Joshanity17 I ran into the same issue and was able to fix it with a one-liner:

// setup
export async function getStaticProps(context) {
  const data = await ...

  return {
    props: {
      data
    }
  }
}

// the page component
export default function MyPage({ data }) {
  if (!data) return null // this is the one-liner check

  // rest of actual component code
}
4reactions
pnetttocommented, Oct 18, 2020

@Timer Hey Joe, I think you pointed out the correct answer to the problem. Nevertheless, Im wondering if Next is trying to render a page that doesn’t even exist during build (“/blog/post/[id]”)?

The getStaticPaths is rightly defining all possible paths at this point, but at build time there’s this weird path (“/blog/post/[id]”) that has a render attempt, breaking the build process.

This means that, in order to skip that attempt, we’d need to check if the props are ok, even though we know the props are ok for the paths we listed in getStaticPaths.

Just wondering if you have any thoughts on this or if that’s just how things are for the time being. Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

NextJS: Failed when fallback set to true - Stack Overflow
It is ok when fallback is set to false but I really like to set fallback set to true so that pages can...
Read more >
Next.js Tutorial - 27 - getStaticPaths fallback true - YouTube
Courses - https://learn.codevolution.dev/⚡️ Checkout Taskade! https://www.taskade.com/ Support UPI - https://support.codevolution.dev/  ...
Read more >
[Solved]-NextJS: Failed when fallback set to true-Reactjs
This function gets called at build time export async function getStaticPaths() { return { // Only `/posts/1` and `/posts/2` are generated at build...
Read more >
Data Fetching: getStaticPaths - Next.js
When next build is run, Next.js will check if getStaticPaths returned fallback: false , it will then build only the paths returned by...
Read more >
Troubles understanding `fallback: true` inside `getStaticPaths`
Well you kind of can make it so you don't have to build as much. If you have fallback set to true, when...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found