[VERCEL DEPLOYMENT] SyntaxError: Unexpected token '<', "<!DOCTYPE "... is not valid JSON
Answered
Asiatic Lion posted this in #help-forum
Asiatic LionOP
Hi everyone ! Deployment error on Vercel with NextJS14:
I think this is because my fetch() endpoint hitting not the good variable environment.
.env.local :
VERCEL env var (on the Vercel Project dashboard):
(This is the deployment URL on Vercel).
My server component to fetch the data :
What's in /api/gallery/route.ts :
I did a lot of research on the internet and couldn't find the solution. Is this an environment variable problem? Am I the one who's doing the wrong thing? I think there is a server/client problem but I'm having trouble understanding what's going on. THANKS !
No problem with next build and fetch on localhost:3000.
.........
Generating static pages (11/15)
SyntaxError: Unexpected token '<', "<!doctype "... is not valid JSON
at JSON.parse (<anonymous>)
at parseJSONFromBytes (node:internal/deps/undici/undici:5584:19)
.........
}I think this is because my fetch() endpoint hitting not the good variable environment.
.env.local :
API_URL=http://localhost:3000VERCEL env var (on the Vercel Project dashboard):
API_URL=https://jdanse-asso.vercel.app(This is the deployment URL on Vercel).
My server component to fetch the data :
import Loading from "@/utils/loading";
import { Suspense } from "react";
import ImageList from "../Gallery/ImageList";
export default async function ServerGallery() {
const getGalleryImages = async () => {
const response = await fetch(`${process.env.API_URL}/api/gallery`);
const data = await response.json();
return data;
};
const galleryImages = await getGalleryImages();
return (
<Suspense fallback={<Loading />}>
<ImageList imagesList={galleryImages} />
</Suspense>
);
}What's in /api/gallery/route.ts :
export async function GET(request: NextRequest) {
await dbConnect();
try {
const images = (await GalleryImage.find({})) as GalleryImageTypes[];
return NextResponse.json(images);
} catch (error) {
if (error instanceof Error) {
return NextResponse.json(
{ success: false, error: error.message },
{ status: 400 }
);
}
return NextResponse.json(
{ success: false, error: "Unknown error" },
{ status: 400 }
);
}
}I did a lot of research on the internet and couldn't find the solution. Is this an environment variable problem? Am I the one who's doing the wrong thing? I think there is a server/client problem but I'm having trouble understanding what's going on. THANKS !
No problem with next build and fetch on localhost:3000.
4 Replies
Asiatic LionOP
Dude, sponspor sended on Github, your nextjs-faq is very helpfull + i like your blog 🙂 Good job !
thank you!
Transvaal lion
I dont know if youre still stuck on this but I just had this problem myself.
the issue for me was the process.env variable wasnt being shared to the client side. the solution was to add NEXTPUBLIC
to the start of your .env variable
the issue for me was the process.env variable wasnt being shared to the client side. the solution was to add NEXTPUBLIC
to the start of your .env variable
API_URL
to
NEXT_PUBLIC_API_URL