Home > OS >  NextJs deployment faild on getStaticPath function: Unexpected token < in JSON at position 0 at JS
NextJs deployment faild on getStaticPath function: Unexpected token < in JSON at position 0 at JS

Time:07-01

This is my function to get a path will be run in local dev mode

export async function getStaticPaths(){
    const datas = await fetch(`https://mydomain.vercel.app/api/annonces/annonces`)
    const annonces = await datas.json()

    const paths = annonces.map(item =>({
        params: {annonce: item._id}
    }))

    return {
        paths,
        fallback: false
    }
}

This is my error when I deploy my app

> Build error occurred
SyntaxError: Unexpected token < in JSON at position 0
    at JSON.parse (<anonymous>)

CodePudding user response:

Got the same error while deployment. I think it has to do with calling API routes created by nextjs in getStaticPaths and getStaticProps. Got it resolved after moving the api code to getStaticPaths and getStaticProps.

As per the documentation "This means that instead of fetching an API route from getStaticProps (that itself fetches data from an external source), you can write the server-side code directly in getStaticProps."

write-server-side-code-directly

  • Related