Problem/Motivation

When testing performance in https://pagespeed.web.dev/, Drupal sites will get a recommendation to "Use efficient cache lifetimes".
This is due to missing Cache-Control HTTP header for static files.

Steps to reproduce

Load your site in PageSpeed or inspect static files (CSS/JS/images) in Network tab of Chrome inspector.

Proposed resolution

As Varnish can alter this, it could be a good candidate to set such header.

Remaining tasks

MR to be submitted

Is there a reason this hasn't been proposed in the past ?
Could this be problematic when images are replaced for example ?

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

  • 4.x Comparechanges, plain diff MR !25

Comments

tostinni created an issue. See original summary.

tostinni’s picture

Status: Active » Needs review

Set to "needs review".

rushikesh raval’s picture

I have reviewed MR!25. It fixes issue as per summary.
I am moving it to RTBC+.

rushikesh raval’s picture

Status: Needs review » Reviewed & tested by the community

shumer made their first commit to this issue’s fork.

shumer’s picture

The original patch applies Cache-Control: public, max-age=31536000, immutable to all static files equally. This works great for cache-busted assets (CSS/JS with query strings like ?v=abc123), but is risky
for files without cache busting - e.g. uploaded images at /sites/default/files/photo.jpg. If someone replaces a file at the same URL, browsers will serve the stale version for up to a year without even attempting revalidation
(immutable prevents conditional requests).

What changed

The VCL now uses three tiers of caching:

  1. Backend-controlled TTL - if the backend sends an X-TTL header (e.g. via nginx config), Varnish uses that value. This gives site owners a way to control static file TTL without editing VCL.
  2. Cache-busted assets (URL contains ?) - 1 year + immutable. Safe because the query string changes when the file changes.
  3. Files without query string - 30 days, no immutable. A reasonable default that still scores well on PageSpeed but doesn't lock in stale content for a year.
shumer’s picture

Status: Reviewed & tested by the community » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.