Enable Cache Components
Next.js 16 introduces Cache Components that replaces the old implicit caching model and makes data fetching dynamic by default. You have to use the 'use cache' directive to perform caching giving you more control over what is cached and their duration.
For a SitecoreAI Content SDK application, this means:
-
Editing and preview modes always bypass the cache.
-
Every component is handled individually and you should not wrap the
<Placeholder/>component with<Suspense/>. -
Cached data is automatically refreshed after a set duration using
cacheLife. -
You can manually invalidate cached data after a mutation using
revalidateTag,updateTag, orrevalidatePath. -
You can render content at runtime using the
connection()function and not pre-render at build time. This usually occurs when you access external information that you intentionally want to change the result of a render, such asMath.random()ornew Date().
To set up Cache Components in your Content SDK Pages Router app:
-
In your
next.config.tsfile, enable thecacheComponentsflag as shown: -
All route handlers are dynamic by default when using Cache Components. The route segment configuration in your route handlers is therefore incompatible and must be removed. In the following files:
-
src/app/api/editing/config/route.ts -
src/app/api/editing/render/route.ts -
src/app/api/sitemap/route.ts -
src/app/api/robots/route.ts
Remove the following line:
-
-
You must create cached versions of your data fetching functions, especially for page, dictionary, and error page requests. In
src/lib, create a file calledcached-functions.tsand add the following: -
After creating the cached versions of your data fetching functions, you must now update references to use them.
-
In
/[[...path]]/page.ts, replace instances of theclient.getPagefunction in the component andgenerateMetadatafunctions with thegetPagefunction fromsrc/lib/cached-functions.ts:ImportantDo not wrap the component with
'use cache';.In the
draft.isEnabledblock, do not routeclient.getPreviewandclient.getDesignLibraryDatathrough cache helpers. -
In
src/app/not-found.tsxand/[[...path]]/not-found.tsx, replace instances ofclient.getErrorPagewith thegetErrorPagefunction fromsrc/lib/cached-functions.ts:ImportantDo not apply
'use cache';to the entireNotFoundcomponent. The file relies ongetCachedPageParams()inside the cached boundary, and React cache boundaries do not align with Next.js cache context in this case. -
In
global-error.tsx, verify that it renders a dedicated layout that does not reference any cache components, as global-error is a client component. You will most likely need to introduce a separate layout rather than using a shared one. -
In
i18n/request.ts, replace instances ofclient.getDictionarywith thegetDictionaryfunction fromsrc/lib/cached-functions.ts:
-
If your SitecoreClient or another fetch layer also uses Next.js fetch caching options such as cache: 'force-cache', next: { revalidate }, or next: { tags }, then you effectively have two caching layers.
-
'use cache'on helper functions likegetPageandgetDictionary, which caches at the application level and can be invalidated withrevalidateTagusingcacheTagnames. -
The fetch data cache, controlled by
fetchoptions and invalidated through tags on fetch requests if you use them.
To keep behavior predictable, it is recommended to prefer one main caching strategy and avoiding conflicting time-to-live (TTL) assigned to the same GraphQL response. If you use both tag systems, make sure webhook invalidation covers both naming schemes.