Skip to content

Commit 57da235

Browse files
authored
Fix caching (#279)
1 parent 0a9c22f commit 57da235

File tree

6 files changed

+41
-35
lines changed

6 files changed

+41
-35
lines changed

Appraisals

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,3 @@ end
1111
appraise "rails edge" do
1212
gem "rails", github: "rails/rails"
1313
end
14-
15-
appraise "phlex 2.1" do
16-
gem "phlex", "~> 2.1.0"
17-
end

app/components/cache.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# frozen_string_literal: true
2+
3+
class Components::Cache < Phlex::HTML
4+
def initialize(text)
5+
@text = text
6+
end
7+
8+
def cache_store = Rails.cache
9+
10+
def view_template
11+
cache do
12+
h1 { @text }
13+
end
14+
end
15+
end

lib/phlex/rails/sgml.rb

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -136,27 +136,6 @@ def set_original_view_context(view_context)
136136
# no-op (see https://github.com/ViewComponent/view_component/issues/2207)
137137
end
138138

139-
def vanish(...)
140-
# Quick fix because we need rails to handle the capture now
141-
capture(...)
142-
end
143-
144-
def capture(*args, &block)
145-
if capture_context
146-
return "" unless block
147-
148-
@_state.around_capture do
149-
if args.length == 0
150-
capture_context.capture { __yield_content__(&block) }
151-
else
152-
capture_context.capture(*args) { __yield_content_with_args__(*args, &block) }
153-
end
154-
end
155-
else
156-
super
157-
end
158-
end
159-
160139
def enable_cache_reloading?
161140
Rails.env.development?
162141
end

lib/phlex/rails/sgml/state.rb

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,21 @@ def buffer
55
@user_context[:capture_context]&.output_buffer&.raw_buffer || super
66
end
77

8-
def around_capture
9-
original_capturing = @capturing
10-
original_fragments = @fragments
8+
def capture
9+
if (capture_context = @user_context[:capture_context])
10+
original_capturing = @capturing
11+
original_fragments = @fragments
1112

12-
begin
13-
@capturing = true
14-
yield
15-
ensure
16-
@capturing = original_capturing
17-
@fragments = original_fragments
13+
capture_context.capture do
14+
@capturing = true
15+
@fragments = nil
16+
yield
17+
ensure
18+
@capturing = original_capturing
19+
@fragments = original_fragments
20+
end
21+
else
22+
super
1823
end
1924
end
2025
end

phlex-rails.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,6 @@ Gem::Specification.new do |spec|
2828

2929
spec.require_paths = ["lib"]
3030

31-
spec.add_dependency "phlex", "~> 2.1.0"
31+
spec.add_dependency "phlex", "~> 2.1.2"
3232
spec.add_dependency "railties", ">= 7.1", "< 9"
3333
end

test/cache.test.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# frozen_string_literal: true
2+
3+
test "basic caching test" do
4+
assert_equal_html render(Components::Cache.new("Original")), <<~HTML.strip
5+
<h1>Original</h1>
6+
HTML
7+
8+
assert_equal_html render(Components::Cache.new("Something else")), <<~HTML.strip
9+
<h1>Original</h1>
10+
HTML
11+
end

0 commit comments

Comments
 (0)