Surrogate cache control header is not set for 4xx responses

Created on 30 May 2025, 6 days ago

Problem/Motivation

The Surrogate Cache Control header is only set for response codes less than 400. I'm not sure why this distinction was made, but it's been in the code since support for surrogate control was introduced 5 years ago. I could see why we wouldn't want 5xx responses included in surrogate cache control (they wouldn't be anyway since those responses are not cacheable), but 404s should be cached in my opinion.

Steps to reproduce

Proposed resolution

Allow the surrogate cache control header to be set for any response code under 500.

Remaining tasks

User interface changes

API changes

Data model changes

πŸ› Bug report
Status

Active

Version

2.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States bkosborne New Jersey, USA

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @bkosborne
  • πŸ‡ΊπŸ‡ΈUnited States bkosborne New Jersey, USA
  • Pipeline finished with Success
    6 days ago
    Total: 289s
    #510505
  • πŸ‡§πŸ‡ͺBelgium dieterholvoet Brussels

    While this does seem like an oversight in the original implementation, this would be a backwards incompatible change for existing sites. To avoid having to create a new major version of the module, I propose we add two new settings: Surrogate 404 cache maximum age and Surrogate 5xx cache maximum age. If we keep these empty for both existing sites and new sites by default, there's no backwards compatibility break.

  • πŸ‡§πŸ‡ͺBelgium dieterholvoet Brussels

    While we're at it, let's add the Expert mode checkbox from the Caching fieldset to this fieldset as well.

  • πŸ‡ΊπŸ‡ΈUnited States bkosborne New Jersey, USA

    This would only apply to 4xx responses, because 5xx responses are already excluded from code above, which checks first if the original response is considered cacheable. It won't be with a 5xx code.

    I'll take a look at adding this option this week...

  • πŸ‡§πŸ‡ͺBelgium dieterholvoet Brussels

    That's a good point. It looks like the 5xx cache maximum age setting doesn't actually do anything. Even if you explicitly create a cacheable 500 response like this:

    return new CacheableResponse(status: 500);
    

    there's Drupal\Core\PageCache\ResponsePolicy\NoServerError who marks the request as non-cacheable. I'll create another issue to remove that setting.

  • πŸ‡§πŸ‡ͺBelgium dieterholvoet Brussels
Production build 0.71.5 2024