You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 116ee51
Browse filesBrowse the repository at this point in the historyBrowse files
Copy file name to clipboardExpand all lines: docs/guide/caching.md
+18-57Lines changed: 18 additions & 57 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,12 +12,9 @@ Experimental
12
12
Since 4.14.0
13
13
{: .label }
14
14
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).
17
16
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.
21
18
22
19
Components are invisible to that mechanism, which means this doesn't work:
23
20
@@ -27,13 +24,11 @@ Components are invisible to that mechanism, which means this doesn't work:
27
24
<% end %>
28
25
```
29
26
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.
32
28
33
29
## Opting in
34
30
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:
37
32
38
33
```ruby
39
34
classPostComponent < ViewComponent::Base
@@ -45,29 +40,11 @@ class PostComponent < ViewComponent::Base
45
40
end
46
41
```
47
42
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).
66
44
67
45
## Caching a component's own output
68
46
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:
71
48
72
49
```ruby
73
50
classPostComponent < ViewComponent::Base
@@ -85,15 +62,13 @@ class PostComponent < ViewComponent::Base
85
62
end
86
63
```
87
64
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:
90
66
91
67
```erb
92
68
<%= render PostComponent.new(post: @post) %>
93
69
```
94
70
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.
97
72
98
73
The cache key combines:
99
74
@@ -103,13 +78,11 @@ The cache key combines:
103
78
- the current `I18n.locale`
104
79
- the values returned by the `cache_on` methods
105
80
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.
108
82
109
83
## Reading a component's digest
110
84
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:
113
86
114
87
```ruby
115
88
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:
125
98
126
99
## Declaring dependencies static analysis can't see
127
100
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:
130
102
131
103
```erb
132
104
<%= render @component %>
133
105
```
134
106
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:
137
108
138
109
```ruby
139
110
defcall
140
111
render "posts/byline"# not tracked
141
112
end
142
113
```
143
114
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:
146
116
147
117
```ruby
148
118
classPostComponent < ViewComponent::Base
@@ -154,8 +124,7 @@ end
154
124
155
125
## Caveats
156
126
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:
159
128
160
129
```erb
161
130
<%# 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:
164
133
<% end %>
165
134
```
166
135
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.
170
137
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`.
173
139
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.
178
141
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