Prerendering is failed on production while deploying to Vercel
Answered
Hassib posted this in #help-forum
HassibOP
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:
31 Replies
HassibOP
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?
are you fetching own api endpoint on the page?
HassibOP
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 />
</>
)
}HassibOP
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
HassibOP
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 functionHassibOP
Thank you, it worked! â¤ï¸
@Hassib Thank you, it worked! â¤ï¸
you're welcome
HassibOP
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 itHassibOP
I mean, Next.js fetch API has the
cache param, is the Prisma result fetching again and again on every request on the server?HassibOP
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
2. Using the
Is this correct? @Ray
1. Using the
cache of the React2. Using the
unstable_cacheIs this correct? @Ray
@Hassib 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? <@743561772069421169>
the cache from react doesn't cache the result
its mainly for deduplication
HassibOP
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?
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?
@Hassib 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?
yes it memoize the data request and its clear after the request
HassibOP
Ahh, the mystery in my mind has been solved.
So basically, the
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?@Hassib 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?
yes
unstable_cache will store the result to data cache which persists across deploymentslike the fetch cache option does
HassibOP
Okay, got it.
How can I declare the revalidation time in
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
}
);HassibOP
Can't thank you enough! â¤ï¸