1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Update documentation based on the new changes for strong & weak ETags [ci skip]

- Update info for weak ETag
- Add info for strong ETag
- Add examples for strong and weak ETags
- Display difference between generated strong and weak ETags strings.
This commit is contained in:
Santosh Wadghule 2016-04-04 13:45:58 +05:30
parent 8040e55063
commit 0287e1fccb

View file

@ -512,12 +512,38 @@ class ProductsController < ApplicationController
end
```
### A note on weak ETags
### Strong v/s Weak ETags
Etags generated by Rails are weak by default. Weak etags allow semantically equivalent responses to have the same etags, even if their bodies do not match exactly. This is useful when we don't want the page to be regenerated for minor changes in response body. If you absolutely need to generate a strong etag, it can be assigned to the header directly.
Rails generates weak ETags by default. Weak ETags allow semantically equivalent
responses to have the same ETags, even if their bodies do not match exactly.
This is useful when we don't want the page to be regenerated for minor changes in
response body.
Weak ETags have a leading `W/` to differentiate them from strong ETags.
```
W/"618bbc92e2d35ea1945008b42799b0e7" → Weak ETag
"618bbc92e2d35ea1945008b42799b0e7" → Strong ETag
```
Unlike weak ETag, Strong ETag implies that response should be exactly same
and byte by byte identical. Useful when doing Range requests within a
large video or PDF file. Some CDNs support only strong ETags, like Akamai.
If you absolutely need to generate a strong ETag, it can be done as follows.
```ruby
response.add_header "ETag", Digest::MD5.hexdigest(response.body)
class ProductsController < ApplicationController
def show
@product = Product.find(params[:id])
fresh_when last_modified: @product.published_at.utc, strong_etag: @product
end
end
```
You can also set the strong ETag directly on the response.
```ruby
response.strong_etag = response.body # => "618bbc92e2d35ea1945008b42799b0e7"
```
References