Next.js Discord

Discord Forum

Page doesn't get server-rendered

Answered
Gazami crab posted this in #help-forum
Open in Discord
Gazami crabOP
Hi, I have a page, that gets all news form the DB. It's a server rendered page, and it works fine on localhost, but in production, it only gets the posts on build.
Any ideas how I could have it be server rendered?

import type { Metadata } from "next";
import Image from "next/image";
import Link from "next/link";
import dayjs from "dayjs";

import { prisma } from "@acme/db";

import Heading from "~/components/Heading";

export const metadata: Metadata = {
  title: "News | Acme",
  description: "Acme News",
};

export default async function NewsPage() {
  const posts = await prisma.update.findMany({
    where: {
      visibility: "public",
    },
    orderBy: {
      date: "desc",
    },
  });

  console.log(posts);

  return (
    <>
      <main className="container">
        <Heading>News</Heading>
        <div className="mx-auto grid gap-x-6 md:grid-cols-2 lg:grid-cols-3  2xl:w-10/12">
          {posts.map((post, index) => (
            <Link
              key={index}
              href={`/news/${post.slug}`}
              className="group mb-20 rounded-lg border px-6 py-8 duration-300 hover:border-dark-blue-800"
            >
              <div className="relative aspect-[16/10]">
                <Image
                  src={`https://utfs.io/f/${post.imageKey}`}
                  alt={post.title}
                  fill
                  style={{ objectFit: "contain" }}
                />
              </div>
              <h2 className="mt-4 font-redhat text-2xl font-bold">
                {post.title}
              </h2>
              <div>{dayjs(post.date).format("MMMM D, YYYY")}</div>
              <div className="mt-4">
                {stripHtmlAndShorten(post.content ?? "")}
              </div>
            </Link>
          ))}
        </div>
      </main>
    </>
  );
}

function stripHtmlAndShorten... //not enough space on Discord for full function
Answered by Ray
By default, Next.js will cache as much as possible to improve performance and reduce cost. This means routes are statically rendered and data requests are cached unless you opt out.
View full answer

7 Replies

@Gazami crab Hi, I have a page, that gets all news form the DB. It's a server rendered page, and it works fine on localhost, but in production, it only gets the posts on build. Any ideas how I could have it be server rendered? ts import type { Metadata } from "next"; import Image from "next/image"; import Link from "next/link"; import dayjs from "dayjs"; import { prisma } from "@acme/db"; import Heading from "~/components/Heading"; export const metadata: Metadata = { title: "News | Acme", description: "Acme News", }; export default async function NewsPage() { const posts = await prisma.update.findMany({ where: { visibility: "public", }, orderBy: { date: "desc", }, }); console.log(posts); return ( <> <main className="container"> <Heading>News</Heading> <div className="mx-auto grid gap-x-6 md:grid-cols-2 lg:grid-cols-3 2xl:w-10/12"> {posts.map((post, index) => ( <Link key={index} href={`/news/${post.slug}`} className="group mb-20 rounded-lg border px-6 py-8 duration-300 hover:border-dark-blue-800" > <div className="relative aspect-[16/10]"> <Image src={`https://utfs.io/f/${post.imageKey}`} alt={post.title} fill style={{ objectFit: "contain" }} /> </div> <h2 className="mt-4 font-redhat text-2xl font-bold"> {post.title} </h2> <div>{dayjs(post.date).format("MMMM D, YYYY")}</div> <div className="mt-4"> {stripHtmlAndShorten(post.content ?? "")} </div> </Link> ))} </div> </main> </> ); } function stripHtmlAndShorten... //not enough space on Discord for full function
I think its because it is a static generated page
By default, Next.js will cache as much as possible to improve performance and reduce cost. This means routes are statically rendered and data requests are cached unless you opt out.
Answer
you can opt out by setting it dynamic in the route segment config like this
export const dynamic = 'force-dynamic'
or ISR with
export const revalidate = 10
@Ray > By default, Next.js will cache as much as possible to improve performance and reduce cost. This means routes are statically rendered and data requests are cached unless you opt out.
Gazami crabOP
Ohh, I had no idea Next did this with app dir as well. Thanks a lot for the help 🙏