Skip to content

Commit a1623fc

Browse files
authored
Merge pull request rails#25930 from mechanicles/doc-http-cache-forever
Add documentation for `http_cache_forever`. [ci skip]
2 parents 79e27b9 + 80f777e commit a1623fc

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

guides/source/caching_with_rails.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,30 @@ class ProductsController < ApplicationController
512512
end
513513
```
514514

515+
Sometimes we want to cache response, for example a static page, that never gets
516+
expired. To achieve this, we can use `http_cache_forever` helper and by doing
517+
so browser and proxies will cache it indefinitely.
518+
519+
By default cached responses will be private, cached only on the user's web
520+
browser. To allow proxies to cache the response, set `public: true` to indicate
521+
that they can serve the cached response to all users.
522+
523+
Using this helper, `last_modified` header is set to `Time.new(2011, 1, 1).utc`
524+
and `expires` header is set to a 100 years.
525+
526+
WARNING: Use this method carefully as browser/proxy won't be able to invalidate
527+
the cached response unless browser cache is forcefully cleared.
528+
529+
```ruby
530+
class HomeController < ApplicationController
531+
def index
532+
http_cache_forever(public: true) do
533+
render
534+
end
535+
end
536+
end
537+
```
538+
515539
### Strong v/s Weak ETags
516540

517541
Rails generates weak ETags by default. Weak ETags allow semantically equivalent

0 commit comments

Comments
 (0)