Skip to content

Control HTTP caching of static assets served by client() #8

Description

@knoan

challenge / use case

client() (src/handlers/client.ts) serves the application bundle through express.static with default options,
so every file, including content-hashed bundles, is served with Cache-Control: public, max-age=0. Observed on
@metreeca/gate 0.13.0:

Resource Cache-Control
index.html / SPA navigation public, max-age=0
hashed bundles (index-*.js/css) public, max-age=0
unhashed public files (docs, images) public, max-age=0

Every page load revalidates every bundle, and downstream caches (Cloud CDN, Firebase Hosting) store nothing.

proposal

Let client() apply a cache policy per resource class:

  • index.html: no-cache, so deploys are picked up immediately
  • content-hashed files: public, max-age=31536000, immutable
  • other static files: a short, configurable max-age

Hashed files are identified by an application-declared path prefix, e.g. client(path, { immutable: "/_/" }),
matched to the build's assetsDir, rather than guessed from file names.

alternatives

  • Detecting hashed files by matching the bundler's hash suffix in file names: needs no application change, but is
    fragile across bundler versions and naming configurations.
  • Setting headers in each application ahead of client(): works, but duplicates the policy in every consumer.

notes

Downstream, ec2u/pipe would switch assetsDir in packages/client/vite.config.ts to a dedicated directory to adopt it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    • Status
      Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions