Skip to content

Commit

Permalink
Update documentation based on the new changes for strong & weak ETags…
Browse files Browse the repository at this point in the history
… [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.
  • Loading branch information
mechanicles committed Apr 5, 2016
1 parent 8040e55 commit 0287e1f
Showing 1 changed file with 29 additions and 3 deletions.
32 changes: 29 additions & 3 deletions guides/source/caching_with_rails.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0287e1f

Please sign in to comment.