In a Rails controller, what Cache-Control headers do expires_in, expires_now, no_store and http_cache_forever produce, and when would you use each?
answer
- max-age, private unless public: true
- expires_now writes no-cache
- no_store replaces everything
- forever: 100 years, immutable
- default: max-age=0, private, must-revalidate
basics
~20 sexpires_in 10.minutes gives max-age=600, private (public with public: true); expires_now gives no-cache; no_store gives no-store; http_cache_forever gives a 100-year max-age with immutable and yields its block only when stale. Without them, Rails sends max-age=0, private, must-revalidate.
solid answer
~40 sThese are `ActionController::ConditionalGet` methods that edit `response.cache_control`. `expires_in 10.minutes` yields `max-age=600, private`; `public: true` swaps in `public`, and `must_revalidate:`, `stale_while_revalidate:`, `stale_if_error:` and `immutable:` add those directives, while any other key such as `"s-maxage" => 5.hours` is passed through as `s-maxage=18000`. `expires_now` replaces the header with `no-cache`, so caches must revalidate every time. `no_store` replaces it with `no-store` for responses that must not be kept, such as account pages; 8.1 adds `must_understand`, which sends `must-understand, no-store`. `http_cache_forever(public: true) { render }` sets `max-age` to 100 years with `public, immutable`, an ETag from the full path and a fixed 2011 `Last-Modified`, and runs the block only when the request is stale. With none of them, Rails sends `max-age=0, private, must-revalidate`.
code
ruby · 8 linesclass AccountsController < ApplicationController
before_action { no_store }
def show
@user = Current.user
# expires_in here would delete no_store and allow caching again
end
endgo deeper
Match each helper to its header: expires_in gives max-age, expires_now gives no-cache, no_store gives no-store, and http_cache_forever gives a 100-year immutable lifetime.
Explain the defaults and options: private unless public: true, extra keys such as s-maxage passed through, and that no_store and expires_now replace the hash while expires_in and fresh_when delete no_store.
Audit cache headers per page type: personal data with no_store, public content with public and s-maxage, and watch for later calls undoing a before_action.
Set the caching policy as a team rule, deciding which page classes may enter shared caches and how content versions change URLs when http_cache_forever is used.
## Where these methods live `expires_in`, `expires_now`, `no_store`, `http_cache_forever` and, since Rails 8.1, `must_understand` are instance methods of **`ActionController::ConditionalGet`**, available in both `ActionController::Base` and `ActionController::API`. Each edits `response.cache_control`, a hash that Rails turns into the `Cache-Control` header when the response is committed. What each directive means to a cache is HTTP's business; the Rails question is which header each call writes. ## The default If an action calls none of them, a normal rendered response gets `Cache-Control: max-age=0, private, must-revalidate`, from `Rack::ETag` or from Rails itself when an ETag or Last-Modified is present. Browsers may keep the response but must revalidate before reusing it, and shared caches must not store it. ## `expires_in` ```ruby expires_in 10.minutes # max-age=600, private expires_in 10.minutes, public: true # max-age=600, public expires_in 1.minute, public: true, "s-maxage" => 5.hours # max-age=60, public, s-maxage=18000 ``` - the first argument becomes `max-age` in seconds; - `public: true` switches `private` to `public`; - `must_revalidate: true`, `stale_while_revalidate: 5.minutes`, `stale_if_error: 1.hour` and `immutable: true` add those directives; - any other key is written as `key=value`, which is how `s-maxage` gets in; - it removes an earlier `no_store` and sets the `Date` header if missing. Use it for responses that may be reused without asking for a while: a public list of articles, a JSON lookup table. ## `expires_now` `expires_now` **replaces** the whole cache-control hash with `no_cache: true`, producing `Cache-Control: no-cache`. Caches may store the response but must revalidate it before every reuse. It suits pages that are fine to store but must never be shown stale without asking, often paired with `fresh_when` so the revalidation is cheap. ## `no_store` and `must_understand` `no_store` **replaces** the hash with `no_store: true`, producing `Cache-Control: no-store`: nothing should be written to any cache. Use it for account settings, payment confirmations and other pages with personal data. Rails 8.1 adds `must_understand`, which sets `must-understand` together with `no-store` (`must-understand, no-store`) as the RFC 9111 pairing requires. Order matters. `fresh_when` and `expires_in` both delete `:no_store` before merging their own settings, while `no_store` and `expires_now` replace everything. So the last call wins: | Calls, in order | Header | |---|---| | `no_store`, then `expires_in 5.minutes` | `max-age=300, private` | | `expires_in 5.minutes`, then `no_store` | `no-store` | | `expires_in 5.minutes`, then `expires_now` | `no-cache` | A `no_store` in a `before_action` can therefore be undone by an `expires_in` in the action. ## `http_cache_forever` ```ruby def terms http_cache_forever(public: true) { render } end ``` 1. It calls `expires_in 100.years, public:, immutable: true`, so the header becomes `max-age=3155695200, public, immutable` (or `private` without `public: true`). 2. It calls `stale?` with `etag: request.fullpath` and `last_modified: Time.new(2011, 1, 1).utc`. 3. It yields the block only when the request is stale; a revalidating client gets a 304. Use it for content that will never change at that URL. Rails' guide warns that browsers and proxies keep reusing the response until the URL changes or the cache is cleared, so the only way to ship a new version is a new URL. ## Choosing - Personal, sensitive page: `no_store`. - Personal page that is safe to store: default, plus `fresh_when` to make revalidation cheap. - Public page that may be slightly stale: `expires_in` with `public: true`, maybe `s-maxage` for a CDN. - Must always be checked: `expires_now` plus `fresh_when`. - Never changes at this URL: `http_cache_forever`.
- Why does `expires_in 5.minutes` after a `before_action { no_store }` make the page cacheable again?`expires_in` starts by deleting `:no_store` from `response.cache_control`, then merges `max-age`. The last helper called decides the header, so the result is `max-age=300, private`. Keep sensitive pages free of `expires_in`, or call `no_store` last.
- Does `http_cache_forever` include the template digest in its ETag?In an `ActionController::Base` controller, yes: it calls `stale?`, which runs the controller's `etag` blocks, including the template digest. But with a 100-year `max-age` and `immutable`, a browser holding the page normally never revalidates, so a changed template only reaches clients through a new URL.
- expires_now sends no-store, so nothing is ever cached.
- expires_in 10.minutes makes the response public by default.
- no_store in a before_action is safe whatever the action calls later.
- http_cache_forever renders the block on every request.
- Rails sends no Cache-Control header unless you call one of these helpers.