@@ -45,21 +45,13 @@ def config
45
45
delegate :content_security_policy_nonce , to : :helpers
46
46
47
47
# Config option that strips trailing whitespace in templates before compiling them.
48
+ class_attribute :__vc_cache_dependencies , instance_accessor : false , instance_predicate : false , default : [ ]
48
49
class_attribute :__vc_strip_trailing_whitespace , instance_accessor : false , instance_predicate : false
49
50
self . __vc_strip_trailing_whitespace = false # class_attribute:default doesn't work until Rails 5.2
50
51
51
52
attr_accessor :__vc_original_view_context
52
53
53
- # Compoents can have a cache key that is used to cache the rendered output.
54
- #
55
- # @return [String]
56
- def cache_key
57
- @vc_cache_key = if defined? ( __vc_cache_args )
58
- Digest ::MD5 . hexdigest (
59
- __vc_cache_args . map { |method | send ( method ) } . join ( "-" )
60
- )
61
- end
62
- end
54
+
63
55
64
56
# Components render in their own view context. Helpers and other functionality
65
57
# require a reference to the original Rails view context, an instance of
@@ -128,8 +120,8 @@ def render_in(view_context, &block)
128
120
end
129
121
end . to_s
130
122
131
- if cache_key . present?
132
- Rails . cache . fetch ( @vc_cache_key ) do
123
+ if view_cache_dependencies . present?
124
+ Rails . cache . fetch ( view_cache_dependencies ) do
133
125
__vc_render_template ( rendered_template )
134
126
end
135
127
else
@@ -283,9 +275,11 @@ def virtual_path
283
275
# For caching, such as #cache_if
284
276
# @private
285
277
def view_cache_dependencies
286
- [ ]
278
+ self . class . view_cache_dependencies
287
279
end
288
280
281
+ alias_method :component_cache_dependencies , :view_cache_dependencies
282
+
289
283
# For caching, such as #cache_if
290
284
#
291
285
# @private
@@ -516,12 +510,16 @@ def sidecar_files(extensions)
516
510
( sidecar_files - [ source_location ] + sidecar_directory_files + nested_component_files ) . uniq
517
511
end
518
512
513
+
514
+
519
515
def cache_on ( *args )
520
- class_eval <<~RUBY , __FILE__ , __LINE__ + 1
521
- def __vc_cache_args
522
- #{ args }
523
- end
524
- RUBY
516
+ self . __vc_cache_dependencies . push ( *args )
517
+ end
518
+
519
+ def view_cache_dependencies
520
+ return unless __vc_cache_dependencies . any?
521
+
522
+ __vc_cache_dependencies . map { |dep | send ( dep ) } . compact
525
523
end
526
524
527
525
# Render a component for each element in a collection ([documentation](/guide/collections)):
0 commit comments