Next.js Discord

Discord Forum

Rerender server component on Link navigation

Answered
Silver Fox posted this in #help-forum
Open in Discord
Silver FoxOP
Hello all,
I'm fairly new to Next.js and have come across the following problem.

I have the following two components:

/app/tours/page.tsx
export default function Page() {
    return (
        <>
            <PageHeader title={"Search tours"} hideBackLink/>

            <Suspense fallback={<Loading/>}>
                <AllToursGrid/>
            </Suspense>
        </>
    )
}


/app/components/tours.component.ts
export async function AllToursGrid() {
    const result = await getAllTours();

    return (
        <div className="grid md:grid-cols-2 xl:grid-cols-3 2xl:grid-cols-4 gap-4">
            <MapArray
                data={result}
                mapFunction={(tour, index) => {
                    return <TourCard tour={tour} key={index}/>;
                }}
            />
        </div>
    )
}


I've added a timeout to the above server function getAllTours which waits about two seconds to test the loading animation. So far so good - everything works as expected. However, I wish to reload the data on every navigation (i.e. I switch to another page and back again and want the latest data being fetched again - and not cached data being displayed).

A (dirty) workaround I've found is to add a client component Refresher:
'use client'

export function Refresher() {
    const router = useRouter();

    useEffect(() => {
        router.refresh();
    }, []);

    return <></>;
}


which bypasses the problem, but it feels as though this isn't the most elegant solution at hand. Is there a possibility - without converting the entire component to a client component and using fetch to load the data every time the user navigates to this page? It might be that I'm going about this wrong in the first place, in which case I'd be happy to know as well :).

Thanks in advance!

6 Replies

Velvet ant
@Velvet ant not sure but is that what your looking for ? <https://nextjs.org/docs/app/building-your-application/caching#opting-out-1>
Silver FoxOP
No, unfortunately not. As far as I understood - after some more digging - I'd be required to opt out of the router cache. But this apperently isn't possible, with some issues dating back to 2021 iirc :/.
Answer
@joulev https://nextjs.org/docs/app/api-reference/next-config-js/staleTimes
Silver FoxOP
Right I see, I‘ll give that a shot - thanks!

Am I even going about this the correct/idiomatic way, or is there a better way to fetch data? I‘m still a little confused as to how to check permissions in this case.
@Silver Fox Right I see, I‘ll give that a shot - thanks! Am I even going about this the correct/idiomatic way, or is there a better way to fetch data? I‘m still a little confused as to how to check permissions in this case.
imo no this is not the best way to fetch data.

if you update the data via a client-side form and would like to see the new data immediately, can just use server actions + revalidatePath/revalidateTag. the router cache is automatically flushed.

if you get the data and would like it being as fresh as possible, client side data fetching is better.

more explanation at https://joulev.dev/blogs/yes-nextjs-router-cache-is-actually-good
Silver FoxOP
Thanks a lot!