Next.jsDev & Eng

Content type: Hands-on

7 min read

How to migrate a Next.js app to Cache Components before Next 17

The cacheComponents flag inverts the App Router's logic: fetch stops being static by default and now requires explicit 'use cache'. I show the path I'd follow to enable the flag, find what breaks first, and compare before and after without making up numbers.

Article · Dev & Eng
Reading
7 min
Code
3

The cacheComponents flag inverts the App Router's logic: fetch stops being static by default and now requires explicit 'use cache'. I show the path I'd follow to enable the flag, find what breaks first, and compare before and after without making up numbers.

Next.js stabilized cacheComponents in version 16.0, moving the option from experimental.cacheComponents to the top level of next.config.ts. The documentation is explicit about why: cacheComponents now controls ppr, useCache, and dynamicIO as a single setting, and the reference page itself warns that "both features will be enabled everywhere in the next major release, and the options will be removed". In other words: what's opt-in today becomes a mandatory default in Next 17. Anyone running a production app on a 16.x version gets a window to migrate calmly, but that window has an expiration date.

I didn't run this migration on a production app for this piece, and any TTFB number I gave here without a source would be made up. What follows is the roadmap I'd follow on a typical App Router project: where to enable the flag, what historically breaks first with this kind of default-behavior change, and how I'd measure before and after using only the tools that Next itself and DevTools already expose.

Enabling the flag: the bare minimum

The starting point is straightforward, but there's a catch the documentation itself points out. Enabling cacheComponents without explicitly declaring partialPrefetching generates a warning during the build, because the first version of Cache Components didn't include Partial Prefetching:

Expert views

What people are saying about it.

The This Week in Rails bulletin from November 27, 2026 brings a command to audit secrets without revealing them, the end of a per-attachment query in Action Text, and an Action Mailer that no longer needs to drag Active Job along at boot.

Bisneto Bragain an iMasters article · Read the article: Rails gets credentials:keys, batched Action Text, and Action Mailer without Active Job

A survey published on Planet PostgreSQL mapped, case by case, why psql -U postgres fails on Debian and Ubuntu installs and why the same account logs in without a password on the official Docker image.

Roberto Dinizin an iMasters article · Read the article: Peer authentication failed for user postgres: why this error appears and how to actually fix it
ts
// next.config.ts
import type { NextConfig } from 'next'

const nextConfig: NextConfig = {
 cacheComponents: true,
 partialPrefetching: true,
}

export default nextConfig

Setting partialPrefetching: false only makes sense if the app had already adopted Cache Components before Partial Prefetching existed and the migration to it hasn't started yet. Outside that specific case, the two flags are born together and, in Next 17, they won't even exist as an option anymore: the behavior disappears and becomes the only one.

The requirement that eliminates the Edge runtime

Before touching a single line of a component, it's worth running a repository-wide search for runtime = 'edge' and runtime = 'nodejs'. Cache Components requires the Node.js runtime and doesn't support the runtime route configuration. The documentation is categorical: remove any runtime export, including the (already deprecated) edge one and the explicit nodejs one. Other server-side JavaScript runtimes aren't guaranteed to work. If the app has API routes or middleware pinned to Edge for latency reasons, that's the first architecture decision the migration forces, even before any use cache.

What breaks first: fetch turning dynamic

Here's the behavior change that catches out most people who migrate without reading the fine print: with cacheComponents on, data fetching is dynamic by default. This inverts the App Router's previous model, in which fetch cached implicitly unless you said otherwise. Now it's the opposite: nothing is cached until you explicitly mark it with use cache.

In practice, this tends to show up in two ways in the first build after enabling the flag:

  • Routes that used to show up as static in the next build report start being rendered dynamically on every request, because they depended on fetch's implicit cache and nobody added use cache anywhere.
  • Components that fetched inside a Server Component with no directive at all keep working, but lose the static shell that used to be served instantly, because everything there is now treated as dynamic.

