Skip to content

Adapters should add cache-control headers to static assets #3194

@Rich-Harris

Description

@Rich-Harris

Describe the problem

As noted in #909 (comment), most adapters don't configure cache-control headers for the three categories of static assets — hashed (Vite output), static directory contents, and prerendered pages.

Describe the proposed solution

For hashed assets there's no real reason they shouldn't have cache-control: public, immutable, max-age=31536000.

For the contents of the static directory, it really ought to be configurable. This could be done on a per-adapter basis (e.g. there's a PR for adapter-cloudflare-workers here), but since it's a universal requirement I think it belongs in the main config. Straw man:

// svelte.config.js
export default {
  kit: {
    static: {
      // cache everything in `static` for 1 hour
      cache: 3600

      // cache large images for one hour, small images for ten minutes, don't cache anything else
      cache: ({ filename, size }) => {
        if (filename.startsWith('images') {
          return size > 1_000_000 ? 3600 : 600;
        }

        return 0;
      }
    }
  }
};

Prerendered pages should adhere to whatever the cache-control header was at the time of prerendering. For some platforms it may be difficult or impossible to set cache headers for static assets, but here we can at least cache stuff in the browser with a <meta http-equiv> tag.

Alternatives considered

No response

Importance

would make my life easier

Additional Information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions