view_component 4.15.0 → 4.19.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/docs/CHANGELOG.md +74 -0
- data/lib/view_component/base.rb +115 -22
- data/lib/view_component/cache_digest/dependency_tracking.rb +0 -5
- data/lib/view_component/cache_digest/digestor_lookup.rb +31 -0
- data/lib/view_component/cache_digest/resolver.rb +19 -20
- data/lib/view_component/cache_digest.rb +64 -37
- data/lib/view_component/{experimentally_cacheable.rb → cacheable.rb} +40 -87
- data/lib/view_component/collection.rb +19 -16
- data/lib/view_component/compile_cache.rb +9 -0
- data/lib/view_component/compiler.rb +12 -2
- data/lib/view_component/engine.rb +25 -5
- data/lib/view_component/instrumentation.rb +5 -1
- data/lib/view_component/request_details.rb +15 -2
- data/lib/view_component/template.rb +29 -19
- data/lib/view_component/translatable.rb +7 -12
- data/lib/view_component/version.rb +1 -1
- data/lib/view_component.rb +1 -1
- metadata +4 -17
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: d29069cd53a93e2e3b1e2285612ecd25985bae4458a842227922a1a5bdb18b96
|
|
4
|
+
data.tar.gz: 6063a6b520fc62961f61e36b30bbbf47eaf5728580a61a5af68231b11c5f7dc4
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 2741959ff497e91a4fd792959667ad0e6ca57f3378e460a8ec6db74c013b09acf4eeb2bfbcbfbfab9b81f92613c29107562c838502b61b5a3b8503e4c75978ed
|
|
7
|
+
data.tar.gz: 8b54025874d993b7fe3b61f565bff74c463ac683f66b0427b05cc3e5791ec1d4b526fd9807010669e641203505c0d0e58376ffab3c6d96e2a1e68bc746e1e4cc
|
data/docs/CHANGELOG.md
CHANGED
|
@@ -10,6 +10,80 @@ nav_order: 6
|
|
|
10
10
|
|
|
11
11
|
## main
|
|
12
12
|
|
|
13
|
+
## 4.19.0
|
|
14
|
+
|
|
15
|
+
* Remove the unused `concurrent-ruby` dependency. `require "concurrent-ruby"` loaded every concurrent-ruby class at boot, a leftover from the `Concurrent::ReadWriteLock` the compiler no longer uses.
|
|
16
|
+
|
|
17
|
+
*Clément Joubert*
|
|
18
|
+
|
|
19
|
+
* Fix every request raising `NoMethodError` in development when an autoload path is a `Pathname`, as engines such as Madmin add. The template watcher now passes the autoload paths to the file watcher as strings.
|
|
20
|
+
|
|
21
|
+
*Anthony Veaudry*
|
|
22
|
+
|
|
23
|
+
* Fix the template annotations integration test failing when it ran after the coverage compile tests, which left components compiled without the annotation in the test cache.
|
|
24
|
+
|
|
25
|
+
*Anthony Veaudry*
|
|
26
|
+
|
|
27
|
+
## 4.18.0
|
|
28
|
+
|
|
29
|
+
* Enable caching support for all components by default, and rename `ViewComponent::ExperimentallyCacheable` to `ViewComponent::Cacheable`. Including the module is no longer necessary. Breaking: remove existing `include ViewComponent::ExperimentallyCacheable` lines. Caching is no longer experimental.
|
|
30
|
+
|
|
31
|
+
*Erik Axel Nielsen, Joel Hawksley*
|
|
32
|
+
|
|
33
|
+
## 4.17.0
|
|
34
|
+
|
|
35
|
+
* Only register the executor hook that watches component templates when Action View doesn't cache templates at boot. With template caching on, as in production, the hook did nothing but still read `ActionView::Base.cache_template_loading` on every request. That value lives in a class variable, which non-main Ractors can't read on Ruby 4.0, so under Rails' experimental `Rails.application.ractorize!` every request failed.
|
|
36
|
+
|
|
37
|
+
*Mikael Henriksson*
|
|
38
|
+
|
|
39
|
+
* Fix `Component.translate` raising `UncaughtThrowError` for keys missing from the sidecar translations, and `NoMethodError` for components without sidecar translations. It now falls back to the global translations, like the instance method does. Fix `translate` returning the `default:` value instead of an existing global translation.
|
|
40
|
+
|
|
41
|
+
*Andrii Furmanets*
|
|
42
|
+
|
|
43
|
+
* Only recompile components when a template or a translation file changed, instead of before every request, when templates aren't cached. Pages render about twice as fast in development.
|
|
44
|
+
|
|
45
|
+
*Clément Joubert*
|
|
46
|
+
|
|
47
|
+
## 4.16.0
|
|
48
|
+
|
|
49
|
+
* Fix subclasses defined after their parent renders a collection using the parent's default collection parameter name instead of their own. Fix recompiling a component not refreshing its collection parameter, counter, and iteration detection. Compute that detection when a component compiles, and store the results frozen so they're shareable across Ractors.
|
|
50
|
+
|
|
51
|
+
*Ben Sheldon*
|
|
52
|
+
|
|
53
|
+
* Fix collection counter and iteration detection, and collection and reserved parameter validation, when `initialize` arguments are forwarded through anonymous arguments (`...` or `**`), such as by prepended modules and subclasses calling `super`.
|
|
54
|
+
|
|
55
|
+
*Ben Sheldon*
|
|
56
|
+
|
|
57
|
+
* Update development and documentation dependencies, baseline existing Audition findings, and accommodate updated lint rules.
|
|
58
|
+
|
|
59
|
+
*Joel Hawksley*
|
|
60
|
+
|
|
61
|
+
* Invalidate Action View's memoized template digests when a component registers with `ViewComponent::CacheDigest`, so a digest computed before the component loaded isn't served for the rest of the process.
|
|
62
|
+
|
|
63
|
+
*Erik Axel Nielsen*
|
|
64
|
+
|
|
65
|
+
* Raise errors encountered while computing a component's cache digest instead of degrading to an untracked component that can serve stale fragments.
|
|
66
|
+
|
|
67
|
+
*Erik Axel Nielsen*
|
|
68
|
+
|
|
69
|
+
* Give `<% cache %>` blocks inside a component's own template a digest, for components that `include ViewComponent::ExperimentallyCacheable`.
|
|
70
|
+
|
|
71
|
+
Rails digests the virtual path of whichever template is rendering. Inside a component that path resolves to no template, because component templates aren't in the view paths, so the Digestor returned an empty digest and the fragment was never invalidated. The only signal was a `Couldn't find template for digesting` line in the log. 4.15.0 fixed the case where the `cache` block wraps the component in a view. This fixes the case where the block sits in the component's template.
|
|
72
|
+
|
|
73
|
+
*Erik Axel Nielsen*
|
|
74
|
+
|
|
75
|
+
* Fix line numbers and, under coverage, missing output for ERB templates that Rails doesn't annotate.
|
|
76
|
+
|
|
77
|
+
On Rails 8.1+, ViewComponent compensated for the newline Rails adds to compiled ERB output ([rails/rails#53731](https://github.com/rails/rails/pull/53731)) for every ERB template, whether from a file or from `erb_template`. That newline lives inside the `<!-- BEGIN ... -->` annotation, which Rails only emits when `annotate_rendered_view_with_filenames` is enabled *and* the template's format is HTML. Compensating unconditionally shifted backtraces for non-HTML templates (`.text.erb`, `.css.erb`) and for every ERB template when annotations are disabled, such as in production, by one line. When coverage was running, the same mismatch made the annotation-stripping workaround remove real template source, so non-HTML templates rendered empty.
|
|
78
|
+
|
|
79
|
+
Inline templates now decide the compensation when they compile rather than when the component class is defined, matching file templates.
|
|
80
|
+
|
|
81
|
+
*Ryutaro Mizokami*
|
|
82
|
+
|
|
83
|
+
* Reduce per-render allocations. Inline renders drop 2 to 3 allocations and collection renders drop 4 to 8 depending on Rails/Ruby version by caching the instrumentation-enabled flag at the module level, memoizing the empty-details `Requested` per `LookupContext`, hoisting per-item metadata lookups out of the collection render loop, and dropping a few gratuitous `**` splats on the `Collection` API boundary.
|
|
84
|
+
|
|
85
|
+
*Joel Hawksley*
|
|
86
|
+
|
|
13
87
|
## 4.15.0
|
|
14
88
|
|
|
15
89
|
* Add experimental caching support, opt-in per component via `include ViewComponent::ExperimentallyCacheable`.
|
data/lib/view_component/base.rb
CHANGED
|
@@ -3,10 +3,12 @@
|
|
|
3
3
|
require "action_view"
|
|
4
4
|
require "action_view/base"
|
|
5
5
|
require "view_component/collection"
|
|
6
|
+
require "view_component/cache_digest"
|
|
6
7
|
require "view_component/compile_cache"
|
|
7
8
|
require "view_component/compiler"
|
|
8
9
|
require "view_component/config"
|
|
9
10
|
require "view_component/errors"
|
|
11
|
+
require "view_component/cacheable"
|
|
10
12
|
require "view_component/inline_template"
|
|
11
13
|
require "view_component/preview"
|
|
12
14
|
require "view_component/request_details"
|
|
@@ -31,6 +33,20 @@ end
|
|
|
31
33
|
module ViewComponent
|
|
32
34
|
class Base
|
|
33
35
|
class << self
|
|
36
|
+
# A digest of the component's source, ancestors, and rendered dependencies.
|
|
37
|
+
#
|
|
38
|
+
# @param finder [ActionView::LookupContext, nil]
|
|
39
|
+
# @param format [Symbol]
|
|
40
|
+
#
|
|
41
|
+
# @return [String]
|
|
42
|
+
def cache_digest(finder: nil, format: :html)
|
|
43
|
+
ViewComponent::CacheDigest.digest(
|
|
44
|
+
self,
|
|
45
|
+
finder: finder || ViewComponent::CacheDigest.default_finder,
|
|
46
|
+
format: format
|
|
47
|
+
)
|
|
48
|
+
end
|
|
49
|
+
|
|
34
50
|
delegate(*ViewComponent::Config.defaults.keys, to: :config)
|
|
35
51
|
|
|
36
52
|
# Returns the current config.
|
|
@@ -106,6 +122,10 @@ module ViewComponent
|
|
|
106
122
|
def render_in(view_context, **, &block)
|
|
107
123
|
self.class.__vc_compile(raise_errors: true) unless self.class.__vc_compiled?
|
|
108
124
|
|
|
125
|
+
if self.class.__vc_cache_output && !@__vc_cache_bypassed
|
|
126
|
+
return __vc_render_cached(view_context, block)
|
|
127
|
+
end
|
|
128
|
+
|
|
109
129
|
__vc_check_reused_instance!
|
|
110
130
|
__vc_reset_render_state!
|
|
111
131
|
|
|
@@ -423,6 +443,21 @@ module ViewComponent
|
|
|
423
443
|
@view_context.instance_variable_set(:@virtual_path, old_virtual_path)
|
|
424
444
|
end
|
|
425
445
|
|
|
446
|
+
# Use the component's source digest for fragments inside its own template.
|
|
447
|
+
#
|
|
448
|
+
# @private
|
|
449
|
+
def digest_path_from_template(template)
|
|
450
|
+
component_path = self.class.virtual_path
|
|
451
|
+
return super unless component_path && template.virtual_path == component_path
|
|
452
|
+
|
|
453
|
+
digest = self.class.cache_digest(
|
|
454
|
+
finder: lookup_context,
|
|
455
|
+
format: template.format || lookup_context.formats.first || :html
|
|
456
|
+
)
|
|
457
|
+
|
|
458
|
+
digest.present? ? "#{component_path}:#{digest}" : component_path
|
|
459
|
+
end
|
|
460
|
+
|
|
426
461
|
private
|
|
427
462
|
|
|
428
463
|
attr_reader :view_context
|
|
@@ -631,7 +666,7 @@ module ViewComponent
|
|
|
631
666
|
# @param spacer_component [ViewComponent::Base] Component instance to be rendered between items.
|
|
632
667
|
# @param args [Arguments] Arguments to pass to the ViewComponent every time.
|
|
633
668
|
def with_collection(collection, spacer_component: nil, **args)
|
|
634
|
-
Collection.new(self, collection, spacer_component,
|
|
669
|
+
Collection.new(self, collection, spacer_component, args)
|
|
635
670
|
end
|
|
636
671
|
|
|
637
672
|
# @private
|
|
@@ -668,6 +703,7 @@ module ViewComponent
|
|
|
668
703
|
# owner is included in a prefix like `ApplicationComponent.inherited`.
|
|
669
704
|
child.identifier = caller_locations(1, 10).reject { |l| l.base_label == "inherited" }[0].path
|
|
670
705
|
child.virtual_path = child.name&.underscore
|
|
706
|
+
ViewComponent::CacheDigest.register(child)
|
|
671
707
|
|
|
672
708
|
# Set collection parameter to the extended component
|
|
673
709
|
child.with_collection_parameter(__vc_provided_collection_parameter)
|
|
@@ -707,6 +743,17 @@ module ViewComponent
|
|
|
707
743
|
@__vc_compiler ||= Compiler.new(self)
|
|
708
744
|
end
|
|
709
745
|
|
|
746
|
+
# Synthetic digest source computed at boot. Not inherited, since each
|
|
747
|
+
# component's source differs.
|
|
748
|
+
#
|
|
749
|
+
# @private
|
|
750
|
+
attr_accessor :__vc_cache_digest_source
|
|
751
|
+
|
|
752
|
+
# @private
|
|
753
|
+
def __vc_template_formats
|
|
754
|
+
__vc_compiler.template_formats
|
|
755
|
+
end
|
|
756
|
+
|
|
710
757
|
# Set the parameter name used when rendering elements of a collection ([documentation](/guide/collections)):
|
|
711
758
|
#
|
|
712
759
|
# ```ruby
|
|
@@ -716,7 +763,7 @@ module ViewComponent
|
|
|
716
763
|
# @param parameter [Symbol] The parameter name used when rendering elements of a collection.
|
|
717
764
|
def with_collection_parameter(parameter)
|
|
718
765
|
@__vc_provided_collection_parameter = parameter
|
|
719
|
-
@
|
|
766
|
+
@__vc_collection_arguments = nil
|
|
720
767
|
end
|
|
721
768
|
|
|
722
769
|
# Strips trailing whitespace from templates before compiling them.
|
|
@@ -746,10 +793,14 @@ module ViewComponent
|
|
|
746
793
|
# rendering is optional.
|
|
747
794
|
# @private
|
|
748
795
|
def __vc_validate_collection_parameter!(validate_default: false)
|
|
749
|
-
|
|
796
|
+
if validate_default
|
|
797
|
+
return if __vc_collection_arguments[:parameter_accepted]
|
|
750
798
|
|
|
751
|
-
|
|
752
|
-
|
|
799
|
+
raise MissingCollectionArgumentError.new(name, __vc_collection_parameter)
|
|
800
|
+
end
|
|
801
|
+
|
|
802
|
+
parameter = __vc_provided_collection_parameter
|
|
803
|
+
return if __vc_collection_parameter_accepted?(parameter)
|
|
753
804
|
|
|
754
805
|
raise MissingCollectionArgumentError.new(name, parameter)
|
|
755
806
|
end
|
|
@@ -766,53 +817,95 @@ module ViewComponent
|
|
|
766
817
|
|
|
767
818
|
# @private
|
|
768
819
|
def __vc_collection_parameter
|
|
769
|
-
|
|
820
|
+
__vc_collection_arguments[:parameter]
|
|
770
821
|
end
|
|
771
822
|
|
|
772
823
|
# @private
|
|
773
824
|
def __vc_collection_counter_parameter
|
|
774
|
-
|
|
825
|
+
__vc_collection_arguments[:counter_parameter]
|
|
775
826
|
end
|
|
776
827
|
|
|
777
828
|
# @private
|
|
778
829
|
def __vc_counter_argument_present?
|
|
779
|
-
|
|
830
|
+
__vc_collection_arguments[:counter]
|
|
780
831
|
end
|
|
781
832
|
|
|
782
833
|
# @private
|
|
783
834
|
def __vc_collection_iteration_parameter
|
|
784
|
-
|
|
835
|
+
__vc_collection_arguments[:iteration_parameter]
|
|
785
836
|
end
|
|
786
837
|
|
|
787
838
|
# @private
|
|
788
839
|
def __vc_iteration_argument_present?
|
|
789
|
-
|
|
840
|
+
__vc_collection_arguments[:iteration]
|
|
841
|
+
end
|
|
842
|
+
|
|
843
|
+
# @private
|
|
844
|
+
def __vc_build_collection_arguments
|
|
845
|
+
names =
|
|
846
|
+
if respond_to?(:attribute_names)
|
|
847
|
+
attribute_names.map(&:to_sym)
|
|
848
|
+
else
|
|
849
|
+
__vc_initialize_signatures.flat_map { |_owner, parameters| parameters.map(&:last) }
|
|
850
|
+
end
|
|
851
|
+
parameter = __vc_provided_collection_parameter || (name && name.demodulize.underscore.chomp("_component").to_sym)
|
|
852
|
+
counter_parameter = :"#{parameter}_counter"
|
|
853
|
+
iteration_parameter = :"#{parameter}_iteration"
|
|
854
|
+
|
|
855
|
+
@__vc_collection_arguments = Ractor.make_shareable(
|
|
856
|
+
{
|
|
857
|
+
initialize_parameter_names: names,
|
|
858
|
+
parameter: parameter,
|
|
859
|
+
parameter_accepted: parameter.nil? || names.include?(parameter) || __vc_splatted_keyword_argument_present?,
|
|
860
|
+
counter_parameter: counter_parameter,
|
|
861
|
+
counter: names.include?(counter_parameter),
|
|
862
|
+
iteration_parameter: iteration_parameter,
|
|
863
|
+
iteration: names.include?(iteration_parameter)
|
|
864
|
+
}
|
|
865
|
+
)
|
|
790
866
|
end
|
|
791
867
|
|
|
792
868
|
private
|
|
793
869
|
|
|
794
|
-
def
|
|
795
|
-
|
|
870
|
+
def __vc_collection_arguments
|
|
871
|
+
@__vc_collection_arguments || __vc_build_collection_arguments
|
|
796
872
|
end
|
|
797
873
|
|
|
798
|
-
def
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
874
|
+
def __vc_collection_parameter_accepted?(parameter)
|
|
875
|
+
parameter.nil? || __vc_initialize_parameter_names.include?(parameter) || __vc_splatted_keyword_argument_present?
|
|
876
|
+
end
|
|
877
|
+
|
|
878
|
+
def __vc_splatted_keyword_argument_present?
|
|
879
|
+
prepended_modules = ancestors.grep(Class).flat_map { |klass| klass.ancestors.take_while { |mod| mod != klass } }
|
|
880
|
+
|
|
881
|
+
__vc_initialize_signatures.any? do |owner, parameters|
|
|
882
|
+
parameters.any? do |type, name|
|
|
883
|
+
type == :keyrest && (name != :** || !prepended_modules.include?(owner))
|
|
804
884
|
end
|
|
885
|
+
end
|
|
805
886
|
end
|
|
806
887
|
|
|
807
|
-
def
|
|
808
|
-
|
|
888
|
+
def __vc_initialize_parameter_names
|
|
889
|
+
__vc_collection_arguments[:initialize_parameter_names]
|
|
809
890
|
end
|
|
810
891
|
|
|
811
|
-
def
|
|
812
|
-
|
|
892
|
+
def __vc_initialize_signatures
|
|
893
|
+
signatures = []
|
|
894
|
+
method = instance_method(:initialize)
|
|
895
|
+
while method
|
|
896
|
+
signatures << [method.owner, method.parameters]
|
|
897
|
+
break unless method.parameters.any? { |type, _| type == :keyrest }
|
|
898
|
+
|
|
899
|
+
method = method.super_method
|
|
900
|
+
end
|
|
901
|
+
signatures
|
|
813
902
|
end
|
|
903
|
+
|
|
904
|
+
attr_reader :__vc_provided_collection_parameter
|
|
814
905
|
end
|
|
815
906
|
|
|
907
|
+
include ViewComponent::Cacheable
|
|
908
|
+
|
|
816
909
|
ActiveSupport.run_load_hooks(:view_component, self)
|
|
817
910
|
end
|
|
818
911
|
end
|
|
@@ -27,11 +27,6 @@ module ViewComponent
|
|
|
27
27
|
end
|
|
28
28
|
|
|
29
29
|
dependencies + CacheDigest.dependencies_in(template)
|
|
30
|
-
rescue
|
|
31
|
-
# A broken digest is preferable to a broken render. Falling back to the
|
|
32
|
-
# dependencies Rails found on its own means the component simply isn't
|
|
33
|
-
# tracked, which is the pre-existing behavior.
|
|
34
|
-
super
|
|
35
30
|
end
|
|
36
31
|
|
|
37
32
|
# @private
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "action_view/digestor"
|
|
4
|
+
|
|
5
|
+
module ViewComponent
|
|
6
|
+
module CacheDigest
|
|
7
|
+
# Lets `ActionView::Digestor` find the synthetic templates `Resolver` builds.
|
|
8
|
+
#
|
|
9
|
+
# Prepended to the Digestor's singleton class rather than appending
|
|
10
|
+
# `Resolver` to the application's view paths, so the resolver is only ever
|
|
11
|
+
# consulted while digesting. Code that walks `view_paths` and expects every
|
|
12
|
+
# entry to be a file system resolver is unaffected.
|
|
13
|
+
#
|
|
14
|
+
# @private
|
|
15
|
+
module DigestorLookup
|
|
16
|
+
# @private
|
|
17
|
+
def self.install!
|
|
18
|
+
digestor = ActionView::Digestor.singleton_class
|
|
19
|
+
return if digestor.include?(self)
|
|
20
|
+
|
|
21
|
+
digestor.prepend(self)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
private
|
|
25
|
+
|
|
26
|
+
def find_template(finder, name, prefixes, partial, keys)
|
|
27
|
+
Resolver.instance.find_templates(name, prefixes.first, partial, {formats: finder.formats}).first || super
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
@@ -30,11 +30,6 @@ module ViewComponent
|
|
|
30
30
|
return [] unless component
|
|
31
31
|
|
|
32
32
|
[build_template(component, virtual_path, details)]
|
|
33
|
-
rescue
|
|
34
|
-
# Never let digest resolution break rendering. Returning no template
|
|
35
|
-
# makes the Digestor treat this as a missing node, which degrades to
|
|
36
|
-
# the behavior components have without this feature.
|
|
37
|
-
[]
|
|
38
33
|
end
|
|
39
34
|
|
|
40
35
|
def to_s
|
|
@@ -47,21 +42,11 @@ module ViewComponent
|
|
|
47
42
|
end
|
|
48
43
|
alias_method :==, :eql?
|
|
49
44
|
|
|
50
|
-
private
|
|
51
|
-
|
|
52
|
-
def build_template(component, virtual_path, details)
|
|
53
|
-
ActionView::Template.new(
|
|
54
|
-
source_for(component),
|
|
55
|
-
"view_component cache digest for #{component.name}",
|
|
56
|
-
ActionView::Template.handler_for_extension(:erb),
|
|
57
|
-
locals: [],
|
|
58
|
-
format: Array(details[:formats]).first || :html,
|
|
59
|
-
virtual_path: virtual_path
|
|
60
|
-
)
|
|
61
|
-
end
|
|
62
|
-
|
|
63
45
|
# The synthetic source. Every section exists to change this string when
|
|
64
46
|
# something the component renders from changes.
|
|
47
|
+
#
|
|
48
|
+
# @param component [Class]
|
|
49
|
+
# @return [String]
|
|
65
50
|
def source_for(component)
|
|
66
51
|
parts = []
|
|
67
52
|
|
|
@@ -93,6 +78,19 @@ module ViewComponent
|
|
|
93
78
|
parts.join("\n")
|
|
94
79
|
end
|
|
95
80
|
|
|
81
|
+
private
|
|
82
|
+
|
|
83
|
+
def build_template(component, virtual_path, details)
|
|
84
|
+
ActionView::Template.new(
|
|
85
|
+
component.__vc_cache_digest_source || source_for(component),
|
|
86
|
+
"view_component cache digest for #{component.name}",
|
|
87
|
+
ActionView::Template.handler_for_extension(:erb),
|
|
88
|
+
locals: [],
|
|
89
|
+
format: Array(details[:formats]).first || :html,
|
|
90
|
+
virtual_path: virtual_path
|
|
91
|
+
)
|
|
92
|
+
end
|
|
93
|
+
|
|
96
94
|
# The component and any component superclasses, nearest first. Including
|
|
97
95
|
# ancestors means editing `ApplicationComponent` invalidates every
|
|
98
96
|
# component that inherits from it.
|
|
@@ -170,8 +168,9 @@ module ViewComponent
|
|
|
170
168
|
end
|
|
171
169
|
|
|
172
170
|
# Built once at load time rather than memoized, so no class-level state
|
|
173
|
-
# is written after boot.
|
|
174
|
-
# every call so it can't go stale when a
|
|
171
|
+
# is written after boot. Unless sources were precomputed at boot, the
|
|
172
|
+
# resolver reads from disk on every call so it can't go stale when a
|
|
173
|
+
# component changes.
|
|
175
174
|
INSTANCE = new
|
|
176
175
|
end
|
|
177
176
|
end
|
|
@@ -21,15 +21,15 @@ module ViewComponent
|
|
|
21
21
|
# not just its template.
|
|
22
22
|
#
|
|
23
23
|
# This module fixes both, reusing Rails' own `ActionView::Digestor` rather than
|
|
24
|
-
# reimplementing static analysis.
|
|
25
|
-
#
|
|
26
|
-
# every hook here short-circuits.
|
|
24
|
+
# reimplementing static analysis. Every discovered component is tracked
|
|
25
|
+
# automatically, independently of output caching.
|
|
27
26
|
#
|
|
28
27
|
# @private
|
|
29
28
|
module CacheDigest
|
|
30
29
|
extend ActiveSupport::Autoload
|
|
31
30
|
|
|
32
31
|
autoload :DependencyTracking
|
|
32
|
+
autoload :DigestorLookup
|
|
33
33
|
autoload :Resolver
|
|
34
34
|
|
|
35
35
|
# Prefix for the synthetic virtual paths components are digested under.
|
|
@@ -56,8 +56,7 @@ module ViewComponent
|
|
|
56
56
|
# Rails' escape hatch for dependencies static analysis can't see.
|
|
57
57
|
EXPLICIT_DEPENDENCY = /#\s*Template Dependency:\s*(\S+)/
|
|
58
58
|
class << self
|
|
59
|
-
# Virtual paths of components
|
|
60
|
-
# their class names.
|
|
59
|
+
# Virtual paths of components, mapped to their class names.
|
|
61
60
|
#
|
|
62
61
|
# Class *names* rather than class objects so the registry survives
|
|
63
62
|
# autoloader reloads without pinning stale constants in memory.
|
|
@@ -67,16 +66,19 @@ module ViewComponent
|
|
|
67
66
|
@registry ||= {}
|
|
68
67
|
end
|
|
69
68
|
|
|
70
|
-
# @return [Boolean] whether any component has opted in.
|
|
71
|
-
def enabled?
|
|
72
|
-
!registry.empty?
|
|
73
|
-
end
|
|
74
|
-
|
|
75
69
|
# @private
|
|
76
70
|
def register(component)
|
|
77
71
|
return unless component.virtual_path && component.name
|
|
72
|
+
return if registry[component.virtual_path] == component.name
|
|
78
73
|
|
|
79
74
|
registry[component.virtual_path] = component.name
|
|
75
|
+
|
|
76
|
+
# Components register as they load, and under lazy loading that happens
|
|
77
|
+
# after Action View has already digested and memoized some templates.
|
|
78
|
+
# Those digests were computed without this component's dependencies and
|
|
79
|
+
# would otherwise be served for the rest of the process, making the
|
|
80
|
+
# digest a function of load order rather than of source.
|
|
81
|
+
expire_digests
|
|
80
82
|
end
|
|
81
83
|
|
|
82
84
|
# The synthetic virtual path a component is digested under.
|
|
@@ -100,28 +102,22 @@ module ViewComponent
|
|
|
100
102
|
constantize_component(name)
|
|
101
103
|
end
|
|
102
104
|
|
|
103
|
-
# Scan a template's source for renders of
|
|
104
|
-
#
|
|
105
|
-
# Called for every template Rails digests, so it exits early when the
|
|
106
|
-
# feature is unused.
|
|
105
|
+
# Scan a template's source for renders of components.
|
|
107
106
|
#
|
|
108
107
|
# @return [Array<String>] synthetic virtual paths
|
|
109
108
|
def dependencies_in(template)
|
|
110
|
-
return [] unless enabled?
|
|
111
|
-
|
|
112
109
|
component_paths_in(template.source)
|
|
113
110
|
end
|
|
114
111
|
|
|
115
112
|
# Scan arbitrary source (a template or a component's Ruby file) for
|
|
116
|
-
# renders of
|
|
113
|
+
# renders of components.
|
|
117
114
|
#
|
|
118
115
|
# @return [Array<String>] synthetic virtual paths
|
|
119
116
|
def component_paths_in(source)
|
|
120
117
|
return [] unless source.is_a?(String) && source.include?("render")
|
|
121
118
|
|
|
122
119
|
source.scan(RENDER_CALL).flatten.uniq.filter_map do |constant_name|
|
|
123
|
-
|
|
124
|
-
virtual_path_for(component) if component
|
|
120
|
+
registered_path_for(constantize_component(constant_name))
|
|
125
121
|
end
|
|
126
122
|
end
|
|
127
123
|
|
|
@@ -146,9 +142,6 @@ module ViewComponent
|
|
|
146
142
|
RENDER_PARSER.new(name, source).render_calls.uniq.select do |path|
|
|
147
143
|
source.include?(path) || source.include?(path.sub(%r{(\A|/)_}, '\1'))
|
|
148
144
|
end
|
|
149
|
-
rescue
|
|
150
|
-
# Never let digest computation break rendering.
|
|
151
|
-
[]
|
|
152
145
|
end
|
|
153
146
|
|
|
154
147
|
# Action View has shipped its render parser as a class (Rails 7.1, and
|
|
@@ -177,17 +170,19 @@ module ViewComponent
|
|
|
177
170
|
next unless /\A(?:::)?[A-Z]/.match?(declared)
|
|
178
171
|
|
|
179
172
|
component = constantize_component(declared)
|
|
180
|
-
|
|
173
|
+
virtual_path = registered_path_for(component)
|
|
174
|
+
[declared, virtual_path] if virtual_path
|
|
181
175
|
end
|
|
182
176
|
end
|
|
183
177
|
|
|
184
178
|
# Compute the digest of a component using Rails' digest tree.
|
|
185
179
|
#
|
|
186
|
-
# @param component [Class] a component
|
|
180
|
+
# @param component [Class] a component class
|
|
187
181
|
# @param finder [ActionView::LookupContext]
|
|
188
182
|
# @param format [Symbol]
|
|
189
183
|
# @return [String]
|
|
190
184
|
def digest(component, finder: default_finder, format: :html)
|
|
185
|
+
register(component)
|
|
191
186
|
virtual_path = virtual_path_for(component)
|
|
192
187
|
return "" unless virtual_path
|
|
193
188
|
|
|
@@ -205,38 +200,70 @@ module ViewComponent
|
|
|
205
200
|
|
|
206
201
|
# Wire the tracker and resolver into Action View.
|
|
207
202
|
#
|
|
208
|
-
#
|
|
209
|
-
# steps below are individually idempotent, so no "already installed" flag
|
|
210
|
-
# is kept. Both hooks short-circuit while the registry is empty, so
|
|
211
|
-
# applications that never opt in are unaffected.
|
|
203
|
+
# Installed during engine initialization. Both steps are idempotent.
|
|
212
204
|
#
|
|
213
205
|
# @private
|
|
214
206
|
def install!
|
|
215
207
|
DependencyTracking.install!
|
|
208
|
+
DigestorLookup.install!
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
# Compute component digests at boot, so requests read them from memory
|
|
212
|
+
# instead of reading and scanning source files.
|
|
213
|
+
#
|
|
214
|
+
# Only safe when templates are cached at boot: the precomputed sources are
|
|
215
|
+
# never refreshed.
|
|
216
|
+
#
|
|
217
|
+
# @param components [Array<Class>]
|
|
218
|
+
# @return [void]
|
|
219
|
+
# @private
|
|
220
|
+
def precompute!(components)
|
|
221
|
+
# Register everything first: a registration expires every memoized
|
|
222
|
+
# digest, so registering midway would discard digests already warmed.
|
|
223
|
+
components.each { |component| register(component) }
|
|
216
224
|
|
|
217
|
-
|
|
218
|
-
|
|
225
|
+
components.each do |component|
|
|
226
|
+
component.__vc_cache_digest_source = Resolver.instance.source_for(component).freeze
|
|
227
|
+
end
|
|
219
228
|
|
|
220
|
-
|
|
229
|
+
# Warms the same digest cache requests read, since a lookup context's
|
|
230
|
+
# cache is shared by every context with the same details.
|
|
231
|
+
components.each do |component|
|
|
232
|
+
component.__vc_template_formats.each do |format|
|
|
233
|
+
finder = ActionView::LookupContext.new(ActionController::Base.view_paths, formats: [format])
|
|
234
|
+
component.cache_digest(finder: finder, format: format)
|
|
235
|
+
end
|
|
221
236
|
end
|
|
222
237
|
end
|
|
223
238
|
|
|
224
239
|
private
|
|
225
240
|
|
|
226
|
-
#
|
|
241
|
+
# Record the actual class name rather than trying to reverse a virtual
|
|
242
|
+
# path, which may contain acronyms or be overridden by the component.
|
|
243
|
+
def registered_path_for(component)
|
|
244
|
+
return unless component
|
|
245
|
+
|
|
246
|
+
register(component)
|
|
247
|
+
virtual_path_for(component)
|
|
248
|
+
end
|
|
249
|
+
|
|
250
|
+
# Drop Action View's memoized template digests, leaving its resolver
|
|
251
|
+
# caches alone: no template changed, only the set of dependencies the
|
|
252
|
+
# Digestor can see.
|
|
253
|
+
def expire_digests
|
|
254
|
+
ActionView::LookupContext::DetailsKey.digest_caches.each(&:clear)
|
|
255
|
+
end
|
|
256
|
+
|
|
257
|
+
# Resolve a constant name to any ViewComponent.
|
|
227
258
|
#
|
|
228
259
|
# Returns nil for anything else, including constants that don't exist.
|
|
229
260
|
# Autoloading here is safe: the template is about to render this constant
|
|
230
261
|
# anyway.
|
|
231
262
|
def constantize_component(constant_name)
|
|
232
263
|
component = constant_name.safe_constantize
|
|
233
|
-
return unless component.is_a?(Class)
|
|
234
|
-
return unless component.respond_to?(:__vc_cacheable?) && component.__vc_cacheable?
|
|
264
|
+
return unless component.is_a?(Class) && component < ViewComponent::Base
|
|
235
265
|
|
|
236
266
|
component
|
|
237
|
-
rescue
|
|
238
|
-
# Never let digest computation break rendering.
|
|
239
|
-
nil
|
|
240
267
|
end
|
|
241
268
|
end
|
|
242
269
|
|
|
@@ -3,25 +3,13 @@
|
|
|
3
3
|
require "view_component/cache_digest"
|
|
4
4
|
|
|
5
5
|
module ViewComponent
|
|
6
|
-
#
|
|
6
|
+
# Caching support for ViewComponents.
|
|
7
7
|
#
|
|
8
|
-
#
|
|
9
|
-
#
|
|
10
|
-
# https://github.com/ViewComponent/view_component/issues/234.
|
|
11
|
-
#
|
|
12
|
-
# Including this module does two things:
|
|
13
|
-
#
|
|
14
|
-
# 1. Registers the component with Rails' template digest tree, so a
|
|
15
|
-
# `<% cache %>` block wrapping the component in a view is invalidated when
|
|
16
|
-
# the component's template, Ruby class, sidecar files, or child components
|
|
17
|
-
# change.
|
|
18
|
-
# 2. Enables the `cache_on` macro, which caches the component's own rendered
|
|
19
|
-
# output.
|
|
8
|
+
# Provides the `cache_on` macro, which caches the component's own rendered
|
|
9
|
+
# output. Components don't cache their output until they call `cache_on`.
|
|
20
10
|
#
|
|
21
11
|
# ```ruby
|
|
22
12
|
# class MessageComponent < ViewComponent::Base
|
|
23
|
-
# include ViewComponent::ExperimentallyCacheable
|
|
24
|
-
#
|
|
25
13
|
# cache_on :message
|
|
26
14
|
#
|
|
27
15
|
# def initialize(message:)
|
|
@@ -29,7 +17,7 @@ module ViewComponent
|
|
|
29
17
|
# end
|
|
30
18
|
# end
|
|
31
19
|
# ```
|
|
32
|
-
module
|
|
20
|
+
module Cacheable
|
|
33
21
|
extend ActiveSupport::Concern
|
|
34
22
|
|
|
35
23
|
# Stands in for `nil` in the cache key. Without it, `expand_cache_key`
|
|
@@ -37,11 +25,6 @@ module ViewComponent
|
|
|
37
25
|
# that respect would share an entry.
|
|
38
26
|
NIL_CACHE_VALUE = :__vc_nil
|
|
39
27
|
|
|
40
|
-
included do
|
|
41
|
-
ViewComponent::CacheDigest.install!
|
|
42
|
-
ViewComponent::CacheDigest.register(self)
|
|
43
|
-
end
|
|
44
|
-
|
|
45
28
|
class_methods do
|
|
46
29
|
# Declare the values that identify a rendering of this component.
|
|
47
30
|
#
|
|
@@ -54,8 +37,7 @@ module ViewComponent
|
|
|
54
37
|
# ```
|
|
55
38
|
#
|
|
56
39
|
# Calling `cache_on` opts the component into caching its own output.
|
|
57
|
-
#
|
|
58
|
-
# Rails' digest tree.
|
|
40
|
+
# Components that don't call it aren't cached.
|
|
59
41
|
#
|
|
60
42
|
# Pass `if:` or `unless:` to cache only some renders. Both accept a method
|
|
61
43
|
# name or a proc evaluated on the component:
|
|
@@ -118,11 +100,6 @@ module ViewComponent
|
|
|
118
100
|
superclass.__vc_cache_unless if superclass.respond_to?(:__vc_cache_unless)
|
|
119
101
|
end
|
|
120
102
|
|
|
121
|
-
# @private
|
|
122
|
-
def __vc_cacheable?
|
|
123
|
-
true
|
|
124
|
-
end
|
|
125
|
-
|
|
126
103
|
# Whether this component caches its own rendered output.
|
|
127
104
|
#
|
|
128
105
|
# @return [Boolean]
|
|
@@ -130,67 +107,11 @@ module ViewComponent
|
|
|
130
107
|
__vc_cache_on.any?
|
|
131
108
|
end
|
|
132
109
|
|
|
133
|
-
#
|
|
134
|
-
#
|
|
135
|
-
# partials it renders.
|
|
110
|
+
# `__vc_caches_output?`, stored when the component compiles so rendering
|
|
111
|
+
# reads a flag instead of computing it.
|
|
136
112
|
#
|
|
137
|
-
# Computed with Rails' own `ActionView::Digestor`, so it's the same digest
|
|
138
|
-
# used to invalidate `<% cache %>` blocks.
|
|
139
|
-
#
|
|
140
|
-
# Usable outside a request, where no view context exists:
|
|
141
|
-
#
|
|
142
|
-
# ```ruby
|
|
143
|
-
# MessageComponent.cache_digest
|
|
144
|
-
# ```
|
|
145
|
-
#
|
|
146
|
-
# @param finder [ActionView::LookupContext] Defaults to a lookup context
|
|
147
|
-
# built from `ActionController::Base.view_paths`.
|
|
148
|
-
# @param format [Symbol]
|
|
149
|
-
# @return [String]
|
|
150
|
-
def cache_digest(finder: nil, format: :html)
|
|
151
|
-
ViewComponent::CacheDigest.digest(
|
|
152
|
-
self,
|
|
153
|
-
finder: finder || ViewComponent::CacheDigest.default_finder,
|
|
154
|
-
format: format
|
|
155
|
-
)
|
|
156
|
-
end
|
|
157
|
-
|
|
158
113
|
# @private
|
|
159
|
-
|
|
160
|
-
super
|
|
161
|
-
ViewComponent::CacheDigest.register(child)
|
|
162
|
-
end
|
|
163
|
-
end
|
|
164
|
-
|
|
165
|
-
# Renders the component, reading from and writing to the Rails cache when
|
|
166
|
-
# `cache_on` has been declared.
|
|
167
|
-
#
|
|
168
|
-
# @private
|
|
169
|
-
def render_in(view_context, **, &block)
|
|
170
|
-
return super unless self.class.__vc_caches_output?
|
|
171
|
-
|
|
172
|
-
# Content provided by the caller isn't part of the cache key, so caching
|
|
173
|
-
# it would serve one caller's content to another. Raised whether or not
|
|
174
|
-
# caching is currently enabled, so the conflict surfaces in development
|
|
175
|
-
# and test rather than only in production.
|
|
176
|
-
if block || __vc_content_set_by_with_content_defined? || __vc_slots_set_by_caller?
|
|
177
|
-
raise ContentPassedToCachedComponentError.new(self.class.name)
|
|
178
|
-
end
|
|
179
|
-
|
|
180
|
-
return super unless __vc_cache_enabled?(view_context)
|
|
181
|
-
|
|
182
|
-
store = Rails.cache
|
|
183
|
-
key = cache_key(view_context)
|
|
184
|
-
|
|
185
|
-
if (cached = store.read(key))
|
|
186
|
-
# Safe to mark as HTML-safe: the cached string was produced by this same
|
|
187
|
-
# rendering pipeline, which escapes output before it's written.
|
|
188
|
-
return cached.html_safe # rubocop:disable Rails/OutputSafety
|
|
189
|
-
end
|
|
190
|
-
|
|
191
|
-
super.tap do |output|
|
|
192
|
-
store.write(key, output.to_s)
|
|
193
|
-
end
|
|
114
|
+
attr_accessor :__vc_cache_output
|
|
194
115
|
end
|
|
195
116
|
|
|
196
117
|
# The cache key for this rendering of the component.
|
|
@@ -227,6 +148,38 @@ module ViewComponent
|
|
|
227
148
|
|
|
228
149
|
private
|
|
229
150
|
|
|
151
|
+
# Renders through the Rails cache. Called by `Base#render_in` for
|
|
152
|
+
# components that declare `cache_on`.
|
|
153
|
+
def __vc_render_cached(view_context, content_block)
|
|
154
|
+
# Content provided by the caller isn't part of the cache key, so caching
|
|
155
|
+
# it would serve one caller's content to another. Raised whether or not
|
|
156
|
+
# caching is currently enabled, so the conflict surfaces in development
|
|
157
|
+
# and test rather than only in production.
|
|
158
|
+
if content_block || __vc_content_set_by_with_content_defined? || __vc_slots_set_by_caller?
|
|
159
|
+
raise ContentPassedToCachedComponentError.new(self.class.name)
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
return __vc_render_uncached(view_context) unless __vc_cache_enabled?(view_context)
|
|
163
|
+
|
|
164
|
+
store = Rails.cache
|
|
165
|
+
key = cache_key(view_context)
|
|
166
|
+
|
|
167
|
+
if (cached = store.read(key))
|
|
168
|
+
# Safe to mark as HTML-safe: the cached string was produced by this same
|
|
169
|
+
# rendering pipeline, which escapes output before it's written.
|
|
170
|
+
return cached.html_safe
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
__vc_render_uncached(view_context).tap do |output|
|
|
174
|
+
store.write(key, output.to_s)
|
|
175
|
+
end
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
def __vc_render_uncached(view_context)
|
|
179
|
+
@__vc_cache_bypassed = true
|
|
180
|
+
render_in(view_context)
|
|
181
|
+
end
|
|
182
|
+
|
|
230
183
|
# Slots set by the caller via `with_*`. Checked before rendering, so slots
|
|
231
184
|
# a component fills in for itself with a `default_*` method — which resolve
|
|
232
185
|
# lazily during the render — aren't counted.
|
|
@@ -12,6 +12,9 @@ module ViewComponent
|
|
|
12
12
|
|
|
13
13
|
delegate :size, to: :@collection
|
|
14
14
|
|
|
15
|
+
EMPTY_SPACER = "".html_safe.freeze
|
|
16
|
+
private_constant :EMPTY_SPACER
|
|
17
|
+
|
|
15
18
|
def render_in(view_context, **_, &block)
|
|
16
19
|
rendered = components.map! do |component|
|
|
17
20
|
component.render_in(view_context, &block)
|
|
@@ -36,18 +39,27 @@ module ViewComponent
|
|
|
36
39
|
# Always rebuild child component instances per render to avoid leaking
|
|
37
40
|
# request-scoped state from a previous render into a later one (GHSA).
|
|
38
41
|
def components
|
|
39
|
-
iterator = ActionView::PartialIteration.new(@collection.size)
|
|
40
|
-
|
|
41
42
|
component.__vc_validate_collection_parameter!(validate_default: true)
|
|
42
43
|
|
|
44
|
+
iterator = ActionView::PartialIteration.new(@collection.size)
|
|
45
|
+
collection_param = component.__vc_collection_parameter
|
|
46
|
+
counter_present = component.__vc_counter_argument_present?
|
|
47
|
+
counter_param = component.__vc_collection_counter_parameter if counter_present
|
|
48
|
+
iteration_present = component.__vc_iteration_argument_present?
|
|
49
|
+
iteration_param = component.__vc_collection_iteration_parameter if iteration_present
|
|
50
|
+
item_options = @options.dup
|
|
51
|
+
|
|
43
52
|
@collection.map do |item|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
53
|
+
item_options[collection_param] = item
|
|
54
|
+
item_options[counter_param] = iterator.index if counter_present
|
|
55
|
+
item_options[iteration_param] = iterator.dup if iteration_present
|
|
56
|
+
instance = component.new(**item_options)
|
|
57
|
+
iterator.iterate!
|
|
58
|
+
instance
|
|
47
59
|
end
|
|
48
60
|
end
|
|
49
61
|
|
|
50
|
-
def initialize(component, object, spacer_component,
|
|
62
|
+
def initialize(component, object, spacer_component, options = {})
|
|
51
63
|
@component = component
|
|
52
64
|
@collection = collection_variable(object || [])
|
|
53
65
|
@spacer_component = spacer_component
|
|
@@ -62,20 +74,11 @@ module ViewComponent
|
|
|
62
74
|
end
|
|
63
75
|
end
|
|
64
76
|
|
|
65
|
-
def component_options(item, iterator)
|
|
66
|
-
item_options = @options.dup
|
|
67
|
-
item_options[component.__vc_collection_parameter] = item
|
|
68
|
-
item_options[component.__vc_collection_counter_parameter] = iterator.index if component.__vc_counter_argument_present?
|
|
69
|
-
item_options[component.__vc_collection_iteration_parameter] = iterator.dup if component.__vc_iteration_argument_present?
|
|
70
|
-
|
|
71
|
-
item_options
|
|
72
|
-
end
|
|
73
|
-
|
|
74
77
|
# Render the spacer through a fresh `dup` so a collection rendered multiple
|
|
75
78
|
# times does not reuse (and trip the single-render guard on) the spacer
|
|
76
79
|
# instance passed by the caller.
|
|
77
80
|
def rendered_spacer(view_context)
|
|
78
|
-
return
|
|
81
|
+
return EMPTY_SPACER unless @spacer_component
|
|
79
82
|
|
|
80
83
|
spacer = @spacer_component.dup
|
|
81
84
|
if spacer.instance_variable_defined?(:@__vc_rendered)
|
|
@@ -25,5 +25,14 @@ module ViewComponent
|
|
|
25
25
|
def invalidate!
|
|
26
26
|
cache.clear
|
|
27
27
|
end
|
|
28
|
+
|
|
29
|
+
# Invalidates the cache when a template or a translation file is added,
|
|
30
|
+
# changed, or removed. Changes to Ruby files reload the application instead.
|
|
31
|
+
def template_watcher(file_watcher)
|
|
32
|
+
extensions = ActionView::Template::Handlers.extensions.map(&:to_s) + %w[yml yaml]
|
|
33
|
+
directories = ActiveSupport::Dependencies.autoload_paths.map(&:to_s).index_with(extensions)
|
|
34
|
+
|
|
35
|
+
file_watcher.new([], directories) { invalidate! }
|
|
36
|
+
end
|
|
28
37
|
end
|
|
29
38
|
end
|
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require "concurrent-ruby"
|
|
4
|
-
|
|
5
3
|
module ViewComponent
|
|
6
4
|
class Compiler
|
|
7
5
|
# Compiler development mode. Can be either:
|
|
@@ -41,6 +39,8 @@ module ViewComponent
|
|
|
41
39
|
return false
|
|
42
40
|
end
|
|
43
41
|
|
|
42
|
+
@component.__vc_build_collection_arguments
|
|
43
|
+
|
|
44
44
|
if raise_errors
|
|
45
45
|
@component.__vc_validate_initialization_parameters!
|
|
46
46
|
@component.__vc_validate_collection_parameter!
|
|
@@ -48,6 +48,8 @@ module ViewComponent
|
|
|
48
48
|
|
|
49
49
|
define_render_template_for
|
|
50
50
|
|
|
51
|
+
@component.__vc_cache_output = @component.__vc_caches_output?
|
|
52
|
+
|
|
51
53
|
@component.__vc_register_default_slots
|
|
52
54
|
@component.__vc_build_i18n_backend
|
|
53
55
|
|
|
@@ -74,6 +76,14 @@ module ViewComponent
|
|
|
74
76
|
filtered_templates
|
|
75
77
|
end
|
|
76
78
|
|
|
79
|
+
# Formats the component has templates for. Inline templates and `#call`
|
|
80
|
+
# methods have no format of their own and render as HTML.
|
|
81
|
+
#
|
|
82
|
+
# @return [Array<Symbol>]
|
|
83
|
+
def template_formats
|
|
84
|
+
gather_templates.map { |template| template.format || :html }.uniq
|
|
85
|
+
end
|
|
86
|
+
|
|
77
87
|
private
|
|
78
88
|
|
|
79
89
|
attr_reader :templates
|
|
@@ -3,11 +3,16 @@
|
|
|
3
3
|
require "rails"
|
|
4
4
|
require "view_component/config"
|
|
5
5
|
require "view_component/deprecation"
|
|
6
|
+
require "view_component/cache_digest"
|
|
6
7
|
|
|
7
8
|
module ViewComponent
|
|
8
9
|
class Engine < Rails::Engine # :nodoc:
|
|
9
10
|
config.view_component = ViewComponent::Config.current
|
|
10
11
|
|
|
12
|
+
initializer "view_component.cache_digest" do
|
|
13
|
+
ViewComponent::CacheDigest.install!
|
|
14
|
+
end
|
|
15
|
+
|
|
11
16
|
initializer "view_component.set_configs" do |app|
|
|
12
17
|
options = app.config.view_component
|
|
13
18
|
|
|
@@ -28,6 +33,7 @@ module ViewComponent
|
|
|
28
33
|
initializer "view_component.enable_instrumentation" do |app|
|
|
29
34
|
ActiveSupport.on_load(:view_component) do
|
|
30
35
|
if app.config.view_component.instrumentation_enabled.present?
|
|
36
|
+
ViewComponent::Instrumentation.enabled = true
|
|
31
37
|
ViewComponent::Base.prepend(ViewComponent::Instrumentation)
|
|
32
38
|
end
|
|
33
39
|
end
|
|
@@ -88,10 +94,11 @@ module ViewComponent
|
|
|
88
94
|
ActiveSupport.on_load(:after_initialize) do
|
|
89
95
|
if Rails.application.config.eager_load
|
|
90
96
|
ActiveSupport::Notifications.instrument("compile.view_component") do
|
|
91
|
-
ViewComponent::Base.descendants.
|
|
92
|
-
|
|
97
|
+
components = ViewComponent::Base.descendants.reject(&:anonymous?)
|
|
98
|
+
components.each(&:__vc_compile)
|
|
93
99
|
|
|
94
|
-
|
|
100
|
+
if ActionView::Base.cache_template_loading
|
|
101
|
+
ViewComponent::CacheDigest.precompute!(components.select(&:__vc_compiled?))
|
|
95
102
|
end
|
|
96
103
|
end
|
|
97
104
|
end
|
|
@@ -131,9 +138,22 @@ module ViewComponent
|
|
|
131
138
|
end
|
|
132
139
|
end
|
|
133
140
|
|
|
134
|
-
|
|
135
|
-
|
|
141
|
+
# Like Rails' own template cache expiry, only watch templates when they
|
|
142
|
+
# aren't cached at boot. With template caching on, as in production, no
|
|
143
|
+
# hook runs per request, so requests never read ActionView's caching
|
|
144
|
+
# class variable, which non-main Ractors can't access.
|
|
145
|
+
ActiveSupport.on_load(:action_view) do
|
|
146
|
+
next if ActionView::Base.cache_template_loading
|
|
147
|
+
|
|
148
|
+
template_watcher = nil
|
|
149
|
+
|
|
150
|
+
app.executor.to_run :before do
|
|
151
|
+
template_watcher ||= CompileCache.template_watcher(app.config.file_watcher)
|
|
152
|
+
template_watcher.execute_if_updated
|
|
153
|
+
end
|
|
136
154
|
end
|
|
155
|
+
|
|
156
|
+
app.reloader.before_class_unload { CompileCache.invalidate! }
|
|
137
157
|
end
|
|
138
158
|
end
|
|
139
159
|
end
|
|
@@ -8,8 +8,12 @@ module ViewComponent # :nodoc:
|
|
|
8
8
|
mod.prepend(self) unless self <= ViewComponent::Instrumentation
|
|
9
9
|
end
|
|
10
10
|
|
|
11
|
+
class << self
|
|
12
|
+
attr_accessor :enabled
|
|
13
|
+
end
|
|
14
|
+
|
|
11
15
|
def render_in(...)
|
|
12
|
-
return super
|
|
16
|
+
return super unless Instrumentation.enabled
|
|
13
17
|
|
|
14
18
|
payload = {
|
|
15
19
|
name: self.class.name,
|
|
@@ -24,8 +24,21 @@ module ViewComponent
|
|
|
24
24
|
# The hash `user_details` would normally be the standard arguments that
|
|
25
25
|
# `render` accepts, but there's currently no mechanism for users to
|
|
26
26
|
# provide these when calling render on a ViewComponent.
|
|
27
|
-
|
|
28
|
-
|
|
27
|
+
if user_details.equal?(EMPTY_DETAILS)
|
|
28
|
+
# Fast path: memoize the empty-details Requested per LookupContext.
|
|
29
|
+
# Rendered many times with the same context, the tuple/Requested
|
|
30
|
+
# allocations from ActionView are then paid at most once.
|
|
31
|
+
cached = instance_variable_defined?(:@__vc_requested_details_cache) &&
|
|
32
|
+
@__vc_requested_details_cache
|
|
33
|
+
return cached if cached
|
|
34
|
+
|
|
35
|
+
details, from_cache = detail_args_for(EMPTY_DETAILS)
|
|
36
|
+
@__vc_requested_details_cache =
|
|
37
|
+
from_cache || ActionView::TemplateDetails::Requested.new(**details)
|
|
38
|
+
else
|
|
39
|
+
details, from_cache = detail_args_for(user_details)
|
|
40
|
+
from_cache || ActionView::TemplateDetails::Requested.new(**details)
|
|
41
|
+
end
|
|
29
42
|
end
|
|
30
43
|
end
|
|
31
44
|
end
|
|
@@ -60,13 +60,7 @@ module ViewComponent
|
|
|
60
60
|
# Strip the annotation line to maintain correct line numbers when coverage
|
|
61
61
|
# is running (avoids segfault from negative lineno)
|
|
62
62
|
def strip_annotation_line?
|
|
63
|
-
erb_newline_compensation_needed? &&
|
|
64
|
-
coverage_running? &&
|
|
65
|
-
ActionView::Base.annotate_rendered_view_with_filenames
|
|
66
|
-
end
|
|
67
|
-
|
|
68
|
-
def erb_newline_compensation_needed?
|
|
69
|
-
Rails::VERSION::MAJOR >= 8 && Rails::VERSION::MINOR > 0 && details.handler == :erb
|
|
63
|
+
erb_newline_compensation_needed? && coverage_running?
|
|
70
64
|
end
|
|
71
65
|
|
|
72
66
|
def compiled_source
|
|
@@ -82,22 +76,11 @@ module ViewComponent
|
|
|
82
76
|
def initialize(component:, inline_template:)
|
|
83
77
|
details = ActionView::TemplateDetails.new(nil, inline_template.language.to_sym, nil, nil)
|
|
84
78
|
|
|
85
|
-
# Rails 8.1 added a newline to compiled ERB output (rails/rails#53731).
|
|
86
|
-
# Subtract 1 to compensate for correct line numbers in stack traces.
|
|
87
|
-
# Inline templates start at line 2+ (defined inside a class), so this
|
|
88
|
-
# won't result in negative line numbers that cause segfaults with coverage.
|
|
89
|
-
lineno =
|
|
90
|
-
if Rails::VERSION::MAJOR >= 8 && Rails::VERSION::MINOR > 0 && details.handler == :erb
|
|
91
|
-
inline_template.lineno - 1
|
|
92
|
-
else
|
|
93
|
-
inline_template.lineno
|
|
94
|
-
end
|
|
95
|
-
|
|
96
79
|
super(
|
|
97
80
|
component: component,
|
|
98
81
|
details: details,
|
|
99
82
|
path: inline_template.path,
|
|
100
|
-
lineno: lineno,
|
|
83
|
+
lineno: inline_template.lineno,
|
|
101
84
|
)
|
|
102
85
|
|
|
103
86
|
@source = inline_template.source.dup
|
|
@@ -106,6 +89,19 @@ module ViewComponent
|
|
|
106
89
|
def type
|
|
107
90
|
:inline
|
|
108
91
|
end
|
|
92
|
+
|
|
93
|
+
private
|
|
94
|
+
|
|
95
|
+
# Rails 8.1 added a newline to compiled ERB output (rails/rails#53731).
|
|
96
|
+
# Subtract 1 to compensate for correct line numbers in stack traces.
|
|
97
|
+
# Inline templates start at line 2+ (defined inside a class), so this
|
|
98
|
+
# won't result in negative line numbers that cause segfaults with coverage.
|
|
99
|
+
#
|
|
100
|
+
# Computed at compile time rather than at initialization because the
|
|
101
|
+
# annotation setting can change between the two.
|
|
102
|
+
def lineno
|
|
103
|
+
erb_newline_compensation_needed? ? super - 1 : super
|
|
104
|
+
end
|
|
109
105
|
end
|
|
110
106
|
|
|
111
107
|
class InlineCall < Template
|
|
@@ -196,6 +192,20 @@ module ViewComponent
|
|
|
196
192
|
|
|
197
193
|
attr_reader :lineno
|
|
198
194
|
|
|
195
|
+
def erb_newline_compensation_needed?
|
|
196
|
+
Rails::VERSION::MAJOR >= 8 && Rails::VERSION::MINOR > 0 &&
|
|
197
|
+
details.handler == :erb &&
|
|
198
|
+
annotated?
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
# The newline compensated for lives inside the `<!-- BEGIN ... -->`
|
|
202
|
+
# annotation, which Rails only prepends to HTML templates, and only when
|
|
203
|
+
# annotations are enabled. Compensating outside those conditions shifts
|
|
204
|
+
# backtraces by a line and, under coverage, strips real template source.
|
|
205
|
+
def annotated?
|
|
206
|
+
ActionView::Base.annotate_rendered_view_with_filenames && html?
|
|
207
|
+
end
|
|
208
|
+
|
|
199
209
|
def compiled_source
|
|
200
210
|
handler = details.handler_class
|
|
201
211
|
this_source = source
|
|
@@ -59,7 +59,11 @@ module ViewComponent
|
|
|
59
59
|
locale = options.delete(:locale) || ::I18n.locale
|
|
60
60
|
key = __vc_i18n_key(key, options.delete(:scope))
|
|
61
61
|
|
|
62
|
-
__vc_i18n_backend
|
|
62
|
+
if __vc_i18n_backend&.exists?(locale, key)
|
|
63
|
+
__vc_i18n_backend.translate(locale, key, options)
|
|
64
|
+
else
|
|
65
|
+
::I18n.translate(key, locale: locale, **options)
|
|
66
|
+
end
|
|
63
67
|
end
|
|
64
68
|
|
|
65
69
|
alias_method :t, :translate
|
|
@@ -102,17 +106,8 @@ module ViewComponent
|
|
|
102
106
|
|
|
103
107
|
__vc_html_escape_translation_options!(options) if as_html
|
|
104
108
|
|
|
105
|
-
if
|
|
106
|
-
translated =
|
|
107
|
-
catch(:exception) do
|
|
108
|
-
__vc_i18n_backend.translate(locale, key, options)
|
|
109
|
-
end
|
|
110
|
-
|
|
111
|
-
# Fallback to the global translations
|
|
112
|
-
if translated.is_a? ::I18n::MissingTranslation
|
|
113
|
-
return @view_context.translate(key, locale: locale, **options)
|
|
114
|
-
end
|
|
115
|
-
|
|
109
|
+
if __vc_i18n_backend.exists?(locale, key)
|
|
110
|
+
translated = __vc_i18n_backend.translate(locale, key, options)
|
|
116
111
|
translated = __vc_html_safe_translation(translated) if as_html
|
|
117
112
|
translated
|
|
118
113
|
else
|
data/lib/view_component.rb
CHANGED
|
@@ -9,11 +9,11 @@ module ViewComponent
|
|
|
9
9
|
|
|
10
10
|
autoload :Base
|
|
11
11
|
autoload :CacheDigest
|
|
12
|
+
autoload :Cacheable
|
|
12
13
|
autoload :Compiler
|
|
13
14
|
autoload :CompileCache
|
|
14
15
|
autoload :Config
|
|
15
16
|
autoload :Deprecation
|
|
16
|
-
autoload :ExperimentallyCacheable
|
|
17
17
|
autoload :InlineTemplate
|
|
18
18
|
autoload :Instrumentation
|
|
19
19
|
autoload :Preview
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: view_component
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 4.
|
|
4
|
+
version: 4.19.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- ViewComponent Team
|
|
@@ -37,20 +37,6 @@ dependencies:
|
|
|
37
37
|
- - ">="
|
|
38
38
|
- !ruby/object:Gem::Version
|
|
39
39
|
version: 7.1.0
|
|
40
|
-
- !ruby/object:Gem::Dependency
|
|
41
|
-
name: concurrent-ruby
|
|
42
|
-
requirement: !ruby/object:Gem::Requirement
|
|
43
|
-
requirements:
|
|
44
|
-
- - "~>"
|
|
45
|
-
- !ruby/object:Gem::Version
|
|
46
|
-
version: '1'
|
|
47
|
-
type: :runtime
|
|
48
|
-
prerelease: false
|
|
49
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
50
|
-
requirements:
|
|
51
|
-
- - "~>"
|
|
52
|
-
- !ruby/object:Gem::Version
|
|
53
|
-
version: '1'
|
|
54
40
|
executables: []
|
|
55
41
|
extensions: []
|
|
56
42
|
extra_rdoc_files: []
|
|
@@ -93,7 +79,9 @@ files:
|
|
|
93
79
|
- lib/view_component/base.rb
|
|
94
80
|
- lib/view_component/cache_digest.rb
|
|
95
81
|
- lib/view_component/cache_digest/dependency_tracking.rb
|
|
82
|
+
- lib/view_component/cache_digest/digestor_lookup.rb
|
|
96
83
|
- lib/view_component/cache_digest/resolver.rb
|
|
84
|
+
- lib/view_component/cacheable.rb
|
|
97
85
|
- lib/view_component/collection.rb
|
|
98
86
|
- lib/view_component/compile_cache.rb
|
|
99
87
|
- lib/view_component/compiler.rb
|
|
@@ -102,7 +90,6 @@ files:
|
|
|
102
90
|
- lib/view_component/deprecation.rb
|
|
103
91
|
- lib/view_component/engine.rb
|
|
104
92
|
- lib/view_component/errors.rb
|
|
105
|
-
- lib/view_component/experimentally_cacheable.rb
|
|
106
93
|
- lib/view_component/inline_template.rb
|
|
107
94
|
- lib/view_component/instrumentation.rb
|
|
108
95
|
- lib/view_component/preview.rb
|
|
@@ -142,7 +129,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
142
129
|
- !ruby/object:Gem::Version
|
|
143
130
|
version: '0'
|
|
144
131
|
requirements: []
|
|
145
|
-
rubygems_version: 4.0.
|
|
132
|
+
rubygems_version: 4.0.20
|
|
146
133
|
specification_version: 4
|
|
147
134
|
summary: A framework for building reusable, testable & encapsulated view components
|
|
148
135
|
in Ruby on Rails.
|