The way to find this is to compare the next build route report before and after enabling the flag, route by route, looking for where a page that was static became fully dynamic with no visible code change. That diff is the map of where use cache directives are missing.

Where to put 'use cache': component, function, or page

use cache is granular by design: you can mark a single function, an entire component, or the page. The right choice depends on where the expensive data is. A Server Component that only fetches product reviews is a good candidate for caching at the component level:

tsx
async function Reviews({ productId }: { productId: string }) {
 'use cache'
 const reviews = await getReviews(productId)
 return <ReviewsList reviews={reviews} />
}

A utility function called from several places, on the other hand, benefits more from caching at the function itself, instead of duplicating the directive in every component that consumes it. The practical rule I'd follow: start by marking the data-access functions (the layer that talks to the database or an external API), not the presentation components. This avoids caching entire JSX trees when only a piece of them depends on slow data.

cacheLife and cacheTag: controlling when to revalidate

use cache on its own caches, but doesn't say for how long or how to invalidate on demand. That's where cacheLife and cacheTag come in, two functions the documentation lists as unlocked by cacheComponents, although the details of how each one behaves live on their own reference pages, not on the flag's main page:

tsx
import { cacheLife, cacheTag } from 'next/cache'

async function getProduct(id: string) {
 'use cache'
 cacheLife(/* desired duration */)
 cacheTag(`product-${id}`)
 return db.product.findUnique({ where: { id } })
}

Based on the function names and the pattern the rest of the cache API follows, a reasonable reading is that cacheLife handles the cache entry's duration and cacheTag serves to mark the entry for on-demand invalidation, useful when a mutation elsewhere in the app needs to drop that specific cache. But it's worth checking each function's reference page before using it in production: the cacheComponents page only confirms they exist and become available when the flag is on, without detailing exact parameters or behavior on that specific page.

Migrating the old flags

Anyone who was already using experimental.useCache or experimental.dynamicIO in Next 15 doesn't just swap one flag for another: the documentation points to the version 16 upgrade guide for that specific case, because the behavior changed along with the stabilization. The same goes for anyone who had experimental.ppr or experimental_ppr at the route segment level: those options were removed, because Partial Prerendering is now the App Router's default behavior when cacheComponents is on. You can't just delete the old flag and turn on the new one without reviewing what depended on it.

Activity and the loader that seems to have vanished

The least obvious side effect of the migration shows up in navigation, not on the first load. With cacheComponents active, Next starts using React's Activity component to preserve state between client-side navigations. Instead of unmounting the previous route, it becomes hidden: the form state or an expanded section's state stays put when the user comes back, and effects are cleaned up when the route is hidden and recreated when it reappears.

This explains a common post-migration symptom: a loading.tsx or a spinner that no longer appears on the second visit to a route, because the route was never actually unmounted enough to need reloading. It's not a bug, it's the new navigation model. Next's own documentation points to a dedicated guide, "Preserving UI state," for UI patterns that break under this behavior, such as dropdowns, dialogs, and test scenarios that assumed a remount.

How I'd measure before and after

Without running this on a real app, I won't make up a TTFB number or a cache hit rate. The roadmap I'd follow to measure this honestly is this:

  1. Run next build before enabling the flag and save the route report (static, dynamic, ISR) as a baseline.
  2. Enable cacheComponents and partialPrefetching, run next build again, and compare route by route: which page changed from static to dynamic with no use cache anywhere.
  3. With the app running on next start, open the DevTools Network tab and compare the TTFB of the same route across both versions, looking at the Server-Timing header when present, instead of just the total load time.
  4. Repeat client-side navigation between two routes several times to observe whether Activity's behavior changes what shows up in the spinner or the form state.

Only after this route-by-route comparison can you say whether the migration brought a faster UI or just moved where the cost shows up. This remains open, even for those who've already migrated: the documentation still doesn't detail standardized cache-hit metrics in Next's own report, so that part of the measurement keeps depending on each team's own instrumentation.

Translated from the Brazilian Portuguese original · Read the original