Next.js Discord

Discord Forum

Prerendering is failed on production while deploying to Vercel

Answered
Hassib posted this in #help-forum
Open in Discord
I have a next.js app which is developed using the App Router version of Next.js, everything is working perfect on local environment but when it's deployed on Vercel, it throws me the following error:
Answered by Ray
you shouldn't fetch route handler in server component
View full answer

31 Replies

SyntaxError: Unexpected token '<', "<!DOCTYPE "... is not valid JSON
    at JSON.parse (<anonymous>)
    at parseJSONFromBytes (node:internal/deps/undici/undici:5329:19)
    at successSteps (node:internal/deps/undici/undici:5300:27)
    at fullyReadBody (node:internal/deps/undici/undici:1447:9)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async specConsumeBody (node:internal/deps/undici/undici:5309:7)
    at async u (/vercel/path0/.next/server/app/departments/page.js:1:4571)
SyntaxError: Unexpected token '<', "<!DOCTYPE "... is not valid JSON
    at JSON.parse (<anonymous>)
    at parseJSONFromBytes (node:internal/deps/undici/undici:5329:19)
    at successSteps (node:internal/deps/undici/undici:5300:27)
    at fullyReadBody (node:internal/deps/undici/undici:1447:9)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async specConsumeBody (node:internal/deps/undici/undici:5309:7)
    at async Promise.all (index 1)
    at async _ (/vercel/path0/.next/server/app/page.js:6:3977)
SyntaxError: Unexpected token '<', "<!DOCTYPE "... is not valid JSON
    at JSON.parse (<anonymous>)
    at parseJSONFromBytes (node:internal/deps/undici/undici:5329:19)
    at successSteps (node:internal/deps/undici/undici:5300:27)
    at fullyReadBody (node:internal/deps/undici/undici:1447:9)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async specConsumeBody (node:internal/deps/undici/undici:5309:7)
    at async Promise.all (index 0)
    at async _ (/vercel/path0/.next/server/app/page.js:6:3977)
Error occurred prerendering page "/doctors". Read more: https://nextjs.org/docs/messages/prerender-error
@Hassib SyntaxError: Unexpected token '<', "<!DOCTYPE "... is not valid JSON at JSON.parse (<anonymous>) at parseJSONFromBytes (node:internal/deps/undici/undici:5329:19) at successSteps (node:internal/deps/undici/undici:5300:27) at fullyReadBody (node:internal/deps/undici/undici:1447:9) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async specConsumeBody (node:internal/deps/undici/undici:5309:7) at async u (/vercel/path0/.next/server/app/departments/page.js:1:4571) SyntaxError: Unexpected token '<', "<!DOCTYPE "... is not valid JSON at JSON.parse (<anonymous>) at parseJSONFromBytes (node:internal/deps/undici/undici:5329:19) at successSteps (node:internal/deps/undici/undici:5300:27) at fullyReadBody (node:internal/deps/undici/undici:1447:9) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async specConsumeBody (node:internal/deps/undici/undici:5309:7) at async Promise.all (index 1) at async _ (/vercel/path0/.next/server/app/page.js:6:3977) SyntaxError: Unexpected token '<', "<!DOCTYPE "... is not valid JSON at JSON.parse (<anonymous>) at parseJSONFromBytes (node:internal/deps/undici/undici:5329:19) at successSteps (node:internal/deps/undici/undici:5300:27) at fullyReadBody (node:internal/deps/undici/undici:1447:9) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async specConsumeBody (node:internal/deps/undici/undici:5309:7) at async Promise.all (index 0) at async _ (/vercel/path0/.next/server/app/page.js:6:3977) Error occurred prerendering page "/doctors". Read more: https://nextjs.org/docs/messages/prerender-error
https://nextjs-faq.com/fetch-api-in-rsc
are you fetching own api endpoint on the page?
I'm using Prisma and the built-in functionality of Next.js API:

import { NextResponse } from "next/server"
import prisma from "@/lib/prisma"

export async function GET(req) {
  const searchParams = req.nextUrl.searchParams
  const queryLimit = searchParams.get("limit")

  const limit = queryLimit ? Number(queryLimit) : undefined

  const service = await prisma.Service.findMany({
    take: limit,
    select: {
      id: true,
      name: true,
      imageUrl: true,
      excerpt: true,
    },
  })

  return NextResponse.json(service)
}
@Ray
And then I'm using the endpoint as below in the page.jsx file:

import Header from "@/components/Header"
import Hero from "@/components/Hero"
import Features from "@/components/Features"
import Departments from "@/components/Departments"
import Appointment from "@/components/Appointment"
import Doctors from "@/components/Doctors"
import Emergency from "@/components/Emergency"
import Footer from "@/components/Footer"

import { getLimitDoctors } from "@/services/doctor"
import { getLimitServices } from "@/services/service"

export default async function HomePage() {
  const [
    doctors,
    services,
  ] = await Promise.all([
    getLimitDoctors(),
    getLimitServices(),
  ])

  return (
    <>
      <Header />

      <main>
        <Hero />
        <Features />
        <Departments services={services} />
        <Appointment />
        <Doctors doctors={doctors} />
        <Emergency />
      </main>

      <Footer />
    </>
  )
}
The services mentioned are implemented like this:

export async function getLimitDoctors() {
  const response = await fetch(`${API_URL}/doctor?limit=7`, {
    method: "GET",
  })

  return response.json()
}
you shouldn't fetch route handler in server component
Answer
Okay, let me try that and will get back to you
@Hassib Okay, let me try that and will get back to you
basically, move the code from the route handler to getLimitDoctors function
Thank you, it worked! ❤️
@Hassib Thank you, it worked! ❤️
you're welcome
Just another quick question, does Next.js cache Prisma result?
@Hassib Just another quick question, does Next.js cache Prisma result?
no you would need to use unstable_cache with it
I mean, Next.js fetch API has the cache param, is the Prisma result fetching again and again on every request on the server?
So there are two options to cache the result when using a third-party library that doesn't support or expose fetch (for example, a database, CMS, or ORM client),

1. Using the cache of the React
2. Using the unstable_cache

Is this correct? @Ray
But the documentation says that it memoizes data requests,
https://nextjs.org/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating#example

Am I confused with the caching and memoizationing data request terms?
Ahh, the mystery in my mind has been solved.
So basically, the cache from react just memoizes the request data for each server request and the unstable_cache caches data and it works for multiple requests, right?
like the fetch cache option does
Okay, got it.
How can I declare the revalidation time in unstable_cache?
@Hassib Okay, got it. How can I declare the revalidation time in `unstable_cache`?
const getCachedUser = unstable_cache(
  async (id) => getUser(id),
  ['my-app-user'],
  {
    revalidate: 30 // 30 sec
  }
);
Can't thank you enough! ❤️