More Next.js answers

by Danny
Published 2 min read

Your data changed but the page did not. Learn why App Router routes get prerendered or cached, and how to revalidate with time-based or on-demand revalidation.

The short answer

#

Usually the fetch is not the problem: the whole route was prerendered at build time, so the HTML holds the data from that moment. Tell Next.js how fresh the data must be. Either revalidate it on a timer, revalidate it on demand when the data changes, or make the route render on every request.


_10
// app/products/page.tsx
_10
export default async function Page() {
_10
const res = await fetch('https://api.example.com/products', {
_10
next: { revalidate: 60 }, // refresh at most once a minute
_10
});
_10
const products = await res.json();
_10
return <ProductList products={products} />;
_10
}

Why it happens

#

Next.js tries to render routes ahead of time. During next build, any route that does not read request-specific data (cookies, headers, search params) is rendered once and the result is stored. Visitors get that stored HTML until something tells Next.js to render again.

Since Next.js 15, fetch responses are not cached by default. That surprises people, because a page can still look stale: the fetch ran once at build time, and the prerendered page is what is being served. In development, pages render on every request, so the problem often only shows up in production.

If your app has Cache Components turned on (the cacheComponents option in Next.js 16), the model is different. Data is dynamic unless you opt into caching with the 'use cache' directive, and the freshness of cached data is set with cacheLife and invalidated with tags.

There is also a client-side Router Cache. When you navigate back to a page you visited, the browser may reuse the payload it already has. After a mutation, refresh it with router.refresh() or by revalidating from a Server Action.

How to fix it

#

Revalidate on a timer

Add next.revalidate to the fetch, as in the short answer. The first visitor after the window passes still sees the old page, and Next.js renders a fresh version in the background for the next visitor. This is stale-while-revalidate, and it suits content that can be a little out of date, such as a blog or product listing.

Revalidate on demand with tags

When you know exactly when data changes, for example after a CMS publish or a form submission, tag the fetch and invalidate the tag.


_10
// Reading the data
_10
const res = await fetch('https://api.example.com/products', {
_10
next: { tags: ['products'] },
_10
});


_11
// app/actions.ts
_11
'use server';
_11
_11
import { revalidateTag, revalidatePath } from 'next/cache';
_11
_11
export async function saveProduct(formData: FormData) {
_11
await db.products.update(/* ... */);
_11
revalidateTag('products', 'max');
_11
// or revalidate a single route
_11
revalidatePath('/products');
_11
}

In Next.js 16, revalidateTag takes a second argument, a cacheLife profile such as 'max', which serves the old data while the new data loads. The single-argument form is deprecated. If you need the user to see their own change straight away inside a Server Action, Next.js 16 also offers updateTag, which expires the entry immediately. On Next.js 15, call revalidateTag('products') with one argument.

Render on every request

For data that must always be live, such as a stock level or a user dashboard, render the route dynamically. Calling connection() from next/server before the fetch tells Next.js the route depends on the incoming request.


_10
import { connection } from 'next/server';
_10
_10
export default async function Page() {
_10
await connection();
_10
const res = await fetch('https://api.example.com/stock');
_10
const stock = await res.json();
_10
return <Stock data={stock} />;
_10
}

Without Cache Components, export const dynamic = 'force-dynamic' in the page file does the same thing for the whole route.

Common pitfalls

#
  • Testing caching with next dev. Development renders every request, so build and run with next build and next start to see real behaviour.
  • Revalidating a path that does not match the route. revalidatePath('/products') does not revalidate /products/123 unless you pass the dynamic segment pattern and the 'page' type.
  • Forgetting that data fetched with a database client or SDK, not fetch, is not affected by fetch options. Use route segment config, 'use cache', or tags on your own cached functions instead.
  • Calling revalidateTag in a Client Component. It only works on the server, in Server Actions or Route Handlers.
#

Was this resource helpful?