Caching

← All configuration scenarios

To reduce asset downloads, set a freshness policy for browsers and shared HTTP caches. To reduce repeated TLS handshake work, enable session reuse on the TCP listener. These settings control different parts of a request.

Cache a static asset

First publish the example app.css. Keep its manifest and replace its server profile with the following. Restart the server with the updated profile.

JSON
{
  "listen": { "port": 9000 },
  "static": {
    "directories": [{
      "path": "public/assets",
      "prefix": "/assets",
      "mode": "manifest",
      "cache_control": "public, max-age=300",
      "etag": true
    }]
  }
}

cache_control sets the Cache-Control header. Here a cache may reuse the response for five minutes. etag adds a validator so a client can check whether its stored copy still matches. It defaults to false. For content-hashed filenames that change with each edit, a longer lifetime with immutable lets a deployment publish a new URL.

Verify a 304 response

Use a curl build with ETag support. Save the tag from the first response, then send it back on a second request.

Shell
url=http://127.0.0.1:9000/assets/app.css
curl -i --etag-save asset.etag "$url"
curl -i --etag-compare asset.etag "$url"

The first request returns 200, the stylesheet, Cache-Control and ETag. With the same representation the second returns 304 and an empty body. A changed representation needs a fresh download. In manifest mode, publish a new immutable build and restart after changing assets. Precompressed variants have distinct tags and vary by Accept-Encoding.

Set a policy in a PHP handler

For a personalized response, set its policy in the handler. For example, no-store tells HTTP caches to avoid storing the response. The static directory’s ETag setting only applies to files served by that directory.

PHP
return Response::text('Your account', 200, [
    'Cache-Control' => 'no-store',
]);

Reuse TLS sessions

On a TLS-enabled profile, set feature.tls_session_resumption to "auto" and inspect server.features at startup. When supported, TCP workers share an encrypted cache of up to 4096 session slots with a five-minute lifetime. Restarting the serving process or rotating the certificate clears it. QUIC uses the selected backend’s TLS handling. See Runtime features.