Skip to content

Commit 116ee51

Browse files
committed
Unwrap caching guide prose and replace invalidation table with a sentence
1 parent 9787698 commit 116ee51

1 file changed

Lines changed: 18 additions & 57 deletions

File tree

‎docs/guide/caching.md‎

Lines changed: 18 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,9 @@ Experimental
1212
Since 4.14.0
1313
{: .label }
1414

15-
**This API is experimental.** It may change or be removed in a non-major release.
16-
Please share feedback in [#234](https://github.com/ViewComponent/view_component/issues/234).
15+
**This API is experimental.** It may change or be removed in a non-major release. Please share feedback in [#234](https://github.com/ViewComponent/view_component/issues/234).
1716

18-
Rails computes a digest for every template from its source and from the templates
19-
it renders. That digest is mixed into the key of every `<% cache %>` block in the
20-
template, so editing a partial invalidates the caches of everything that renders it.
17+
Rails computes a digest for every template from its source and from the templates it renders. That digest is mixed into the key of every `<% cache %>` block in the template, so editing a partial invalidates the caches of everything that renders it.
2118

2219
Components are invisible to that mechanism, which means this doesn't work:
2320

@@ -27,13 +24,11 @@ Components are invisible to that mechanism, which means this doesn't work:
2724
<% end %>
2825
```
2926

30-
Editing `PostComponent`'s template, Ruby class, or sidecar files doesn't invalidate
31-
the fragment, so the stale markup is served until the cache is cleared by hand.
27+
Editing `PostComponent`'s template, Ruby class, or sidecar files doesn't invalidate the fragment, so the stale markup is served until the cache is cleared by hand.
3228

3329
## Opting in
3430

35-
Include `ViewComponent::ExperimentallyCacheable` in each component that should
36-
participate in caching:
31+
Include `ViewComponent::ExperimentallyCacheable` in each component that should participate in caching:
3732

3833
```ruby
3934
class PostComponent < ViewComponent::Base
@@ -45,29 +40,11 @@ class PostComponent < ViewComponent::Base
4540
end
4641
```
4742

48-
That's all that's needed for the `<% cache %>` block above to work. The component
49-
is registered with Rails' digest tree, and the fragment is invalidated when any of
50-
the following change:
51-
52-
| Change | Invalidates |
53-
|---|---|
54-
| The component's template | ✅ |
55-
| The component's Ruby class | ✅ |
56-
| A sidecar file (such as an i18n `.yml`) | ✅ |
57-
| A superclass's template or Ruby class | ✅ |
58-
| A child component rendered by the template | ✅ |
59-
| A partial rendered by the template | ✅ |
60-
| A child component rendered by an inline template or `#call` method | ✅ |
61-
| A partial rendered by an inline template | ✅ |
62-
| A partial rendered by a `#call` method | ❌ [use the escape hatch](#declaring-dependencies-static-analysis-cant-see) |
63-
64-
Components that don't include the module are unaffected, and applications that
65-
never opt in pay no cost.
43+
That's all that's needed for the `<% cache %>` block above to work. The component is registered with Rails' digest tree, and the fragment is invalidated when the component's template, Ruby class, sidecar files, superclasses, child components, or rendered partials change, including children and partials rendered from an inline template and children rendered from a `#call` method. The one exception is a partial referenced by string path from a `#call` method, which needs [the escape hatch](#declaring-dependencies-static-analysis-cant-see).
6644

6745
## Caching a component's own output
6846

69-
Use `cache_on` to have the component cache its own rendered output. Each argument
70-
names a method whose value identifies a rendering of the component:
47+
Use `cache_on` to have the component cache its own rendered output. Each argument names a method whose value identifies a rendering of the component:
7148

7249
```ruby
7350
class PostComponent < ViewComponent::Base
@@ -85,15 +62,13 @@ class PostComponent < ViewComponent::Base
8562
end
8663
```
8764

88-
Rendering the component now reads from and writes to `Rails.cache`, with no
89-
`<% cache %>` block at the call site:
65+
Rendering the component now reads from and writes to `Rails.cache`, with no `<% cache %>` block at the call site:
9066

9167
```erb
9268
<%= render PostComponent.new(post: @post) %>
9369
```
9470

95-
Private methods are allowed, so the values that form the key don't have to be
96-
part of the component's public interface.
71+
Private methods are allowed, so the values that form the key don't have to be part of the component's public interface.
9772

9873
The cache key combines:
9974

@@ -103,13 +78,11 @@ The cache key combines:
10378
- the current `I18n.locale`
10479
- the values returned by the `cache_on` methods
10580

106-
Caching is skipped unless `perform_caching` is enabled on the controller, matching
107-
the behavior of Rails' `cache` helper. Override `#cache_key` for full control.
81+
Caching is skipped unless `perform_caching` is enabled on the controller, matching the behavior of Rails' `cache` helper. Override `#cache_key` for full control.
10882

10983
## Reading a component's digest
11084

111-
`.cache_digest` returns the digest of everything the component renders from. It
112-
works outside a request, where no view context exists:
85+
`.cache_digest` returns the digest of everything the component renders from. It works outside a request, where no view context exists:
11386

11487
```ruby
11588
PostComponent.cache_digest # => "a1b2c3..."
@@ -125,24 +98,21 @@ Use it to build cache keys by hand, or to key a cache in a background job:
12598

12699
## Declaring dependencies static analysis can't see
127100

128-
Dependencies are discovered by scanning template and Ruby source, so dynamic
129-
renders are invisible:
101+
Dependencies are discovered by scanning template and Ruby source, so dynamic renders are invisible:
130102

131103
```erb
132104
<%= render @component %>
133105
```
134106

135-
The same applies to a partial referenced by string path from a `#call` method,
136-
which is the one dependency kind that isn't discovered automatically:
107+
The same applies to a partial referenced by string path from a `#call` method, which is the one dependency kind that isn't discovered automatically:
137108

138109
```ruby
139110
def call
140111
render "posts/byline" # not tracked
141112
end
142113
```
143114

144-
Declare these with Rails' `# Template Dependency:` comment, in either the Ruby
145-
file or the template:
115+
Declare these with Rails' `# Template Dependency:` comment, in either the Ruby file or the template:
146116

147117
```ruby
148118
class PostComponent < ViewComponent::Base
@@ -154,8 +124,7 @@ end
154124

155125
## Caveats
156126

157-
**Content blocks aren't cached.** Content passed as a block isn't part of the
158-
cache key, so caching it would risk serving one caller's content to another:
127+
**Content blocks aren't cached.** Content passed as a block isn't part of the cache key, so caching it would risk serving one caller's content to another:
159128

160129
```erb
161130
<%# Not cached: the block's content isn't in the key %>
@@ -164,18 +133,10 @@ cache key, so caching it would risk serving one caller's content to another:
164133
<% end %>
165134
```
166135

167-
To cache a component that takes content, include the values that determine that
168-
content in `cache_on`, and set the content from within the component rather than
169-
from the call site.
136+
To cache a component that takes content, include the values that determine that content in `cache_on`, and set the content from within the component rather than from the call site.
170137

171-
**Slots have the same constraint.** Slot content set by the caller isn't part of
172-
the key unless declared in `cache_on`.
138+
**Slots have the same constraint.** Slot content set by the caller isn't part of the key unless declared in `cache_on`.
173139

174-
**`cache_on` methods run before the component renders**, so they can only depend
175-
on the component's own state, not on `helpers` or the view context. A cache key
176-
that depends on the view context is usually a sign the value should be passed to
177-
the component instead.
140+
**`cache_on` methods run before the component renders**, so they can only depend on the component's own state, not on `helpers` or the view context. A cache key that depends on the view context is usually a sign the value should be passed to the component instead.
178141

179-
**Included modules aren't tracked.** A component's superclasses are, but a module
180-
included into a component isn't, since a module has no template or source file of
181-
its own to hash. Use `# Template Dependency:` for those.
142+
**Included modules aren't tracked.** A component's superclasses are, but a module included into a component isn't, since a module has no template or source file of its own to hash. Use `# Template Dependency:` for those.

0 commit comments

Comments
 (0)