---
title: "Enable Cache Components"
slug: "enable-cache-components"
abstract: ""
---
# 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`](https://nextjs.org/docs/app/api-reference/functions/cacheLife).
- You can manually invalidate cached data after a mutation using [`revalidateTag`](https://nextjs.org/docs/app/api-reference/functions/revalidateTag), [`updateTag`](https://nextjs.org/docs/app/api-reference/functions/updateTag), or [`revalidatePath`](https://nextjs.org/docs/app/api-reference/functions/revalidatePath).
- You can render content at runtime using the [`connection()`](https://nextjs.org/docs/app/api-reference/functions/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 as `Math.random()` or `new Date()`.

To set up Cache Components in your Content SDK Pages Router app:

1. In your `next.config.ts` file, enable the `cacheComponents` flag as shown:

    ```ts
    const nextConfig: NextConfig = {
      cacheComponents: true,
      ...
    };
    ```

2. 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:

    ```ts
    export const dynamic = 'force-dynamic';
    ```

3. Copy the [3 cached versions](https://github.com/Sitecore/content-sdk/tree/dev/packages/create-content-sdk-app/src/templates/nextjs-app-router-cache-components/src/lib/cache) of your data fetching functions for page, dictionary, and error page requests into the `src/lib/cache` folder.
4. After copying the data fetching functions, you must now update references to use them.
    - In `/[[...path]]/page.ts`, replace instances of the `client.getPage` function in the component and `generateMetadata` functions with the `getSitecorePage` function from `src/lib/cache`:

        ```ts
        // Before
        await client.getPage(path ?? [], { site, locale });

        // After
        import { getSitecorePage } from 'src/lib/cache/get-sitecore-page';

        const page = await getSitecorePage({ site, locale, path: path ?? [] });
        ```

        > [!important]
        > Do not wrap the component with `'use cache';`.
        >
        > In the `draft.isEnabled` block, do not route `client.getPreview` and `client.getDesignLibraryData` through cache helpers.

    - In `src/app/not-found.tsx` and `/[[...path]]/not-found.tsx`, replace instances of `client.getErrorPage` with the `getSitecoreErrorPage` function from `src/lib/cache`:

        ```ts
        // Before
        await client.getErrorPage(ErrorPage.NotFound, { site, locale });

        // After
        import { getSitecoreErrorPage } from 'src/lib/cache/get-sitecore-error-page';
        import { ErrorPage } from '@sitecore-content-sdk/nextjs';

        const page = await getSitecoreErrorPage({ site, locale, code: ErrorPage.NotFound });
        ```

        > [!important]
        > Do not apply `'use cache';` to the entire `NotFound` component. The file relies on `getCachedPageParams()` 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 of `client.getDictionary` with the `getSitecoreDictionary` function from the `src/lib/cache` folder:

        ```ts
        // Before
        await client.getDictionary({ site, locale });

        // After
        import { getSitecoreDictionary } from 'src/lib/cache/get-sitecore-dictionary';

        const dictionary = await getSitecoreDictionary({ site, locale });
        ```

        > [!important]
        > You must disable the SDK's dictionary cache in the client configuration (`sitecore.config.ts`) so that it doesn't clash with the Cache Components layer:
        >
        > ```ts
        > defineConfig({
        > ...
        > dictionary: { caching: { enabled: false } },
        > });
        > ```

## Add cache tags to cached reads

Using `'use cache'` together with `cacheLife()` enables time-based cache expiry, but it does not provide a mechanism for targeted cache invalidation.

To invalidate specific content on demand, such as after a Sitecore publish webhook, each cached data-fetching function must register deterministic cache tags using `cacheTag()`. The revalidation endpoint must then reconstruct and revalidate the same tag values.

The Content SDK provides helper functions that generate these tags consistently for both page rendering and webhook processing. Use the SDK helpers rather than constructing tag strings manually.

Import the helpers from the SDK package entry point:

```ts
import {
  collectSitecorePageCacheTags,
  buildSitecoreDictionaryCacheTag,
} from '@sitecore-content-sdk/nextjs';
```

The helper functions normalize tag segments before constructing tag values. Segments are:

- Converted to lowercase
- Sanitized so that `/, :,` and whitespace characters become `_`.

This ensures tags remain stable regardless of differences in URL casing, encoding, or webhook payload formatting.

> [!important]
> Because cached reads and webhook handlers use the same SDK tag builders, a Sitecore publish webhook or custom mutation flow can reconstruct the correct tag and invalidate the exact cache entries that were created during rendering.
>
> Without cache tags, Cache Components are limited to time-based expiration through `cacheLife()`. With cache tags, applications can support immediate, event-driven cache invalidation through `revalidateTag()` or `updateTag()`.

### `collectSitecorePageCacheTags`

The `collectSitecorePageCacheTags()` helper function returns a deduplicated `string[]` containing:

- The route tag
- The item tag associated with the rendered Sitecore item.

```ts
collectSitecorePageCacheTags({
  site,
  locale,
  path,
  route,
});
```

You can use this helper in page and error-page cache functions.

### `buildSitecoreDictionaryCacheTag`

The `buildSitecoreDictionaryCacheTag()` helper function returns the dictionary tag for a site and locale.

```ts
buildSitecoreDictionaryCacheTag({
  site,
  locale,
});
```

You can use this helper in dictionary cache functions.

## Avoid multiple caching layers

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 like `getSitecorePage` and `getSitecoreDictionary`, which caches at the application level and can be invalidated with `revalidateTag` using `cacheTag` names.
- The fetch data cache, controlled by `fetch` options 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.