2

I am using vercel for NextJS and this is my setup in getStaticPaths

  const paths = posts.map((post) => ({
    params: { player: post.player, id: post.id },
  }))

  return { paths, fallback: true }

When I set the fallback to true, I have got this error in vercel:

21:55:01.736 info - Generating static pages (1752/1752) 21:55:01.736 > Build error occurred 21:55:01.739 Error: Export encountered errors on following paths: 21:55:01.739 /clip/[player]/[id]

It is ok when fallback is set to false but I really like to set fallback set to true so that pages can be updated frequently. Any help will be greatly appreciated...

2 Answers 2

2

Inside your /clip/[player]/[id].js file, you need to handle the fallback state when that page is being requested on-demand.

// pages/posts/[id].js
import { useRouter } from 'next/router'

function Post({ post }) {
  const router = useRouter()

  // If the page is not yet generated, this will be displayed
  // initially until getStaticProps() finishes running
  if (router.isFallback) {
    return <div>Loading...</div>
  }

  // Render post...
}

// This function gets called at build time
export async function getStaticPaths() {
  return {
    // Only `/posts/1` and `/posts/2` are generated at build time
    paths: [{ params: { id: '1' } }, { params: { id: '2' } }],
    // Enable statically generating additional pages
    // For example: `/posts/3`
    fallback: true,
  }
}

// This also gets called at build time
export async function getStaticProps({ params }) {
  // params contains the post `id`.
  // If the route is like /posts/1, then params.id is 1
  const res = await fetch(`https://.../posts/${params.id}`)
  const post = await res.json()

  // Pass post data to the page via props
  return {
    props: { post },
    // Re-generate the post at most once per second
    // if a request comes in
    revalidate: 1,
  }
}

export default Post

https://nextjs.org/docs/basic-features/data-fetching#fallback-true

Sign up to request clarification or add additional context in comments.

3 Comments

A few days ago I have changed it to fallback: 'blocking" and it works. i am not very sure what is the implication for using blocking. I think fallback blocking is a new feature of nextjs.
With blocking, when a request is made to a page that hasn’t been generated, Next.js will server-render the page on the first request. Future requests will serve the static file from the cache. The reason this worked for you is because you didn't have to chance anything on the page re: the loading state. Glad it's working!
Just need to handle fallback in the beginning of component which gets some data from getStaticProps function. You can use received data after fallback handling.
0

What I did was conditionally render my component. So, my component receives the object data and if I need to use a value from data, such as "title", I will do...

data?.title

Also, for my entire return component I will conditionally render it. For example...

{data !== undefined ? (
  <div className ='main-content'>
    <p> This is the content that I want rendered if data is defined </p>
  </div>
) : (
  <div className = 'fallback-content'>
    <p> This shows if data == undefined </p>
  </div>
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.