Skip to main content
Version: Latest
FeatureAIv1.4.3open-dxp/headless-bundle

Frontend Cache

A headless frontend often caches the API responses it renders. When an editor changes a page, the frontend should rebuild only the parts that show this page. The frontend cache makes this possible.

Every API response carries the header X-Cache-Tags. The header lists the elements in the response, for example document_42,asset_12. The frontend stores the response under these tags. When an element changes, OpenDXP sends its tags to the frontend. The frontend then drops the cache entries with these tags and keeps all others.

This works with every frontend that can cache by tag, such as Next.js, Nuxt or TanStack Start.

Note

The frontend cache is for frontends with a cache of their own, like the data cache of Next.js. If only Varnish caches the API, FOSHttpCacheBundle already clears it. You may not need the frontend cache then.

Requirements​

The frontend cache builds on the HTTP cache of OpenDXP. Set it up first, as described in HTTP Cache.

Configuration​

Enable the frontend cache and set a secret. The frontends receive the secret as a bearer token.

# config/packages/headless.yaml
opendxp_headless:
frontend_cache:
enabled: true
secret: '%env(FRONTEND_CACHE_SECRET)%'
path: /api/revalidate # the default

Then tick Flush Frontend Cache in the headless settings of each site that should receive the tags. See Site-based Host Configuration.

The bundle provides the service OpenDxp\Bundle\HeadlessBundle\HttpCache\FrontendCacheClient. It is a proxy client of FOSHttpCacheBundle. The project decides how FOSHttpCacheBundle uses it.

Without a proxy​

If the frontends are the only caches, the frontend cache client is the proxy client:

# config/packages/fos_http_cache.yaml
fos_http_cache:
tags:
enabled: true
cache_manager:
custom_proxy_client: OpenDxp\Bundle\HeadlessBundle\HttpCache\FrontendCacheClient

With a proxy like Varnish​

FOSHttpCacheBundle sends its invalidations to one proxy client. To reach Varnish and the frontends, the project combines both in a MultiplexerClient:

# config/services.yaml
services:
app.http_cache.proxy_clients:
class: FOS\HttpCache\ProxyClient\MultiplexerClient
arguments:
- ['@fos_http_cache.proxy_client.varnish', '@OpenDxp\Bundle\HeadlessBundle\HttpCache\FrontendCacheClient']
# config/packages/fos_http_cache.yaml
fos_http_cache:
proxy_client:
varnish:
http:
servers: ['varnish:80']
tags:
enabled: true
cache_manager:
custom_proxy_client: app.http_cache.proxy_clients

The same works with every other proxy client of FOSHttpCacheBundle. Replace varnish with its name.

The endpoint of the frontend​

OpenDXP sends the tags at the end of each request, command or worker message that changed something. Each site receives one request:

POST https://{main domain of the site}/api/revalidate
Authorization: Bearer {secret}
Content-Type: application/json

{"tags": ["document_42", "document_list"]}

The frontend checks the secret. It drops every cache entry with one of the tags and answers with a 2xx status.

If a frontend cannot be reached, the other frontends still receive their request. OpenDXP writes the failure to the headless log, see Backend Logging.

Example: Next.js​

Next.js stores the result of a function under the tags the function sets. This needs cacheComponents: true in next.config.js.

// src/api/routeDocument.ts
import { cacheTag } from 'next/cache';

export async function getRouteDocument(uri: string) {
'use cache';

const response = await fetch(`${process.env.API_BASE_URI}/route-document/${uri}`);
cacheTag(...(response.headers.get('X-Cache-Tags') ?? '').split(',').filter(Boolean));

return response.json();
}

The endpoint drops the cache entries of the tags it receives:

// src/app/api/revalidate/route.ts
import { revalidateTag } from 'next/cache';

export async function POST(request: Request) {
if (request.headers.get('Authorization') !== `Bearer ${process.env.FRONTEND_CACHE_SECRET}`) {
return new Response(null, { status: 401 });
}

const { tags } = await request.json();
for (const tag of tags) {
revalidateTag(tag, { expire: 0 });
}

return Response.json({ success: true });
}

Upgrading from api_flush_cache_trigger​

api_flush_cache_trigger is deprecated since 1.4 and will be removed in 2.0. It sends a request to each site on every change, and the frontend then drops its whole cache. It keeps working as long as the frontend cache is disabled.

  1. Configure the frontend cache as described above.
  2. Remove api_flush_cache_trigger from your configuration.
  3. Change the endpoint of your frontend so that it drops only the entries of the tags it receives.
  4. Remove project classes that extend ApiTriggerListener.

When the frontend cache is enabled, ApiTriggerListener sends no more requests. The Flush Frontend Cache setting of each site stays as it is.