svelte-on-rails 19.2.2 → 19.2.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 64fab65b35eaba112765f7d99ae3026a75bc25bb907618ca15b8b5f9fbf6f86e
4
- data.tar.gz: 22e6c0907a069dab0f84f3a8391309bad6418cd0b996321bc1c2f9cdb9fd4ea4
3
+ metadata.gz: bdd155e6ec85bcd4e50bd36c110a259f3cbe4004331c947a486c976dcf36561f
4
+ data.tar.gz: c2a6b04173b9212f77c4262f26b27e2e0bf14b19f9ef5a109efeaf07690c23cb
5
5
  SHA512:
6
- metadata.gz: c5c22b14278bebb7818730221a3508f3baaa87a411ff65dcd144bcf2472a676206ac15b245556a20fbb3e54297b22b71043552c0052dbdf345bb9583b4ac82c8
7
- data.tar.gz: 306019a8aca724942b7460e4ba3d741eed67e211ee9dbb317768f06897940fc349c74bf2d764c791b16dd0eef682a59ab6afb1ce16e6ce005b2de685e7db3c29
6
+ metadata.gz: e78ba049e517ba2fcae3ae325a1d95a5153c1f20962d17d3a149c7b87133e2442123568ba6b985b6658116434906573133423c2c9bd0a73831092d97400be509
7
+ data.tar.gz: 83393c1c21f7f01aca3d1f071228171ab4b8ab289d7a68183cc5a17a765fa39295ff3629c39cd154b8c912ddc5e50cfc436dcd499abf3539f5b34c8b350bb75a
@@ -4,7 +4,7 @@ module SvelteOnRails
4
4
  module Lib
5
5
  class ViewHelperSupport
6
6
 
7
- UNCACHED_PROPS_PLACEHOLDER = "**UNCACHED_PROPS_PLACEHOLDER_VDKVUKCYPTHQ4R6Z4EJ**"
7
+ UNCACHED_PROPS_PLACEHOLDER = "**Uncached Props are available only after hydration**"
8
8
  RENDER_OPTIONS = { ssr: [true, false, :auto], hydrate: :bool, debug: :bool, cache_key: :string_array, expires_in: :integer, cached: :bool }
9
9
  attr_reader :conf
10
10
 
@@ -22,11 +22,11 @@ module SvelteOnRails
22
22
  @component_name = component
23
23
  @view_dir = view_dir
24
24
  @request = request
25
- @props = if props.key?(:_uncached)
26
- props.merge(_uncached: UNCACHED_PROPS_PLACEHOLDER)
27
- else
28
- props
29
- end
25
+ @cached_props = if props.key?(:_uncached)
26
+ props.merge(_uncached: UNCACHED_PROPS_PLACEHOLDER)
27
+ else
28
+ props
29
+ end
30
30
  @html_options = html_options
31
31
 
32
32
  @ssr = resolve_ssr?(request)
@@ -157,6 +157,31 @@ module SvelteOnRails
157
157
  Rails.logger.info " [SOR] #{component_paths[:name]}.svelte #{message} (#{ms.round(1)}ms)"
158
158
  end
159
159
 
160
+ def render_svelte(current_template)
161
+ caller_loc = Proc.new do
162
+ v_path = current_template.identifier
163
+ caller_locations.find { |loc| loc.to_s.include?(v_path) }.to_s.match(/^[\s\S]+(:[0-9]+(?=:in))/).to_s
164
+ end
165
+
166
+ r = if SvelteOnRails::SsrServer.instance.alive?
167
+ SvelteOnRails::SsrServer.instance.render(
168
+ component_paths,
169
+ cached_props,
170
+ debug?,
171
+ caller_loc
172
+ )
173
+ else
174
+ SvelteOnRails::Lib::FallbackRenderer.render(component_paths, cached_props, debug?)
175
+ end
176
+ if r[:success]
177
+ set_request_metrics(:rendered)
178
+ else
179
+ @skip_caching = true
180
+ end
181
+
182
+ r
183
+ end
184
+
160
185
  def render_cached(view_context, &block)
161
186
 
162
187
  # increase expired time
@@ -166,7 +191,7 @@ module SvelteOnRails
166
191
  # render
167
192
 
168
193
  res = if @cached_content
169
- msg = "returned from cache#{", with uncached props" if @props.key?(:_uncached)}"
194
+ msg = "returned from cache#{", with uncached props" if @cached_props.key?(:_uncached)}"
170
195
  @cached_content.html_safe
171
196
  else
172
197
  msg = "Rendered"
@@ -208,8 +233,8 @@ module SvelteOnRails
208
233
 
209
234
  end
210
235
 
211
- def props
212
- @props ||= {}
236
+ def cached_props
237
+ @cached_props ||= {}
213
238
  end
214
239
 
215
240
  private
@@ -300,10 +325,9 @@ module SvelteOnRails
300
325
  @cache_key = begin
301
326
  fingerprint = (@conf.watch_changes? ? @conf.fingerprint(component_paths[:path], debug?, component_paths[:name]) : '')
302
327
 
303
- cache_key_raw = @view_dir + @component_name +
304
- @props.to_s + @options.to_s + @html_options.to_s +
305
- @options[:cache_key].to_s +
306
- fingerprint
328
+ cache_key_raw = @view_dir + @component_name + @options.to_s +
329
+ @cached_props.to_s + fingerprint + @options[:cache_key].to_s +
330
+ (@cached_props.key?(:_uncached) ? "" : @html_options.to_s)
307
331
 
308
332
  "#{@conf.redis_namespace}:#{Digest::XXH64.hexdigest(cache_key_raw)}"
309
333
  end
@@ -30,24 +30,52 @@ module SvelteOnRails
30
30
  )
31
31
 
32
32
  if support.ssr?
