Skip to content

Caching

Caching is the process of storing data so that future requests for that data load faster. Instead of generating a response from scratch for every request, the server returns a cached copy, reducing the load and speeding up response times. To deliver content and assets as fast as possible, Storyblok leverages a Content Delivery Network (CDN) powered by Amazon CloudFront.

Storyblok’s CDN caches two types of data at endpoints around the world:

  • Content: Content Delivery API responses for requests of published content, for up to seven days.
  • Assets: Images, videos, documents, and other static files, including image variations generated by the Image Service, for up to one year.

The CDN caches Content Delivery API responses, reducing latency and offloading traffic from the backend. Use this cache to build fast websites and applications that stay within the API’s rate limits.

The Content Delivery API enforces rate limits on uncached requests. For example, the limit for single stories is 50 per second, whereas the limit for requests containing 75 to 100 stories is 6 per second. In contrast, cached requests from the CDN allow over 1000 requests per second. Find the complete rate limit table in the Content Delivery API reference. Maximizing cache usage is the most effective way to stay within these limits.

To ensure a high cache hit rate for published content, Storyblok uses the cv parameter. The cv is a space-specific numeric counter (a Unix timestamp) that increments with each content change within the space. It serves as a cache key: the CDN serves repeat requests with the same cv directly from the cache.

The stories, datasources, and datasource entries endpoints return the current cv as a top-level property of the response. The links, tags, and spaces endpoints don’t include it.

The API redirects a request to the same URL with the latest cv value in the following cases:

  • The request has no cv parameter.
  • The request has an outdated or invalid cv value that the CDN holds no cached response for.
Terminal window
GET https://api.storyblok.com/v2/cdn/stories?token=[PUBLIC_ACCESS_TOKEN]
# 301 Redirect
GET https://api.storyblok.com/v2/cdn/stories?cv=1735645795&token=[PUBLIC_ACCESS_TOKEN]

The CDN caches the response to the first request for this exact URL and serves every subsequent request with the same cv value directly from the cache, without hitting the backend.

The redirect itself reaches the backend and counts as an uncached request toward the rate limits, the API request quota, and traffic. Pass the latest cv to skip it.

While the stories, datasources, and datasource entries endpoints of the Content Delivery API return the current cv value, the most efficient way to retrieve it is via the spaces endpoint:

Terminal window
GET https://api.storyblok.com/v2/cdn/spaces/me?token=[PUBLIC_ACCESS_TOKEN]
{
"space": {
"id": 123456,
"name": "Storyblok",
"domain": "https://www.storyblok.com/",
"version": 1544117388
}
}

The version property is the space’s current version: a Unix timestamp that updates with every content change. The spaces endpoint response has no cv property, and Storyblok only caches it for two seconds. That makes it ideal for retrieving content changes without going through the long-lived cache of the content endpoints.

Unless you configure a TTL for the access token, version is also the latest cv value. To maximize cache efficiency, use it as the cv query parameter in all subsequent API calls.

When you publish new content, the version value updates, but previous responses remain cached under the old cv. To retrieve up-to-date content, fetch the updated version and use it as the new cv parameter.

Fetching, storing, and invalidating the cv parameter should be part of your application architecture. Use cron jobs, custom events, or Storyblok’s webhooks to detect when to retrieve a new cv.

After you publish a story, version can take a few seconds to update, so an immediate polling of the spaces endpoint may return the old value. Your application detects a publish within the polling interval plus that delay.

The CDN caches assets uploaded to Storyblok, as well as optimized image variations generated by the Image Service. The origin serves the first request for an asset, and the CDN serves subsequent requests from its cache to minimize latency. Refer to the assets concept for further information.

The CDN also caches image variations created on demand using the Image Service. The CDN caches a particular variation the first time it receives the request.

After a long period without requests, the server clears the cached variation, and the next request regenerates it. Pages with low traffic encounter this most often: the first image load after a quiet period takes noticeably longer, but subsequent loads are fast.

To identify why the image load is slow, inspect the x-cache response header:

  • A Miss from cloudfront value means the CDN generated a variation for that request.
  • A Hit from cloudfront value means the CDN served it from cache.

Storyblok invalidates an asset’s cache automatically when you replace or change the asset, for example with the Image Editor or the Replace Assets app). The next request serves the new version, and Storyblok caches it from then on. You don’t need to clear the cache manually.

The following strategies help optimize API usage and cache performance. Each strategy addresses a different aspect of caching, and combining them yields the best results.

One strategy applies to every project.

First and foremost, always include the cv in API requests. This is the single most effective optimization.

The following strategies keep cached content up to date.

Use Storyblok’s webhooks to detect content changes and trigger cache invalidation in your application. For example, to update the cv to the most up-to-date value, trigger a webhook whenever someone publishes, updates, moves, or deletes a story. Refer to the webhooks developer concept for further information.

