Next.js Discord

Discord Forum

How do you not let next.js cache data? I need this component to be SSR.

Unanswered
Blanc de Hotot posted this in #help-forum
Open in Discord
Blanc de HototOP
I thought next didn't cache per default now? How can I make sure this is server side rendered fresh data at request and not build.

I run "next": "14.2.4",

Thanks!

` import { Button, buttonVariants } from "@/components/ui/button"; import Link from "next/link"; import db from "@/db/db"; export const dynamic = "force-dynamic"; export const fetchCache = "force-no-store"; export default async function Hero() { const data = await db.heroSection.findFirst(); if (!data) { return <div className="relative overflow-hidden py-16 lg:py-20">not foreground</div>; } return ( <div className="relative overflow-hidden py-16 lg:py-20"> <div className="max-w-2xl text-center mx-auto"> <p className="">{data.eyebrowHeading}</p> {/* Title */} <div className="mt-4 max-w-2xl"> <h1 className="scroll-m-20 text-4xl font-extrabold tracking-tight lg:text-5xl"> {data.title} </h1> </div> {/* End Title */} <div className="mt-4 max-w-3xl"> <p className="md:text-xl text-lg text-muted-foreground">{data.subtitle}</p> </div> {/* Buttons */} <div className="mt-7 gap-3 flex justify-center"> <Link href="/products" className={buttonVariants({ variant: "default", size: "lg" })}> {data.button1Title} </Link> <Link href={data.button1Link || "/"} className={buttonVariants({ variant: "outline", size: "lg" })} > Om oss </Link> </div> </div> </div> ); }´

35 Replies

Great golden digger wasp
bro like how can you directly get data from your db. Like it is possible in next js but doesn't means that you should do that
Because the only correct way to fetch data in next js is using fetch api
or like any other library
even using server actions to fetch data is not a correct way
and you are just querying through the db inside your component.
like its my humble advice that you should create an api to fetch the data
Blanc de HototOP
You have no idea what you are talking about. Not only, do you come into a thread and answer something completely different than the topic, you are also totally wrong in what you are saying.

Next.js uses react server components per default. This data is fetched, and the pages are built on the server (SSG per default).
Northeast Congo Lion
Can you try export const revalidate=0
Or worst case u can do revalidatePath or Tag everytime u open the page
@Blanc de Hotot I thought next didn't cache per default now? How can I make sure this is server side rendered fresh data at request and not build. I run "next": "14.2.4", Thanks! import { Button, buttonVariants } from "@/components/ui/button"; import Link from "next/link"; import db from "@/db/db"; export const dynamic = "force-dynamic"; export const fetchCache = "force-no-store"; export default async function Hero() { const data = await db.heroSection.findFirst(); if (!data) { return <div className="relative overflow-hidden py-16 lg:py-20">not foreground</div>; } return ( <div className="relative overflow-hidden py-16 lg:py-20"> <div className="max-w-2xl text-center mx-auto"> <p className="">{data.eyebrowHeading}</p> {/* Title */} <div className="mt-4 max-w-2xl"> <h1 className="scroll-m-20 text-4xl font-extrabold tracking-tight lg:text-5xl"> {data.title} </h1> </div> {/* End Title */} <div className="mt-4 max-w-3xl"> <p className="md:text-xl text-lg text-muted-foreground">{data.subtitle}</p> </div> {/* Buttons */} <div className="mt-7 gap-3 flex justify-center"> <Link href="/products" className={buttonVariants({ variant: "default", size: "lg" })}> {data.button1Title} </Link> <Link href={data.button1Link || "/"} className={buttonVariants({ variant: "outline", size: "lg" })} > Om oss </Link> </div> </div> </div> ); } ``´
hi, is this a doubt or you really did observe that next cached it? exporting dynamic as force-dynamic should be enough to make it server-rendered for each request
or is this component a child component of the root page.{tsx,jsx} component? given that the component name is Hero
Komondor
Try moving the
export const dynamic = "force-dynamic";
export const fetchCache = "force-no-store";
to the page.tsx file
^ if it does occur that Hero is a child component of page/layout.tsx file
Komondor
I agree with Duraid, what you have is kind of bad practice. It'd be better to pass a hero argument into your component, and fetch it outside the component, like in the page.tsx file. It'd still be server side rendered
i would have to disagree on that one :)
components fetching their own data makes it easier for it to be split into it's own <Suspense> chunk.
fetching the hero data on the main page.tsx file would block the entire page from rendering, it's a good practice to move the fetching onto a child server component to fetch the data independently so you can separate it to load as its own component in a <Suspense> boundary
But regarding my comment about moving the cache disabling to page.tsx, I think you have to do that because in NextJS you disaable a cache on a route, not on a specific component
@Komondor You can use several suspenses in page.tsx for several components that will stream data to the page when the data is ready
yes that is the case when the promise isn't awaited, you can pass promises into individual server components wrapped in a <Suspense> and have it be able to do the same thing as if they were fetched independently.
i thought you weren't taking suspense into account, if you were to have an await on the top-level page.tsx, it would make everything else wait until that promise is resolved.
i kinda forgot this pattern, but the OP can use either, both will work just fine :)
Komondor
Right both will work the same. It's just fetching things from the DB within a single component can lead to some un-manageable code in the future. That is, you'll end up adding arguments to the component such as dontFetch: boolean
it's better to fetch the data outside, and pass in the data as arguments to the component
out of curiosity, on what occasion do you need a dontFetch parameter? i have played around with next for a while, and not sure where and when that "workaround" will crop up?
Komondor
It comes down to re-usability. The components should be re-usable. As the application grows and you use the component in other areas, there will be times where you don't always want to fetch the data when the component is getting rendered. I know this because the fetch from the DB is a side-affect of rendering the component, and as an application grows things pop up where you need to supress the side-affects, or delay them
Here's an example: I reuse this component and I end up rendering it 20 times on my page, because I want 20 instances of it. That will cause 20 hits to the database. I don't want 20 hits to the database, I want one hit. So I add an argument to compensate for that
It already has side affects, and to compensate you have to make the component even more complex with more arguments
ah thank you! i wasn't working on projects with a lot of server components, haven't experienced such roadblock before. thanks for the insight
i guess the general rule of thumb is to keep side effects to a minimum for components that only displays some data
Komondor
Here's another use case. Since the data is fetched from the DB within this component, the data HAS to already be in the DB in order to render this component. That couples the data source to the component. That means in the future you can't allow the user to build their own Hero, and supply the data to the Hero component elsewhere. No, in order to render a Hero that data must first be in the DB.
that use case of "building their own hero" is pretty specific and may never pop up, but by coupling the data source to the component you've limited its reusability
Blanc de HototOP
I will only use this component at one place.
Did anyone have a suggestion how this can be not cached btw?
@Blanc de Hotot Did anyone have a suggestion how this can be not cached btw?
Komondor
I think you need to move the cache options you have to the page.tsx file
@Blanc de Hotot Did anyone have a suggestion how this can be not cached btw?
Highlander
Hey. I face the very same problem right now. Would be great if you found a way to go and can share it here.
@Highlander Hey. I face the very same problem right now. Would be great if you found a way to go and can share it here.
Blanc de HototOP
I think I solved it by going back to the page.tsx, because caching doesn't happen at the component, instead only at the page level.