1

I've got a simple React Server Component (below) that seems to get it's data at build time and never reloads it when the site is running. I would have thought that it would retrieve the data once when the server component first rendered.

Without converting it to a client component, how can I get this server component to reload when page first is requested with a GET.

import prisma from "@/lib/prisma";

export default async function AdminNewsSourceList() {

  const dataNewsSource = await prisma.newsSource.findMany();

  return (
    <div className="container">
      <h1 className="my-3">News Sources</h1>
      <table className="table table-striped">
    ...
Mayank Kumar Chaudhari
  • 16,027
  • 10
  • 55
  • 122
Peter Kellner
  • 14,748
  • 25
  • 102
  • 188

1 Answers1

0

You need to use segment-level-caching - https://nextjs.org/docs/app/building-your-application/data-fetching/caching#segment-level-caching

do

export const revalidate = 1
Mayank Kumar Chaudhari
  • 16,027
  • 10
  • 55
  • 122
  • I'm still a little confused. Does revalidate=60 mean that every 60 seconds, whether or not a GET request is sent to the site, the async function and database will queried? My desired behavior is to only get the data when a GET request is made like GetServerSideProps does. I'm good with a cache there, but I don't want to show stale data when I'm outside the cache period. – Peter Kellner Jul 28 '23 at 17:46
  • First Get request after revalidation period will get stale data and in the background the page regeneration will begin... Until the page is regenerated all requests will continue to see stale data. – Mayank Kumar Chaudhari Jul 28 '23 at 18:53
  • Is there a way to avoid the extra GET for updated data immediately? – Peter Kellner Jul 29 '23 at 19:21