Cache storefront pages on a CDN
On this page
A CDN (content delivery network) in front of your store can answer requests for public pages from a stored copy, without the request reaching StoreConnect. Pages load faster for shoppers, and a traffic spike lands on the CDN instead of the store.
Use this process to turn on page caching for chosen page types, keep your theme cacheable, and check whether a page was cached. Caching is off until you turn it on, so a store that does nothing keeps working as before.
This is different from the cache tag, which stores a rendered fragment inside StoreConnect. Page caching stores the whole page on the CDN.
How page caching works
When a page can safely be shown to every visitor, StoreConnect sends it with headers that tell the CDN to keep a copy. A page can be shared only if nothing on it belongs to one visitor, so a cacheable page:
- starts no session, and sets neither the
store_connect_sessionsession cookie nor thesc_tokencart cookie - renders without a CSRF security token in the forms and meta tags
- renders as it would for a new visitor, cookie banner included
The first visitor action that needs a session, such as adding to cart or signing in, reaches StoreConnect and creates the session as usual. From then on the visitor carries the session cookie, and the CDN must send their requests straight to StoreConnect.
Before you start
- Set up your own CDN in front of your store domain. StoreConnect does not provide one. The CDN must honor the
CDN-Cache-Controlheader. - In the CDN, add a rule that bypasses the cache for any request that carries the
store_connect_sessioncookie. StoreConnect cannot enforce this rule itself. Without it, a signed-in shopper or a shopper with items in their cart can be shown a copy cached for someone else.
Page types you can cache
Only these page types can be cached, and only when you list them.
| Page type | Pages |
|---|---|
home |
The store’s home page |
page |
A content page |
article |
The article index and an individual article |
product |
The product index and a product page |
category |
A category page |
search |
Search results |
locations |
The location index and a location page |
Nothing else is ever cached, including the cart, checkout, the customer account pages and every form submission.
Turn on page caching
- Open the Store record and go to the Store Variables related list.
- Click New.
- Enter
page_cache.typesas the Key. - Enter the page types to cache as the Value, separated by commas. For example,
home, page, product, category. - Save the record.
- (Optional) Repeat steps 2 to 5 to add
page_cache.ttlandpage_cache.stalevariables, which change how long the CDN keeps a page. See Cache lifetimes. - Request a page of a listed type in a private browser window, and check the response headers as described in Check whether a page was cached.
Pages of the listed types now return X-SC-Cache: cacheable and a CDN-Cache-Control header, and the CDN serves repeat requests for them from the stored copy.
:::warning
Before you list home or page, check for custom forms on those pages. A custom form on a cached page fails on submit unless the form is set up for it. See Forms on a cached page.
:::
To turn caching off, delete the page_cache.types variable or clear the value.
Cache lifetimes
These store variables set how long the CDN keeps a page. The values are whole numbers of seconds.
| Key | Default | Effect |
|---|---|---|
page_cache.types |
Not set, so caching is off | The page types to cache, separated by commas. Case and spaces do not matter. |
page_cache.ttl |
60 |
How many seconds the CDN serves the copy before it counts as stale. At most 86400 (one day). |
page_cache.stale |
600 |
How many seconds after that the CDN may keep serving the stale copy while it fetches a fresh one. At most 604800 (seven days). |
page_cache.ttl.<type> |
The page_cache.ttl value |
page_cache.ttl for one page type only, such as page_cache.ttl.product. |
page_cache.stale.<type> |
The page_cache.stale value |
page_cache.stale for one page type only, such as page_cache.stale.product. |
0 is allowed. A blank, negative, decimal or non-numeric value, or one over the limit, is ignored: a page type’s value falls back to the store-wide value, and that falls back to the default.
For example, to keep product pages for five minutes while every other page type uses the defaults, add page_cache.ttl.product with the value 300.
A change applies to the next page StoreConnect sends. A copy the CDN already holds keeps the lifetime it was stored with.
Pages that are never cached
Even when the page type is listed, a page is never cached when:
- it requires sign-in or is restricted to a membership
- the request carries the
store_connect_sessioncookie - the visitor has a saved cart, which happens when a returning guest’s session has ended but their cart cookie has not
- IP lookup is enabled for the account, which turns caching off for all of the account’s stores
- it is opened in the editor, an editor preview or through a preview link
- the request is not a
GETorHEAD - the URL carries the
_sc_nocacheparameter
A page refused for any of these reasons is sent with the session and cookies as normal.
Keep a theme cacheable
A page that passes the checks above can still use something that belongs to one visitor while it renders. StoreConnect watches for this and refuses to cache the page if any of the following happen. The page is still sent to the visitor, just without the cache headers.
X-SC-Cache value |
What the page did |
|---|---|
not-success |
Returned something other than a success, such as a redirect, a not-found page or an error. A 304 Not Modified counts as a success. |
flash |
Set a notice or alert message, for this page or the next. |
session-accessed |
Wrote to the session, for example with a session tag or a Liquid controller action such as pricebook.set or outlet.set. Writing a value and then removing it still counts. Reading the session does not. |
request-data-read |
Read a current_request field that is not part of the URL, asked for the store suggested for the visitor’s location (current_store.geolocation.suggested_store), or was requested over XHR or with an Accept header asking for a different format from the one in the URL. |
cookie-set |
Set any cookie, including through a header tag. |
These current_request fields come from the URL, so a cached page can read them: url, path, fullpath, host, protocol, query_string, params, base_url, canonical_url, canonical_path, local_path, store_path and page_number. Reading any other field, such as ip, content_type or a geolocation_ field, or outputting the whole object with the json filter, stops the page caching. The shipped theme reads only URL fields.
Nothing a cacheable page writes to the session is kept. Campaign (UTM) parameters on the landing URL are not attributed, and signing in returns the shopper to a default page rather than the page they were on, unless the theme makes a bootstrap call.
Content that changes per visitor
A cached copy freezes whatever the page rendered the first time, and every visitor is handed that copy. Mark any component whose content differs per visitor, such as a cart menu, or goes stale quickly, such as a stock counter, with dynamic: true:
```liquid
{% component “cart-menu”, dynamic: true, reload: “sc.cart-updated” %} ```
A dynamic component renders as an empty placeholder in the page and is fetched fresh for each visitor after the page loads. See Dynamic components.
A component on a cacheable page cannot keep values with the context tag, because there is no session to keep them against. Its {% context %} values are dropped and a warning appears in the theme console.
Forms on a cached page
A CSRF token is stored in the visitor’s session, so a cacheable page carries none. Forms render without the hidden authenticity_token field, and the layout leaves out the csrf-param and csrf-token meta tags. This happens every time a cacheable page renders, whether it comes from the CDN or straight from StoreConnect, so it starts as soon as you list the page type.
These forms check the browser’s Origin header instead of the token, so they keep working on a cached page:
- add to cart, add bundle to cart and add CPQ preset to cart
- the CPQ bundle configurator on a product page
- the cookie banner’s accept, reject and settings
- express checkout wallets on the product page, such as Apple Pay and Google Pay
- a generic custom form whose Form record has Same-site origin (cached pages) selected in Alternative Verification Methods
Any other form on a cached page fails when submitted, and the shopper sees “Your session has expired or an unexpected error occurred. Please try again.” That includes a custom form without the setting above, on a home or content page you have listed. Move the form to a page type you do not cache, or turn on the setting for that form.
The Origin header must match the store’s own origin exactly, including https. If the CDN connects to StoreConnect over plain http, these forms are rejected.
Theme JavaScript that reads the csrf-token meta tag gets nothing on a cached page. In the shipped theme this affects @rails/ujs, theme/utils/fetch.js and the remove-item links in the cart dropdown. A bootstrap call restores the token.
Check whether a page was cached
Every storefront page response carries an X-SC-Cache header, whether caching is on or not. It says cacheable, or names the first reason the page was not cached.
For example, request a product page and print the headers:
```bash
curl -sI https://shop.example.com/products/linen-shirt | grep -i -E ‘x-sc-cache|cdn-cache-control|cache-control’ ```
A cacheable page returns:
```text
x-sc-cache: cacheable cdn-cache-control: max-age=60, stale-while-revalidate=600 cache-control: private, no-store ```
CDN-Cache-Control is read only by the CDN. Cache-Control: private, no-store stops browsers keeping the page, so a shopper who signs in never goes on seeing an anonymous copy. A cacheable page keeps Vary: Accept-Encoding and drops any other Vary value. StoreConnect sets these headers after the theme has rendered, so a header tag that sets Cache-Control, CDN-Cache-Control or Vary has no effect on a cacheable page.
X-SC-Cache value |
Meaning |
|---|---|
cacheable |
The page was sent with the cache headers. |
off |
The store has no page_cache.types variable. |
not-get |
The request was not a GET or HEAD, such as a form submission. |
type-not-eligible |
The page has no page type, or the type is not listed in page_cache.types. |
bypass-marker |
The URL carries _sc_nocache. |
session-cookie |
The request carries the store_connect_session cookie. |
geolocation |
IP lookup is enabled for the account. |
edit-mode |
The page was opened in the editor, an editor preview or a preview link. |
cart |
The visitor has a saved cart. |
login-required |
The page requires sign-in or a membership. |
not-success, flash, session-accessed, request-data-read, cookie-set |
See Keep a theme cacheable. |
X-SC-Cache reports what StoreConnect decided, not what the CDN did. A page served from the CDN copy carries the value it was stored with, cacheable. To see whether the CDN served the copy, read the CDN’s own header, such as cf-cache-status on Cloudflare.
Responses that do not come from a storefront page, such as theme assets and component fetches, carry no X-SC-Cache header.
Restore the session after a cached page loads
A page served from the CDN never reaches StoreConnect, so that visit gets no session, cart cookie or CSRF token, and the landing URL is not recorded. A theme can restore all four with one request after the page loads. This is up to the theme; the shipped theme does not make the call, and caching, dynamic components and the forms above work without it.
Send POST /async/bootstrap, under the store’s path if it has one, with a JSON body:
```json
{ “url”: “https://shop.example.com/collections/sale?utm_source=newsletter”, “components”: [ {“nonce”: “8f3c1e7a-0d2b-4c55-9e61-2a7f3b9c1d04”, “token”: “eyJfcmFpbHMi…”} ] } ```
urlis required. Send the page the visitor landed on, normallywindow.location.href. It must be an absolutehttporhttpsURL on the same host, or the call returns422with{"error": "invalid url"}and writes nothing.componentsis optional. Each entry is a component placeholder’sdata-nonceanddata-token. A call renders at most 20 components; any past that return{"error": "too many components"}and can be fetched individually.
The call sets up the session and cart cookie, records campaign parameters and the return-to page from url, and returns a csrf_token and the rendered components keyed by nonce:
```json
{ “csrf_token”: “…”, “components”: { “8f3c1e7a-0d2b-4c55-9e61-2a7f3b9c1d04”: {“html”: “…”} } } ```
Write csrf_token into the page’s csrf-token meta tag so theme JavaScript that reads it works. A component entry returns {"error": "invalid token"} or {"error": "render failed"} when it cannot be rendered, without affecting the others.
If the bootstrap call fails or times out, reload the page past the cache by adding _sc_nocache=bootstrap_failed or _sc_nocache=bootstrap_timeout to the URL.
A page makes one bootstrap call. Mark components with batch: true or batch: "<group>" to tell the theme’s loader which ones to include in it; StoreConnect only adds a data-batch attribute to the placeholder.
If a bootstrap call arrives with the session cookie already set, the CDN served a cached page to a visitor it should have bypassed. StoreConnect logs a warning to the theme console and handles the call normally. Check the CDN’s bypass rule if you see this warning.
To turn the endpoint off, add the store variable page_cache.bootstrap_disabled with the value true. Every call then returns 404 with {"error": "disabled"}.
Was this article helpful?
Thanks for your feedback! It helps us improve our docs.