Setting a time to live (TTL) is most effective for high-traffic projects that send many requests to the Content Delivery API and update content frequently. It reduces uncached API calls and lowers response times. However, it introduces a trade-off: content updates don’t appear until the TTL expires and a new change occurs.

By default, the cv value changes with every content update, which means previously cached responses become stale immediately. The TTL setting for public access tokens overrides this behavior by making the cv persist for a defined duration. Refer to the access tokens developer concept for further information.

Setting a TTL value (in seconds) on a public access token freezes the cv for that duration. During the TTL window, all API requests using that token receive the same cv. Therefore, after the first request for each URL, the CDN serves all responses from the cache.

After the TTL window has expired, the cv doesn’t change automatically. The next content change triggers a new cv. The first API request after that change retrieves fresh content and creates a new cache entry.

For example, a TTL of 300 seconds (five minutes) means:

  1. The first request retrieves content and caches it with the current cv.
  2. All requests in the next five minutes receive the cached response, with the exact same content—even if content changes in the meantime.
  3. The next content change after five minutes generates a new cv, and the first subsequent request creates a fresh cache.

Concretely, the cv returned to a token with a TTL is the space’s version rounded down to the nearest multiple of the TTL:

cv = floor(version / ttl) * ttl

A space at version 1786950860 served through a token with a TTL of 3600 therefore receives cv=1786950000, while the spaces endpoint keeps reporting 1786950860.

Sending the space’s raw version as the cv results in a redirect to the rounded value on every request. Don’t compare the two values or substitute one for the other.

If your project makes both server-side and client-side API calls, a shared cache that uses the same token can be beneficial. To achieve that, retrieve the cv server-side and expose it to the client via a dedicated endpoint.

The architecture of your application affects how well it uses the cache.

Consider that the rendering mode of your application determines how and when it makes API requests, which directly affects caching behavior:

  • Client-side rendering (CSR) and server-side rendering (SSR): The application fetches data for every user request. Implement a caching strategy that reduces the number of requests the backend processes. Consider how often your content changes to determine cache invalidation frequency.
  • Static site generation (SSG): During the build process, the application fetches all content at once, which can be aggressive on rate limits. To avoid rate limit errors, use the multiple stories endpoint rather than the single story endpoint. Consider that some static builds run with multiple workers, which may impact the benefits of the cv.

As explained further under cache version, the cv is space-specific rather than story-specific.

Monitoring and error handling help you detect and recover from rate limit issues.

Track request volume and rate limit status to detect potential issues before they affect your application. Use the space dashboard to monitor consumption metrics. Refer to the space dashboard manual for further information. Additionally, in your application, log rate limit errors.

With a solid caching strategy in place, operating within Storyblok’s rate limits should be the norm. Nevertheless, implement retry logic with exponential backoff to handle HTTP 429 responses.

Your own infrastructure gives you more control over caching.

For greater control over asset caching, consider setting up a custom assets domain. This allows to defining custom cache headers and TTL policies, and leveraging any existing CDN infrastructure. Refer to the custom assets domain documentation for setup guides.

Beyond Storyblok’s CDN, consider implementing cache layers in your own application. Browser caching reduces redundant network requests for returning visitors. Application-level caching reduces the number of requests to Storyblok’s API during server-side rendering or build processes. For example, store API responses in memory or in a key-value store. To keep cached entries fresh, set cache durations based on how often your content changes, and use webhooks to invalidate stale entries.

Storyblok’s SDKs manage the cv parameter for you.

Storyblok’s JavaScript libraries use the JavaScript client, which handles the cv parameter automatically. When the application makes its first API call to the stories endpoint, the client stores the cv from the response in memory and reuses it for all subsequent calls. The stored cv always comes from a content response, so it stays correct for access tokens with a TTL.

Override the cached cv by passing a custom value in an API request:

import StoryblokClient from "storyblok-js-client";
const Storyblok = new StoryblokClient({
accessToken: "PUBLIC_ACCESS_TOKEN",
});
const response = await Storyblok.getStory("home", {
version: "published",
cv: "1735815318",
});

The client caches this value. Subsequent calls without a custom cv parameter reuse it:

// Results in GET /stories/home?cv=1735815318&version=published&token=[YOUR_TOKEN]
const response = await Storyblok.getStory("home", {
version: "published",
});

Call the flushCache() method to clear the client’s in-memory cache:

await Storyblok.flushCache();

Passing the current timestamp as the cv parameter sends every request to the backend, because the timestamp never matches the latest cv. The API redirects each request to the latest cv, and every redirect counts toward the rate limit.

const response = await Storyblok.getStory("home", {
version: "published",
cv: Date.now(),
});

Alternatively, calling the /stories/ endpoint with version: "draft" flushes the client cache. Use this approach only in preview or development environments.

Was this page helpful?

What went wrong?

This site uses reCAPTCHA and Google's Privacy Policy (opens in a new window).Terms of Service (opens in a new window) apply.