Next.js Discord

Discord Forum

Build errors on frontend in pages with server side fetches when server is offline.

Answered
zhefciad posted this in #help-forum
Open in Discord
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} />
  );
}
Answered by Arinji
yea
View full answer

20 Replies

Yes, on build nextjs fetches data once so if you dont have your server running, how will it fetch
i know
but is that normal?
Should i do something to stop it from fetching on npm run build?
@zhefciad but is that normal?
yea
Answer
.. it shld fetch it once so it can build your pages
cus it's weird, that doesnt happen when im using vite + express
Read the docs :/
@Arinji yea
gotcha
mark an answer
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
thanks man
@zhefciad thanks man
mark an answer :D
i thumbed up
lol idk how