Next.js Discord

Discord Forum

Error occured prerendering page

Answered
Transvaal lion posted this in #help-forum
Open in Discord
Transvaal lionOP
I'm building for production and I receive this error for several of my API routes.

Error occurred prerendering page "/api/employers/jobs/candidate_packages/list". Read more: https://nextjs.org/docs/messages/prerender-error
TypeError: Cannot read properties of undefined (reading 'headers')


These are what I have inside src/app/api/employers/jobs/candidate_packages/list/route.ts

import { employerJobCandidatePackageList } from "@/lib/constant/employerRouteList";
import { serverFetch } from "@/lib/helper/fetch";
import { cookies } from "next/headers";

export async function GET() {

    try {
        const cookieStore = cookies();
        const token = cookieStore.get('employerAccessToken');

        if (!token?.value) {
            return new Response('Unauthorized', {
                status: 401,
            })
        }

        const response = await serverFetch(employerJobCandidatePackageList, {
            token: token?.value,
            config: {
                method: 'GET'
            }
        })

        return new Response(response)

    } catch (error: any) {

        if (error?.response?.data) {
            new Response(error.response.data)
        } else {
            new Response(error)
        }
    }
}


Anyone able to identify the issue? Why is the compiler attempting to prerender an API route?
Answered by Transvaal lion
I found the issue to my errors. in my API routes in the catch block I did not return the new Response(). 🤦‍♂️
View full answer

6 Replies

Transvaal lionOP
I saw this issue on the repo and added the cache to no-store but I'm still receving the prerender error.

https://github.com/vercel/next.js/issues/48979
Transvaal lionOP
I found the issue to my errors. in my API routes in the catch block I did not return the new Response(). 🤦‍♂️
Answer