# Three silent traps after enabling Next.js 16 Cache Components

> Enabling Next.js 16 Cache Components froze an API at build time, turned 404s into 200s and cached a failure. None of it raised an error.

- Date: 2026-11-25
- Topics: Next.js, Performance, Debugging
- Chinese original: https://kbs0830.com/blog/nextjs-cache-components-traps

**Cache Components** in Next.js 16 (`cacheComponents: true` in `next.config.ts`) splits a page into two parts: a static shell rendered at build time, and live data streamed in through `<Suspense>`. Visitors see the shell immediately and the data arrives right after, which made my home page a lot faster.

This site has plenty of live data: how many aircraft my home radar receiver is tracking, visitor stats, exchange rates. Before turning the feature on I wrote a checklist and converted things one by one. The build passed, unit tests passed, browser tests passed, and I still walked into three traps. What they have in common: **none of them raises an error**.

## Trap 1: an API that reads a file synchronously freezes at build time

`/api/radar` returns live numbers from the radar. Another process writes them to `data/radar.json` every 30 seconds:

```ts
export function GET() {
  const state = JSON.parse(readFileSync("data/radar.json", "utf8"));
  return Response.json(summarize(state));
}
```

With Cache Components enabled, this API returned **the same numbers forever**.

The new model decides whether something is dynamic by checking whether you do anything asynchronous and request-bound: reading the request, calling `fetch`, reading a file asynchronously. `readFileSync` is synchronous, so Next concluded that this GET used no live data, ran it once at build time, stored the result, and served that copy to every request afterwards.

**No error, no warning.** In the build output the route is marked `○` (static) instead of `ƒ` (run on every request), and you will not notice unless you go looking.

The fix is to tell Next explicitly that this handler is dynamic:

```ts
import { connection } from "next/server";

export async function GET() {
  await connection(); // everything after this runs on every request
  const state = JSON.parse(readFileSync("data/radar.json", "utf8"));
  return Response.json(summarize(state));
}
```

The way to verify it is crude but effective: **edit the data file by hand, call the API again, and the number has to change**. I changed my test data from 382 to 1,382. Before the fix the API still said 382. Only after adding `connection()` did it return 1,382.

## Trap 2: URLs that do not exist return 200

For a dynamic route such as `/blog/[slug]`, calling `notFound()` for an unknown slug used to produce a 404.

With Cache Components, **the shell is streamed first**. The HTTP status code is decided when the first byte goes out, and it is 200. By the time the content part reaches `notFound()`, the page does show the 404 UI, but the status is already 200. This is a "soft 404", and search engines will happily index a pile of URLs that do not exist as if they were normal pages.

The fix is one line on the dynamic route, which makes Next check whether the URL exists before it starts streaming:

```ts
export const ensureStatic = "navigation";
```

To check: `curl -o /dev/null -w "%{http_code}" https://your-site/blog/does-not-exist` has to print 404.

## Trap 3: a failure gets cached

The font for my Open Graph images is fetched from Google Fonts, and I cached it with `"use cache"`:

```ts
async function loadFont() {
  "use cache";
  try {
    return await fetchFont();
  } catch {
    return null; // fall back to the default font
  }
}
```

It looks reasonable: if the fetch fails, fall back. The problem is that `null` is also a successful return value, so **it gets cached**. One flaky network moment and every share image uses the default font for a long time, until the cache entry expires.

To keep failures out of the cache, **throw instead of returning null**, and handle the failure at the call site:

```ts
async function loadFont() {
  "use cache";
  return await fetchFont(); // a failure throws and is not cached
}

const font = await loadFont().catch(() => null);
```

## What I learned

- After enabling Cache Components, **read the symbol next to every route in the build output**: ○ static, ◐ shell plus streaming, ƒ run on every request, ● prerendered. A live API must be ƒ
- An API that reads a file synchronously needs `await connection()` as its first line
- Dynamic routes need `ensureStatic = "navigation"`, and you should confirm with curl that a missing URL really returns 404
- Inside `"use cache"`, throw on failure instead of returning null
- All three traps are **silent**. The only way to catch them is to actually poke at the thing: change the data, call the API, look at the status code

One more note: when a unit test calls a route's `GET` directly, `connection()` and `after()` fail because there is no request context. Mock `next/server` in those tests.

---

Author: PINGWEI LI (李秉威, Li Ping-Wei)
Original page: https://kbs0830.com/en/blog/nextjs-cache-components-traps
