Forge Fate
Backend & Infrastructure

I Deleted It From the Database, but the Page Stayed Up: Clearing the Next.js Cache on Demand

5 min read

On the day I enabled caching, a category page stayed up after I deleted the category from the database. Several of the 60-second intervals configured in Part 5 passed. Only restarting the web container changed it to a 404.

I did not establish why revalidation failed to update the page that day. I added an endpoint so the tool changing a post or category could explicitly request a refresh.

A deleted category page kept returning 200

When I first built the blog, there were three categories: Reviews, IT, and Life. After rethinking the content direction, I decided to drop the Reviews category. Since there were still zero production posts, after confirming it was safe, I deleted the rows directly from the categories table in the production database and reseeded.

But the /category/review page didn't go away. Even after several 60-second revalidation windows, it kept serving the old page with 200. It only switched to 404 once I restarted the web container.

Why didn't it change after 60 seconds?

The record establishes that restarting the web container changed the response to 404. The disk-permissions problem from Part 4 was also present, but that alone does not establish which cache layer retained the result or why.

A database edit sends no immediate notification to the web app. Time-based revalidation remains useful, but verifying a change immediately after saving called for an explicit invalidation path.

Requesting invalidation after a save

The next day, I added a single cache-clearing endpoint to the web app. After a save completes, the editing tool calls this URL so the next server request using that data fetches it again. The diagram shows the successful path: saving, authentication, and the fresh fetch all succeed.

Preparing diagram.

Diagram source
sequenceDiagram
  participant W as Editor
  participant A as API
  participant N as Web
  participant R as Reader
  W->>A: Save content
  A-->>W: Saved
  W->>N: POST /api/revalidate<br/>Bearer token
  N->>N: Authenticate, invalidate tag
  N-->>W: 200
  R->>N: Page request
  N->>A: Fetch fresh data
  A-->>N: Fetch succeeds
  N-->>R: Updated content

The web app’s public-content fetches carry the same tag, and the endpoint invalidates that tag.

blog-web/lib/api.ts (excerpt)ts
export const CONTENT_TAG = "blog-content";fetch(url, { next: { revalidate: 60, tags: [CONTENT_TAG] } });
blog-web/app/api/revalidate/route.ts (excerpt)ts
export async function POST(request: Request): Promise<Response> {  const expected = process.env.REVALIDATE_TOKEN ?? "";  if (!expected) {    return Response.json({ error: "revalidation disabled" }, { status: 401 }); // fail-closed  }  const auth = request.headers.get("authorization") ?? "";  const provided = auth.startsWith("Bearer ") ? auth.slice("Bearer ".length) : "";  if (!provided || !tokenMatches(provided, expected)) {    return Response.json({ error: "unauthorized" }, { status: 401 });  }  revalidateTag(CONTENT_TAG);  return Response.json({ revalidated: true, tag: CONTENT_TAG });}

I kept the 60-second time-based revalidation and added an explicit invalidation call after saves.

Use a single tag

revalidateTag lets you split tags finely. With separate tags per post, per category, and per tag name, you can clear only what changed.

I grouped the content under blog-content. Deleting one category could affect the home page, post details, and category lists, and I did not want the caller to track all those relationships. The tradeoff was fetching unchanged content again too. I accepted that cost; I had not measured it with a load test.

No token means the feature is off

This endpoint lives on the public web app, so it checks a Bearer token, as the API does for writes.

A missing configuration token or an incorrect request token returns 401. Leaving the token unset must not bypass authentication (fail-closed).

The token-comparison implementation is in the supplement below. The essential contract here is that missing or incorrect tokens are rejected, and only an authorized request invalidates the content tag.

What the public endpoint can do

At the time, I chose to expose this route on the public web app and protect it with a token. Blocking a path at a proxy or moving it to an internal service would have been other options.

The handler does not read or modify posts or database rows. Repeated invalidation can still increase rendering and API traffic on subsequent requests, causing delays or reduced availability. Leaving out rate limiting was an operational choice, not a finding that abuse would have little impact.

Read the token at request time

This blog once had an incident where an environment variable got frozen into static pages at build time (covered in Part 9). This endpoint does the opposite: it reads process.env.REVALIDATE_TOKEN on every request. The variable is read inside the function handling the POST request. Updating the environment supplied to the container and recreating it makes the new value available. Editing the .env file alone does not change the environment of an already running process. [S3]

Verification: checking the refresh path after a save

This did not reproduce the root cause of the category incident. A title edit tested the contract: save, invalidate, then receive new content on the next request.

  1. Calls with no token or a wrong token returned 401, and the right token returned 200.
  2. Right after editing a post title through the API, the home page still showed the old title.
  3. On the request after calling the endpoint, the new title appeared immediately. No 60-second wait.
  4. After emptying the token in the configuration and only recreating the container, even the correct token got 401. I then restored the token and confirmed it worked again.

This code uses the one-argument Next.js 15 call, revalidateTag(CONTENT_TAG). After invalidation, the next server request using that data fetches it again. [S1] A 200 from the endpoint confirms invalidation handling, not regeneration of every page or a successful fresh API response. The call also does not automatically refresh a page already open in a browser.

Remaining assumptions and what came after

  • It assumes a single web instance. With the default local cache used here, invalidation on one instance cannot be assumed to reach the others. Multiple instances would need coordinated storage and propagation of tag invalidation. [S4]
  • The endpoint later came to be used in several places. The admin app calls it from the same Mac every time it saves a post or changes a status, and I later refined that call with a 5-second timeout and failure logging. Calling it once right after deploying the web app also became part of the operating procedure; Part 8 explains why.

The next post covers a cache that changed to an empty list during an API outage. I trusted the cache to protect the site when the API stopped, but the API client was actually turning failures into a "success" in the form of an empty list. It explains why that empty result was cached and how I corrected it.

Supplement: token comparison

Node.js timingSafeEqual compares byte sequences of equal length. I hash both tokens with SHA-256 to obtain 32-byte values before calling it. [S2] This addresses timing differences in the comparison step; it does not guarantee identical timing for hashing and the entire request handler on every input.

blog-web/app/api/revalidate/route.ts (excerpt)ts
function tokenMatches(provided: string, expected: string): boolean {  const a = createHash("sha256").update(provided).digest();  const b = createHash("sha256").update(expected).digest();  return timingSafeEqual(a, b);}

Sources

About these records

This post is based on the incident I hit while cleaning up categories on July 1, 2026, and my work notes for the cache-clearing endpoint I built the next day. The code is excerpted from the repository as checked on November 7, 2026. It describes the one-argument revalidateTag call in Next.js 15. My records don't establish exactly why the page didn't change after 60 seconds during the incident, and I keep that part separate.

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