Forge Fate
Backend & Infrastructure

Pages Loaded With the API Down: Page and Data Caching in Next.js

4 min read

Calling the API on every page view seemed unnecessary. Posts changed occasionally; between edits, readers saw the same content. I enabled Next.js ISR caching.

Pages still loaded with the API stopped. I still needed to check what was stored and what the result proved. The disk-permission problem in Part 4 was one example.

Starting point: every request reached the API

The first goal was to see a hello response from the API at the public URL. I disabled caching so that a value baked at build time would not be mistaken for a live response.

tsx
export const dynamic = "force-dynamic";const res = await fetch(`${base}/api/hello`, { cache: "no-store" });

Keeping these settings after adding post lists and detail pages would repeat rendering and API calls on every visit. I decided to reuse results while the posts remained unchanged.

Starting with a 60-second revalidation interval

ISR reuses pre-rendered pages and regenerates them when needed. I started with time-based revalidation rather than coupling the publishing tool to a refresh call on the web app.

blog-web/lib/api.ts (at introduction)ts
const REVALIDATE = 60;const res = await fetch(`${base}/api/posts`, { next: { revalidate: REVALIDATE } });

I also removed force-dynamic from home, category, and post-detail pages. Removing that setting alone does not prove that every route's rendered output is cached. Dynamic rendering and fetch caching need separate checks.

At the time I thought of this as "up to 60 seconds of delay." More precisely, a request after the cached entry is 60 seconds old can start revalidation. No visit means no refresh, and neither regeneration time nor failures fit within a guaranteed update deadline. [S1]

Page output and API responses are separate caches

LayerStoresWork avoided on reuse
Full Route CacheRendered output, including HTML and React Server Component data (RSC)Page rendering
Data CacheResponses from fetches configured for cachingThat API call

A page can render on every request while its fetches reuse the Data Cache. Conversely, a valid cached page can skip rendering altogether. The absence of an API call does not identify which layer served the request.

Their lifetimes differ too. The docs describe the Data Cache as persisting across requests and deployments, but the Full Route Cache is cleared on a new deployment. Self-hosting also requires checking storage and volume configuration. When this blog replaces a container with a new Docker image, I do not assume the previous container's cache comes with it. [S1]

Route behavior observed in July and August

RouteRecorded observationLimit of that evidence
HomeHTML existed in the build. August reproductions showed output retained or replaced depending on regeneration successA build without the API can also save an empty result (Part 8)
Post detailIn the August reproduction it rendered per request and recovered immediately when the API returnedThe July build table was insufficient evidence for claiming Full Route Cache use
CategoryReturned 200 with the API down in July; after deletion, the old response persisted until a restartThe responsible cache layer and conditions were not established (Part 6)
RSS and sitemapExplicit revalidate = 60; cached output and refresh behavior checked in AugustTheir refresh times need not match the home page

These are observations at specific times. Fetches inside a post-detail render also have cache settings, but that is not the same as caching the rendered page. The configuration changed later. A production audit on September 26 found Full Route Cache HIT and STALE responses for post-detail pages too. The August observations in the table do not describe a permanent property of those routes.

The RSS GET route handler needed explicit configuration. Ordinary GET route handlers in Next.js 15 are not cached by default. Sitemaps and OG images are special metadata handlers: they are static by default unless Dynamic APIs or configuration change that behavior. [S3] I specified a refresh interval for those routes as well.

ts
export const revalidate = 60;

Likewise, ƒ in the next build output does not mean fetch responses are uncached. Reading source can confirm that force-dynamic was removed; actual caching needs separate checks of build output and requests. [S1] [S2]

What happens after 60 seconds

The usual time-based revalidation flow for a cached response is: [S1] [S2]

  1. Within its lifetime, return the stored response.
  2. On the first request after expiry, return the old response and start revalidation in the background.
  3. Requests after successful revalidation receive the new result. Failure leaves the previous result intact.

To check an update, send a request after expiry, then check again after revalidation finishes. Two rapid requests do not guarantee fresh content. Nor should the 60 seconds be counted from publication or API shutdown: the relevant clock belongs to the cached entry.

What I checked, and what it proved

I initially tried counting API log entries, but HTTP access logging was not enabled in Spring Boot. Instead, I stopped the API container and verified 200 responses from home, category pages, and the sitemap.

That proved responses were available without the API. Disk writes needed the separate permissions check in Part 4; behavior during an outage after expiry needed the reproduction in Part 7. I had not yet checked a failed refresh of an expired entry.

Would a 404 remain cached too?

My initial notes also worried about cached 404 responses, but the later post-detail reproduction showed per-request rendering. Per-request rendering does not by itself rule out fetch caching. What this reproduction showed was that the post-detail page recovered immediately when the API returned. It did not establish a single lifetime for every 404 on the site.

The next post covers a deleted category whose old page remained available. Instead of only waiting for time-based revalidation, I added a way for the writer to request cache invalidation.

Sources

About these records

I distinguish my initial conclusions from what later reproductions established. The sources are my July 1 ISR notes, July 2 cache-invalidation notes, and August 26 outage reproduction. I also included the later post-detail behavior recorded in the September 26 audit. This is a record of the configuration I used with Next.js 15.

Report an error or share feedback

Open a draft with this article’s title and URL. Review the message and recipient before sending.

To: [email protected]

Open email draft

If no email app opens, copy these details into your usual email service.

Contact information