What does config.action_dispatch.strict_freshness change in Rails 8.x when a request sends both If-None-Match and If-Modified-Since, and when would an upgraded app notice?
answer
- two validators, one verdict
- load_defaults 8.0 turns it on
- true: the ETag alone decides
- false: both must match
- only fresh_when and stale? read it
basics
~20 sWith strict_freshness true, which load_defaults 8.0 sets, Rails judges a request carrying If-None-Match by the ETag alone and ignores If-Modified-Since; with false, the old default, both validators must match before fresh_when or stale? answers 304.
solid answer
~40 s`config.action_dispatch.strict_freshness` sets `ActionDispatch::Http::Cache::Request.strict_freshness`, which `request.fresh?` reads when `fresh_when` and `stale?` decide on a 304. When `true`, if `If-None-Match` is present only the ETag is compared, and `If-Modified-Since` is consulted only when there is no `If-None-Match`, as RFC 7232 section 6 says. When `false`, every validator the client sent must match: the ETag and `Last-Modified <= If-Modified-Since`. The framework default is `false`; `config.load_defaults "8.0"` (and so 8.1) turns it on. An upgraded app notices when its `last_modified:` covers something the ETag does not, for example `fresh_when etag: @article, last_modified: @article.comments.maximum(:updated_at)`: under strict rules a new comment no longer forces a 200 for clients that send the ETag. The fix is to put everything that matters into the ETag.
code
ruby · 5 lines# config/application.rb
config.load_defaults 8.1 # strict_freshness is true from 8.0 on
# Temporary opt-out while ETags are made complete
config.action_dispatch.strict_freshness = falsego deeper
Know that config.action_dispatch.strict_freshness exists and that new Rails 8 apps have it on, so the ETag decides when a browser sends both validators.
Explain both behaviours: true lets If-None-Match decide alone, false requires every validator to match, and only fresh_when, stale? and http_cache_forever read it.
During an upgrade, find conditional GET calls whose last_modified covers data the ETag misses, make the ETags complete, and use an explicit false only as a dated stopgap.
Treat load_defaults upgrades as behaviour changes to review, with a list of settings like this one that need an owner and an exit date when temporarily overridden.
## The question it answers A browser that stored a response with both an `ETag` and a `Last-Modified` header revalidates with both `If-None-Match` and `If-Modified-Since`. The server must decide whether the copy is fresh. HTTP's rule (RFC 7232 section 6) is that `If-None-Match` takes precedence: when it is present, `If-Modified-Since` is ignored. Rails long used a stricter-sounding but different rule: both had to match. **`config.action_dispatch.strict_freshness`** chooses between the two. ## How the setting is wired - `ActionDispatch::Railtie` defines `config.action_dispatch.strict_freshness = false` and copies it into **`ActionDispatch::Http::Cache::Request.strict_freshness`** at boot. - **`config.load_defaults "8.0"`** sets it to `true`, and `load_defaults "8.1"` inherits that. A new Rails 8.1 app therefore runs with it on. - It is read by **`request.fresh?(response)`**, the check behind `fresh_when`, `stale?` and `http_cache_forever`. ## The two behaviours | Client sends | `strict_freshness = true` | `strict_freshness = false` | |---|---|---| | `If-None-Match` only | fresh if the ETag matches | fresh if the ETag matches | | `If-Modified-Since` only | fresh if `Last-Modified <= If-Modified-Since` | same | | both | fresh if the ETag matches; date ignored | fresh only if the ETag **and** the date both match | | neither | not fresh | not fresh | The ETag comparison is an exact string match against the comma-separated `If-None-Match` list, or `*`. ## When an upgrade changes behaviour The two settings disagree only when the ETag matches but the date check fails. That happens when the action passes a `last_modified:` that tracks something the ETag does not: ```ruby fresh_when etag: @article, last_modified: @article.comments.maximum(:updated_at) ``` 1. Under `false`, a new comment moves `Last-Modified` past the client's `If-Modified-Since`, so the request is stale and the page renders with the comment. 2. Under `true`, the client also sends `If-None-Match`, the article's ETag still matches, the date is ignored, and the client gets a 304 without the new comment. So the upgrade risk is pages that quietly relied on `Last-Modified` as a second validator. The fix is to put every input into the ETag, for example `fresh_when etag: [@article, @article.comments.maximum(:updated_at)], last_modified: ...`, or to touch the article when a comment changes. ## What it does not touch - **`Rack::ConditionalGet`**, the middleware that turns a matching 200 into a 304 for responses that did not call `fresh_when`, has its own logic. It already checks `If-None-Match` first and falls back to `If-Modified-Since` only without it. - ETag generation, weak or strong, and `Cache-Control` are unchanged. The configuration guide describes the setting as belonging to an "`ActionDispatch::ETag` middleware"; no such class exists in Rails 8.1. The source shows the switch lives in `ActionDispatch::Http::Cache::Request#fresh?`. ## Upgrading deliberately - When moving `load_defaults` to 8.0, search for `fresh_when` and `stale?` calls that pass both `etag:` and a `last_modified:` derived from different data. - If you need time to fix them, set `config.action_dispatch.strict_freshness = false` explicitly, write down when it will be removed, and then make the ETags complete. - Calls that pass only a record (`fresh_when @article`) derive both validators from the same record and behave the same either way in practice.
- Does `strict_freshness` affect responses that never call `fresh_when` or `stale?`?No. Those get their ETag from `Rack::ETag` and their 304 from `Rack::ConditionalGet`, which has its own rule: it checks `If-None-Match` when present and otherwise `If-Modified-Since`. Only `request.fresh?`, used by `fresh_when`, `stale?` and `http_cache_forever`, reads the setting.
- Why does `fresh_when @article` behave the same under both settings in practice?Both validators come from the same record: the ETag from its cache key and version, `Last-Modified` from `updated_at`. When the record changes, both change, so the ETag check alone gives the same answer as checking both.
- strict_freshness makes Rails require both the ETag and the date to match.
- strict_freshness changes how Rails generates ETags.
- Rails 8.1 apps upgraded from 7.2 get strict_freshness without changing load_defaults.
- strict_freshness also changes how Rack::ConditionalGet decides on 304s.
- With strict_freshness on, If-Modified-Since is never used.
Assumes Rails 8.1 with load_defaults 8.1; strict_freshness defaults to false in the framework and has been turned on by load_defaults 8.0 since Rails 8.0.