Next.js Discord

Discord Forum

Vercel Build Error

Answered
Treeing Cur posted this in #help-forum
Open in Discord
Treeing CurOP
Hello guys, Im trying to deploy a next js project into vercel and im getting this build error:
SyntaxError: Unexpected token T in JSON at position 0
    at JSON.parse (<anonymous>)
    at parseJSONFromBytes (node:internal/deps/undici/undici:4553:19)
    at successSteps (node:internal/deps/undici/undici:4527:27)
    at fullyReadBody (node:internal/deps/undici/undici:1307:9)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async specConsumeBody (node:internal/deps/undici/undici:4536:7)
    at async I (/vercel/path0/.next/server/app/page.js:6:260876)
    at async W (/vercel/path0/.next/server/app/page.js:6:261888)


Im new to nextjs so im not really sure whats causing this issue:

im calling this fetch commands like this:

import { fetchPageInfo } from '@/utils/fetchPageInfo'
import { fetchProjects } from '@/utils/fetchProjects'
import { fetchSkills } from '@/utils/fetchSkills'
import { fetchExperience } from '@/utils/fetchExperience'
import { fetchSocials } from '@/utils/fetchSocials'

export default async function Home() {
  const pageInfo: PageInfo = await fetchPageInfo();
  const experience: Experience[] = await fetchExperience();
  const skills: Skill[] = await fetchSkills();
  const projects: Project[] = await fetchProjects();
  const socials: Social[] = await fetchSocials();

  return (
    <div className='bg-[rgb(36,36,36)] text-white h-screen snap-y snap-mandatory overflow-y-scroll overflow-x-hidden z-0 scrollbar scrollbar-track-gray-400/20 scrollbar-thumb-[#F7AB0A]'>
      <Header socials={socials}/>

      {/* Hero */}
      <section id="hero" className='snap-start'>
        <Hero pageInfo={pageInfo
// rest of code
Answered by Ray
you should move the code from route handler, and just run it inside the server component
View full answer

18 Replies

@Treeing Cur Hello guys, Im trying to deploy a next js project into vercel and im getting this build error: SyntaxError: Unexpected token T in JSON at position 0 at JSON.parse (<anonymous>) at parseJSONFromBytes (node:internal/deps/undici/undici:4553:19) at successSteps (node:internal/deps/undici/undici:4527:27) at fullyReadBody (node:internal/deps/undici/undici:1307:9) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async specConsumeBody (node:internal/deps/undici/undici:4536:7) at async I (/vercel/path0/.next/server/app/page.js:6:260876) at async W (/vercel/path0/.next/server/app/page.js:6:261888) Im new to nextjs so im not really sure whats causing this issue: im calling this fetch commands like this: import { fetchPageInfo } from '@/utils/fetchPageInfo' import { fetchProjects } from '@/utils/fetchProjects' import { fetchSkills } from '@/utils/fetchSkills' import { fetchExperience } from '@/utils/fetchExperience' import { fetchSocials } from '@/utils/fetchSocials' export default async function Home() { const pageInfo: PageInfo = await fetchPageInfo(); const experience: Experience[] = await fetchExperience(); const skills: Skill[] = await fetchSkills(); const projects: Project[] = await fetchProjects(); const socials: Social[] = await fetchSocials(); return ( <div className='bg-[rgb(36,36,36)] text-white h-screen snap-y snap-mandatory overflow-y-scroll overflow-x-hidden z-0 scrollbar scrollbar-track-gray-400/20 scrollbar-thumb-[#F7AB0A]'> <Header socials={socials}/> {/* Hero */} <section id="hero" className='snap-start'> <Hero pageInfo={pageInfo // rest of code
should be something fail to parse to json from your fetch function
@Ray should be something fail to parse to json from your fetch function
Treeing CurOP
import { PageInfo } from "@/typings";

export const fetchPageInfo = async () => {
    const res = await fetch(`${process.env.NEXT_PUBLIC_BASE_URL}/api/getPageInfo`, { next: { revalidate: 3600 } });
    const data = await res.json();
    const pageInfo: PageInfo = data.pageInfo;
    return pageInfo;
};
@Treeing Cur Hello guys, Im trying to deploy a next js project into vercel and im getting this build error: SyntaxError: Unexpected token T in JSON at position 0 at JSON.parse (<anonymous>) at parseJSONFromBytes (node:internal/deps/undici/undici:4553:19) at successSteps (node:internal/deps/undici/undici:4527:27) at fullyReadBody (node:internal/deps/undici/undici:1307:9) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async specConsumeBody (node:internal/deps/undici/undici:4536:7) at async I (/vercel/path0/.next/server/app/page.js:6:260876) at async W (/vercel/path0/.next/server/app/page.js:6:261888) Im new to nextjs so im not really sure whats causing this issue: im calling this fetch commands like this: import { fetchPageInfo } from '@/utils/fetchPageInfo' import { fetchProjects } from '@/utils/fetchProjects' import { fetchSkills } from '@/utils/fetchSkills' import { fetchExperience } from '@/utils/fetchExperience' import { fetchSocials } from '@/utils/fetchSocials' export default async function Home() { const pageInfo: PageInfo = await fetchPageInfo(); const experience: Experience[] = await fetchExperience(); const skills: Skill[] = await fetchSkills(); const projects: Project[] = await fetchProjects(); const socials: Social[] = await fetchSocials(); return ( <div className='bg-[rgb(36,36,36)] text-white h-screen snap-y snap-mandatory overflow-y-scroll overflow-x-hidden z-0 scrollbar scrollbar-track-gray-400/20 scrollbar-thumb-[#F7AB0A]'> <Header socials={socials}/> {/* Hero */} <section id="hero" className='snap-start'> <Hero pageInfo={pageInfo // rest of code
try comment out the fetch function, and only run once to see which is causing the error
oh
you fetching your own api?
Treeing CurOP
yes
you shouldn't do that in server component
you should move the code from route handler, and just run it inside the server component
Answer
so you don't need to create separate route handler for data fetching
Treeing CurOP
oh
export async function GET() {
  try {
    const pageInfo: PageInfo = await sanityClient.fetch(query);
    return NextResponse.json({ pageInfo } as Data, {status: 200});
  } catch (error) {
    console.error(error);
    return NextResponse.json({ message: 'Error fetching data' }, {status: 500});
  }
}



so i dont need a route and just call the server side logic directly on my component?
Treeing CurOP
i see
still a lot to learn 👀
it worked tho
thanks brother hope you have a nice day
np
cool