33
- res = if config.watch_changes? && !config.build_status['passed']
34
- sor_error_tag(support, wrapper_html, component_props, 'server-side-svelte-build-error', 'SVELTE BUILD FAILED')
35
- elsif caching
36
- support.set_request_metrics(:from_cache)
37
- support.render_cached(self) do
38
- render_component(support, wrapper_html, component_props)
33
+ if config.watch_changes? && !config.build_status['passed']
34
+ sor_error_tag(support, wrapper_html, component_props, 'server-side-svelte-build-error', 'SVELTE BUILD FAILED')
35
+ elsif caching
36
+ support.set_request_metrics(:from_cache)
37
+ if component_props.key?(:_uncached)
38
+
39
+ # when we have uncached props, we do not cache the wrapper tag
40
+
41
+ attr = support.tag_attributes(wrapper_html, component_props)
42
+ content_tag(:div, attr) do
43
+ support.render_cached(self) do
44
+ res = support.render_svelte(@current_template)
45
+ if res[:success]
46
+ concat(res[:head].html_safe)
47
+ concat(res[:html].html_safe)
48
+ else
49
+ sor_error_tag(support, wrapper_html, component_props, 'server-side-svelte-render-error', 'SVELTE RENDER FAILED')
50
+ end
51
+ end
52
+ end
53
+
54
+ else
55
+
56
+ # when all props are cached, we can cache the wrapper tag too
57
+ # ... but using only cached props for making sure we don't cache any dynamic content
58
+
59
+ support.render_cached(self) do
60
+ res = support.render_svelte(@current_template)
61
+ if res[:success]
62
+ attr = support.tag_attributes(wrapper_html, support.cached_props)
63
+ content_tag(:div, attr) do
64
+ concat(res[:head].html_safe)
65
+ concat(res[:html].html_safe)
39
66
  end
40
67
  else
41
- r = render_component(support, wrapper_html, component_props)
42
- support.log_completed("Rendered #{'as empty element that will be mounted on the client side' unless support.ssr?}")
43
- r
68
+ sor_error_tag(support, wrapper_html, component_props, 'server-side-svelte-render-error', 'SVELTE RENDER FAILED')
44
69
  end
45
- if component_props.key?(:_uncached)
46
- u_prp = CGI.escapeHTML(component_props[:_uncached].to_json)
47
- res.sub(""**UNCACHED_PROPS_PLACEHOLDER_VDKVUKCYPTHQ4R6Z4EJ**"", u_prp).html_safe
70
+ end
71
+
72
+ end
48
73
  else
49
- res
74
+ r = render_component(support, wrapper_html, component_props)
75
+ support.log_completed("Rendered #{'as empty element that will be mounted on the client side' unless support.ssr?}")
76
+ r
50
77
  end
78
+
51
79
  else
52
80
  render_empty_tag(support, wrapper_html, component_props)
53
81
  end
@@ -57,41 +85,34 @@ module SvelteOnRails
57
85
 
58
86
  def render_component(support, html_options, component_props)
59
87
 
60
- if support.ssr?
61
-
62
- caller_loc = Proc.new do
63
- v_path = @current_template.identifier
64
- caller_locations.find { |loc| loc.to_s.include?(v_path) }.to_s.match(/^[\s\S]+(:[0-9]+(?=:in))/).to_s
65
- end
88
+ caller_loc = Proc.new do
89
+ v_path = @current_template.identifier
90
+ caller_locations.find { |loc| loc.to_s.include?(v_path) }.to_s.match(/^[\s\S]+(:[0-9]+(?=:in))/).to_s
91
+ end
66
92
 
67
- ssr_result = if SvelteOnRails::SsrServer.instance.alive?
68
- SvelteOnRails::SsrServer.instance.render(
69
- support.component_paths,
70
- support.props,
71
- support.debug?,
72
- caller_loc
73
- )
74
- else
75
- SvelteOnRails::Lib::FallbackRenderer.render(support.component_paths, support.props, support.debug?)
76
- end
77
-
78
- if ssr_result[:success]
79
- support.set_request_metrics(:rendered)
80
- attr = support.tag_attributes(html_options, support.props)
81
- content_tag(:div, attr) do
82
- concat(ssr_result[:head].html_safe)
83
- concat(ssr_result[:html].html_safe)
84
- end
85
- else
86
- support.instance_variable_set(:@skip_caching, true)
87
- sor_error_tag(support, html_options, component_props, 'server-side-svelte-render-error', 'SVELTE RENDER FAILED')
93
+ ssr_result = if SvelteOnRails::SsrServer.instance.alive?
94
+ SvelteOnRails::SsrServer.instance.render(
95
+ support.component_paths,
96
+ support.cached_props,
97
+ support.debug?,
98
+ caller_loc
99
+ )
100
+ else
101
+ SvelteOnRails::Lib::FallbackRenderer.render(support.component_paths, support.cached_props, support.debug?)
102
+ end
103
+
104
+ if ssr_result[:success]
105
+ support.set_request_metrics(:rendered)
106
+ attr = support.tag_attributes(html_options, component_props)
107
+ content_tag(:div, attr) do
108
+ concat(ssr_result[:head].html_safe)
109
+ concat(ssr_result[:html].html_safe)
88
110
  end
89
-
90
111
  else
91
-
92
- render_empty_tag(support, html_options, component_props)
93
-
112
+ support.instance_variable_set(:@skip_caching, true)
113
+ sor_error_tag(support, html_options, component_props, 'server-side-svelte-render-error', 'SVELTE RENDER FAILED')
94
114
  end
115
+
95
116
  end
96
117
 
97
118
  def render_empty_tag(support, html_options, component_props)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: svelte-on-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 19.2.2
4
+ version: 19.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christian Sedlmair