stale?
- 1.0.0
- 1.1.6
- 1.2.6
- 2.0.3
- 2.1.0
- 2.2.1
- 2.3.8
- 3.0.0 (0)
- 3.0.9 (-1)
- 3.1.0 (0)
- 3.2.1 (12)
- 3.2.8 (0)
- 3.2.13 (0)
- 4.0.2 (0)
- 4.1.8 (0)
- 4.2.1 (10)
- 4.2.7 (0)
- 4.2.9 (0)
- 5.0.0.1 (29)
- 5.1.7 (0)
- 5.2.3 (0)
- 6.0.0 (0)
- 6.1.3.1 (0)
- 6.1.7.7 (0)
- 7.0.0 (10)
- 7.1.3.2 (-38)
- 7.1.3.4 (0)
- What's this?
stale?(object = nil, **freshness_kwargs)
public
Sets the etag and/or last_modified on the response and checks them against the request. If the request doesn’t match the provided options, it is considered stale, and the response should be rendered from scratch. Otherwise, it is fresh, and a 304 Not Modified is sent.
Options
See #fresh_when for supported options.
Examples
def show @article = Article.find(params[:id]) if stale?(etag: @article, last_modified: @article.updated_at) @statistics = @article.really_expensive_call respond_to do |format| # all the supported formats end end end
You can also just pass a record:
def show @article = Article.find(params[:id]) if stale?(@article) @statistics = @article.really_expensive_call respond_to do |format| # all the supported formats end end end
etag will be set to the record, and last_modified will be set to the record’s updated_at.
You can also pass an object that responds to maximum, such as a collection of records:
def index @articles = Article.all if stale?(@articles) @statistics = @articles.really_expensive_call respond_to do |format| # all the supported formats end end end
In this case, etag will be set to the collection, and last_modified will be set to maximum(:updated_at) (the timestamp of the most recently updated record).
When passing a record or a collection, you can still specify other options, such as :public and :cache_control:
def show @article = Article.find(params[:id]) if stale?(@article, public: true, cache_control: { no_cache: true }) @statistics = @articles.really_expensive_call respond_to do |format| # all the supported formats end end end
The above will set Cache-Control: public, no-cache in the response.
When rendering a different template than the controller/action’s default template, you can indicate which digest to include in the ETag:
def show super if stale?(@article, template: "widgets/show") end