← Workflows

阅读中文版本

HTTP Cache Headers Workflow

Configure HTTP cache headers for static assets and API responses to improve performance and reduce server load.

beginner~20 mincachinghttpworkflow

Objective

Set appropriate cache headers to optimize loading performance.

Environment

  • A web server or CDN configuration access
  • Understanding of Cache-Control and ETag headers
  • Static assets and API endpoints to configure

Steps

1. Identify cacheable resources

List static assets and API responses that can be safely cached.

Expected state: A list of cacheable resources with their cache strategies is created.

2. Set static asset headers

Configure long-lived cache headers with fingerprinted filenames for static assets.

Expected state: Static assets have Cache-Control: public, max-age=31536000, immutable.

3. Set API response headers

Configure appropriate cache headers for API responses, using ETag or Last-Modified.

Expected state: API responses have appropriate cache validation headers.

4. Verify with curl

Use curl -I to inspect the headers returned for each resource.

Expected state: The correct cache headers are present in responses.

Decision points

Use immutable for all static assets?

  • Yes, with fingerprinting
  • No, only for truly immutable files

Use immutable only when filenames are content-hashed; otherwise use a long max-age with revalidation.

Expected outcome

All cacheable resources have appropriate cache headers, verified with curl.

Verification

Run curl -I for each resource type and confirm the cache headers match the intended strategy.

Cleanup

No cleanup needed; cache configuration should be committed.

Cache headers are free performance

Proper cache headers dramatically reduce latency and server load. Static assets should be cached aggressively with fingerprinting.

curl -I https://example.com/static/app.abc123.js
# Should show: Cache-Control: public, max-age=31536000, immutable

Implementation Notes

  1. Identify cacheable resources — List static assets and API responses that can be safely cached. Expected state: A list of cacheable resources with their cache strategies is created.

  2. Set static asset headers — Configure long-lived cache headers with fingerprinted filenames for static assets. Expected state: Static assets have Cache-Control: public, max-age=31536000, immutable.

  3. Set API response headers — Configure appropriate cache headers for API responses, using ETag or Last-Modified. Expected state: API responses have appropriate cache validation headers.

  4. Verify with curl — Use curl -I to inspect the headers returned for each resource. Expected state: The correct cache headers are present in responses.

Common Pitfalls

  • Use immutable for all static assets? Options: Yes, with fingerprinting / No, only for truly immutable files. Recommendation: Use immutable only when filenames are content-hashed; otherwise use a long max-age with revalidation.