aboutsummaryrefslogtreecommitdiffstats
path: root/guides/source/caching_with_rails.md
diff options
context:
space:
mode:
authorSantosh Wadghule <santosh.wadghule@gmail.com>2016-04-04 13:45:58 +0530
committerSantosh Wadghule <santosh.wadghule@gmail.com>2016-04-05 15:28:28 +0530
commit0287e1fccb2736993f58adac4f6eba1857082f87 (patch)
tree2839bbb7e0bf712d45784389ffb4ee1438ce6da5 /guides/source/caching_with_rails.md
parent8040e55063abde6eeaac2702ca9961ce34a2d6c7 (diff)
downloadrails-0287e1fccb2736993f58adac4f6eba1857082f87.tar.gz
rails-0287e1fccb2736993f58adac4f6eba1857082f87.tar.bz2
rails-0287e1fccb2736993f58adac4f6eba1857082f87.zip
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.
Diffstat (limited to 'guides/source/caching_with_rails.md')
-rw-r--r--guides/source/caching_with_rails.md32
1 files changed, 29 insertions, 3 deletions
diff --git a/guides/source/caching_with_rails.md b/guides/source/caching_with_rails.md
index ec4444eb43..ae204a55d6 100644
--- a/guides/source/caching_with_rails.md
+++ b/guides/source/caching_with_rails.md
@@ -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
+ 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.add_header "ETag", Digest::MD5.hexdigest(response.body)
+ response.strong_etag = response.body # => "618bbc92e2d35ea1945008b42799b0e7"
```
References