Skip to content

pgs: Duplicate cache-control headers when using _headers file #215

@imcotton

Description

@imcotton

When setting custom cache-control headers in the _headers file, pgs.sh returns two cache-control headers in the HTTP response:

  1. The default cache-control from Souin middleware (e.g., max-age=60, must-revalidate)
  2. The custom cache-control from _headers file (e.g., public, max-age=31536000, immutable)

This creates ambiguity for browsers and CDNs about which cache directives to follow.

Expected Behavior

Only one cache-control header should be present, with user-defined headers in _headers taking precedence over the middleware defaults.

Actual Behavior

Two cache-control headers are returned:

cache-control: max-age=60, must-revalidate
cache-control: public, max-age=31536000, immutable

Root Cause Analysis

The issue occurs because:

  • Souin cache middleware sets a default CacheControl value
  • User headers from _headers file are added using w.Header().Add(), which appends without overwriting
  • The default cache control is configured from PGS_CACHE_CONTROL env var or defaults to max-age=600

Suggested Solution

When applying user headers from _headers, use w.Header().Set() instead of w.Header().Add() for the cache-control header to override the middleware's default. This would ensure user-defined cache directives take precedence while preserving the ability to set other custom headers.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions