Will this fetch be executed on the server or client?
Answered
Transvaal lion posted this in #help-forum
Transvaal lionOP
Page.tsx
async function getData(slug: string) {
const res = await fetch(
`${process.env.PAYLOAD_PUBLIC_SERVER_URL}/events/where[group.slug][equals]=${slug}`,
{ cache: "no-store" }
);
// The return value is *not* serialized
// You can return Date, Map, Set, etc.
if (!res.ok) {
// This will activate the closest `error.js` Error Boundary
throw new Error("Failed to fetch data");
}
return res.json();
}
export default async function Page({ params }: { params: { slug: string } }) {
const events = await getData(params.slug);
return (
<html lang="en">
<head>
...Answered by Giant panda
The fetch runs on the server unless the page is a client component, which is highly discouraged anyways.
7 Replies
Server, you are using env variables which don't have the NEXT_PUBLIC prefix
@Arinji Server, you are using env variables which don't have the NEXT_PUBLIC prefix
Giant panda
Your first part is completely unrelated to the second.
Giant panda
The fetch runs on the server unless the page is a client component, which is highly discouraged anyways.
Answer
it wont run though cause you cant have env variables run on the client side without that prefix right?
like its gonna be undefined
Giant panda
Whether it runs successfully or not is unrelated to OP's question.
hmm, that is true, sorry