Build errors on frontend in pages with server side fetches when server is offline.
Answered
zhefciad posted this in #help-forum
zhefciadOP
Is this normal behavior when server side fetching? Should I always have to open my backend so that the build succeeds?
import { Suspense } from "react";
import LandingPage from "./LandingPage";
import { getKindeServerSession } from "@kinde-oss/kinde-auth-nextjs/server";
import Link from "next/link";
export default async function Home() {
const baseUrl =
process.env.NEXT_PUBLIC_BASE_URL ||
process.env.NEXT_PUBLIC_LAN_BACKEND_URL ||
"http://127.0.0.1:1337";
const session = getKindeServerSession();
const user = await session.getUser();
const listings = await fetch(
`${baseUrl}/api/listings?filters[status][$eq]=active&populate=*`
).then((res) => {
return res.json();
});
let photos = Array.from({ length: 6 }, (_, i) => i + 1);
const kindeUserDb = await fetch(
`${baseUrl}/api/kinde-users?filters[kindeID][$eq]=${user?.id}`
).then((res) => {
return res.json();
});
return (
<LandingPage slides={listings.data} kindeUserDb={kindeUserDb} user={user} />
);
}20 Replies
Yes, on build nextjs fetches data once so if you dont have your server running, how will it fetch
Answer
.. it shld fetch it once so it can build your pages
zhefciadOP
cus it's weird, that doesnt happen when im using vite + express
Read the docs :/
@Arinji yea
zhefciadOP
gotcha
mark an answer
zhefciadOP
wait bruh
import { Suspense } from "react";
import LandingPage from "./LandingPage";
import { getKindeServerSession } from "@kinde-oss/kinde-auth-nextjs/server";
import Link from "next/link";
export default async function Home() {
const baseUrl =
process.env.NEXT_PUBLIC_BASE_URL ||
process.env.NEXT_PUBLIC_LAN_BACKEND_URL ||
"http://127.0.0.1:1337";
const session = getKindeServerSession();
const user = await session.getUser();
const listings = await fetch(
`${baseUrl}/api/listings?filters[status][$eq]=active&populate=*`
).then((res) => {
return res.json();
});
let photos = Array.from({ length: 6 }, (_, i) => i + 1);
const kindeUserDb = await fetch(
`${baseUrl}/api/kinde-users?filters[kindeID][$eq]=${user?.id}`
).then((res) => {
return res.json();
});
return (
<LandingPage slides={listings.data} kindeUserDb={kindeUserDb} user={user} />
);
}This is good right?
looks fine
zhefciadOP
thanks man
@zhefciad thanks man
mark an answer :D
zhefciadOP
i thumbed up
lol idk how