svelte-on-rails 7.1.2 → 8.0.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 484b04267cd80435b0f5ea0f561e0b5a4658115ecffadbf283221b9809022985
4
- data.tar.gz: ac2cca41825693eb678f25007528a87b22ffedb23dd744e5c573730451346860
3
+ metadata.gz: b2251e235c9c3ef4744e197e276e77ada5f0530baaf3b17ecbf9fc09a130cf98
4
+ data.tar.gz: b4359024b0fa5ce9ebf02b2384b1ae7931cbb4982670a2a3a000c8b2cdd20d42
5
5
  SHA512:
6
- metadata.gz: be660994114829e76e6d2ef685ed718d499807bab18f955a7471facb688ec9647762b20c1d008a9e68153da95463b739951efb2f0aef05e1a2e563e85b85891b
7
- data.tar.gz: 0421f4c6a5cc2e8812fe8722501631f399cbebb13c735580008ea902dd420bbb504df2d7f34a91ca98c427823b95972e121a83449749445e5d5886523ae802c9
6
+ metadata.gz: ab0dd3ba7377a4e485840bdddca3425ecacec267d98a37c74224450d2acffd34265dbb6021eaa443ced8e92205b70b4bfac6bc6e42d060670868730d81b7d586
7
+ data.tar.gz: c665f45bbe57aab35cec4601dff88160a6818ad4f7055b8163d26b470feec82c76f75c024779913fc96bfe67aa4f6cfce451f22f78165b051379adf3b6d9dcd3
data/README.md CHANGED
@@ -11,7 +11,7 @@ Realizing [DHH's vision](https://rubyonrails.org/2021/12/15/Rails-7-fulfilling-a
11
11
 
12
12
  Svelte offers the simplest and most elegant soulution to building reactive, high-performance front-end components.
13
13
 
14
- See: [Comparitions on the guide](https://svelte-on-rails-docs-51acfa.gitlab.io/about/why.html)
14
+ See: [Comparitions on the guide](https://svelte-on-rails.dev/about/why.html)
15
15
 
16
16
  # Features
17
17
 
@@ -93,9 +93,9 @@ And you will see "Svelte Hello World" on the browser! 👍 🤗
93
93
 
94
94
  # Contributors welcome
95
95
 
96
- see [Guide / run your first test](https://svelte-on-rails-docs-51acfa.gitlab.io/first_test.html)
96
+ see [Guide / run your first test](https://svelte-on-rails.dev/first_test.html)
97
97
 
98
98
 
99
99
  ## Licence
100
100
 
101
- Copyright © 2025-2028 sedlmair.ch. Distributed by [MIT License](https://svelte-on-rails-docs-51acfa.gitlab.io/license.html)
101
+ Copyright © 2025-2028 sedlmair.ch. Distributed by [MIT License](https://svelte-on-rails.dev/license.html)
@@ -44,7 +44,7 @@ module SvelteOnRails
44
44
  end
45
45
 
46
46
  def redis_cache_store
47
- (@configs[:redis_cache_store] || {}).with_indifferent_access
47
+ (@configs[:redis_cache_store] || {}).transform_keys(&:to_sym)
48
48
  end
49
49
 
50
50
  def redis_instance
@@ -3,6 +3,8 @@ module SvelteOnRails
3
3
 
4
4
  module Lib
5
5
  class ViewHelperSupport
6
+
7
+ RENDER_OPTIONS = %i[ssr hydrate debug cache_key expires_in]
6
8
  attr_reader :filename, :options, :html_options, :request, :conf
7
9
 
8
10
  def initialize(file, props, html_options, options, request, caching = false)
@@ -20,7 +22,7 @@ module SvelteOnRails
20
22
  @options, @html_options = prepare_options(
21
23
  options,
22
24
  html_options,
23
- %i[ssr hydrate debug cache_key expires_in]
25
+ RENDER_OPTIONS
24
26
  )
25
27
  @request = request
26
28
  @ssr = determine_ssr
@@ -110,6 +112,70 @@ module SvelteOnRails
110
112
  end
111
113
  end
112
114
 
115
+ def render_cached(view_context, &block)
116
+
117
+ cached_content = conf.redis_instance.get(cache_key)
118
+
119
+ # debug
120
+
121
+ if debug?
122
+
123
+ debug_log("Rendering component: «#{filename}», cache_key: «#{custom_cache_key}»")
124
+ debug_log("Redis configuration: #{conf.redis_cache_store}")
125
+ ttl = conf.redis_instance.ttl(cache_key)
126
+
127
+ key_stat = if cached_content.present?
128
+ 'has content'
129
+ elsif conf.redis_instance.exists(cache_key)
130
+ 'exists but no content'
131
+ else
132
+ 'not exists'
133
+ end
134
+
135
+ ttl_stat = if conf.redis_instance.exists(cache_key)
136
+ ", ttl was: #{ttl} seconds, now set to: #{redis_expiration_seconds} seconds"
137
+ end
138
+
139
+ debug_log("Cache key: «#{cache_key}» (#{key_stat}#{ttl_stat})")
140
+
141
+ end
142
+
143
+ # increase expired time
144
+
145
+ conf.redis_instance.expire(cache_key, redis_expiration_seconds)
146
+
147
+ # render
148
+
149
+ log_message = '?'
150
+ res = if cached_content
151
+ log_message = "Returned #{filename}.svelte from cache"
152
+ cached_content.html_safe
153
+ else
154
+ log_message = "Rendered #{filename}.svelte and stored to cache"
155
+ debug_log("cache recalculating for key: #{cache_key}")
156
+ r = view_context.instance_eval(&block)
157
+ debug_log("cache recalculated")
158
+
159
+ conf.redis_instance.set(cache_key, r)
160
+ r
161
+ end
162
+ log_rendering(log_message)
163
+
164
+ res
165
+
166
+ end
167
+
168
+ def render(view_context, &block)
169
+
170
+ debug_log("Rendering component: #{filename}")
171
+
172
+ r = view_context.instance_eval(&block)
173
+
174
+ log_rendering("Rendered #{filename}.svelte #{'as empty element that will be mounted on the client side' unless ssr?}")
175
+
176
+ r
177
+ end
178
+
113
179
  private
114
180
 
115
181
  def determine_ssr
@@ -135,7 +201,7 @@ module SvelteOnRails
135
201
  ].compact.join('-')
136
202
 
137
203
  @cache_key_primary = [
138
- conf.redis_cache_store[:namespace] ? conf.redis_cache_store[:namespace] : "svelte-on-rails:#{Rails.env}",
204
+ conf.redis_cache_store[:namespace] ? conf.redis_cache_store[:namespace] : "svelte-on-rails:#{Rails.env rescue 'unknown'}",
139
205
  filename_part,
140
206
  ].join(':')
141
207
 
@@ -31,36 +31,32 @@ module SvelteOnRails
31
31
  cnf.rails_root
32
32
  ].join(' ')
33
33
 
34
- Dir.chdir(cnf.rails_root) do
35
- stdout, stderr, status = Open3.capture3(cmd, stdin_data: props.to_json)
34
+ stdout, stderr, status = Open3.capture3(cmd, stdin_data: props.to_json, chdir: cnf.rails_root)
36
35
 
37
- ary = stdout.split('[svelte-on-rails:successful-json-response]')
36
+ ary = stdout.split('[svelte-on-rails:successful-json-response]')
38
37
 
39
- unless ary.length == 2
40
- raise "[svelte-on-rails] render ERROR for component: #{@component_files[:svelte_filename]}\n\ncommand:\n+++\n#{cmd}\n+++\n\nstdout:\n+++\n#{stdout}+++\n\n\nstderr:\n+++\n#{stderr}+++"
41
- end
42
-
43
-
44
- begin
38
+ unless ary.length == 2
39
+ raise "[svelte-on-rails] render ERROR for component: #{@component_files[:svelte_filename]}\n\ncommand:\n+++\n#{cmd}\n+++\n\nstdout:\n+++\n#{stdout}+++\n\n\nstderr:\n+++\n#{stderr}+++"
40
+ end
45
41
 
42
+ begin
46
43
 
47
- res = JSON.parse(ary[1])
48
- css_file = @component_files[:compiled_file] + '.css'
49
- if File.exist?(css_file)
50
- res['css'] = File.read(css_file)
51
- end
44
+ res = JSON.parse(ary[1])
45
+ css_file = @component_files[:compiled_file] + '.css'
46
+ if File.exist?(css_file)
47
+ res['css'] = File.read(css_file)
48
+ end
52
49
 
53
- unless status.to_s.match(/^pid [0-9]+ exit 0$/)
54
- cmp = "#{@component_files[:svelte_filename]} was returned «#{status.to_s}»\n\n"
55
- msg = "#{cmp}output from render.js (stderr) =>\n+++\n" + stderr + "+++\n\nRender Svelte Server-side =>\n#{cmd}\n\n"
56
- utils.puts_warning(msg)
57
- end
50
+ unless status.to_s.match(/^pid [0-9]+ exit 0$/)
51
+ cmp = "#{@component_files[:svelte_filename]} was returned «#{status.to_s}»\n\n"
52
+ msg = "#{cmp}output from render.js (stderr) =>\n+++\n" + stderr + "+++\n\nRender Svelte Server-side =>\n#{cmd}\n\n"
53
+ utils.puts_warning(msg)
54
+ end
58
55
 
59
- return res
60
- rescue JSON::ParserError => e
56
+ return res
61
57
 
62
- raise "[svelte-on-rails] render ERROR Svelte Server-side for component: #{@component_files[:svelte_filename]}\n\nError message:\n+++\n#{e.message}\n+++\n\nstdout:\n+++\n#{stdout}+++\n\n\nstderr:\n+++\n#{stderr}+++"
63
- end
58
+ rescue => e
59
+ raise "MY_ERR => #{e}\n\n+++\n#{e.backtrace.join("\n")}\n+++"
64
60
  end
65
61
  end
66
62
 
@@ -2,80 +2,47 @@
2
2
  module SvelteOnRails
3
3
  module ViewHelpers
4
4
 
5
- def svelte_component(path, props = {}, html: {}, options: {}, _props: {})
5
+ def svelte_component_ruby2(path, *args)
6
6
 
7
- prp = validate_props(props, _props)
7
+ props, html, options = validate_ruby2_props(args)
8
8
 
9
- support = SvelteOnRails::Lib::ViewHelperSupport.new(path, prp, html, options, request, false)
9
+ support = SvelteOnRails::Lib::ViewHelperSupport.new(path, props, html, options, request, false)
10
10
 
11
- support.debug_log("Rendering component: #{path}")
12
- log_message = '?'
13
-
14
- log_message = "Rendered #{support.filename}.svelte #{'as empty element that will be mounted on the client side' unless support.ssr?}"
15
- res = render_component(support)
16
-
17
- support.log_rendering(log_message)
18
-
19
- res
11
+ support.render(self) do
12
+ render_component(support)
13
+ end
20
14
 
21
15
  end
22
16
 
23
- def cached_svelte_component(path, props = {}, html: {}, options: {}, _props: {})
24
-
25
- prp = validate_props(props, _props)
17
+ def cached_svelte_component_ruby2(path, *args)
26
18
 
27
- support = SvelteOnRails::Lib::ViewHelperSupport.new(path, prp, html, options, request, true)
19
+ props, html, options = validate_ruby2_props(args)
28
20
 
29
- log_message = '?'
30
- redis = support.conf.redis_instance
31
- cached_content = redis.get(support.cache_key)
21
+ support = SvelteOnRails::Lib::ViewHelperSupport.new(path, props, html, options, request, true)
32
22
 
33
- # TTL
34
- if support.debug?
35
-
36
- support.debug_log("Rendering component: «#{filename}», cache_key: «#{support.custom_cache_key}»")
37
- support.debug_log("Redis configuration: #{support.conf.redis_cache_store}")
38
- ttl = redis.ttl(support.cache_key)
23
+ support.render_cached(self) do
24
+ render_component(support)
25
+ end
39
26
 
40
- key_stat = if cached_content.present?
41
- 'has content'
42
- elsif redis.exists(support.cache_key)
43
- 'exists but no content'
44
- else
45
- 'not exists'
46
- end
27
+ end
47
28
 
48
- ttl_stat = if redis.exists(support.cache_key)
49
- ", ttl was: #{ttl} seconds, now set to: #{support.redis_expiration_seconds} seconds"
50
- end
29
+ def svelte_component(path, props = {}, html: {}, options: {})
51
30
 
52
- support.debug_log("Cache key: «#{support.cache_key}» (#{key_stat}#{ttl_stat})")
31
+ support = SvelteOnRails::Lib::ViewHelperSupport.new(path, props, html, options, request, false)
53
32
 
33
+ support.render(self) do
34
+ render_component(support)
54
35
  end
55
36
 
56
- redis.expire(support.cache_key, support.redis_expiration_seconds)
57
-
58
- res = if cached_content
59
- log_message = "Returned #{support.filename}.svelte from cache"
60
- cached_content.html_safe
61
- else
62
- log_message = "Rendered #{support.filename}.svelte and stored to cache"
63
- support.debug_log("cache recalculating for key: #{support.cache_key}")
64
- r = render_component(support)
65
- support.debug_log("cache recalculated")
66
-
67
- # redis.scan_each(match: "#{support.cache_key_primary}:*") do |key|
68
- # redis.del(key)
69
- # support.debug_log("deleted cache, pattern: «#{support.cache_key_primary}:*», key: #{key.sub("#{support.cache_key_primary}:", '[..]')}")
70
- # end
37
+ end
71
38
 
72
- redis.set(support.cache_key, r)
73
- r
74
- end
39
+ def cached_svelte_component(path, props = {}, html: {}, options: {})
75
40
 
76
- support.log_rendering(log_message)
41
+ support = SvelteOnRails::Lib::ViewHelperSupport.new(path, props, html, options, request, true)
77
42
 
78
- res
43
+ support.render_cached(self) do
44
+ render_component(support)
45
+ end
79
46
 
80
47
  end
81
48
 
@@ -98,18 +65,32 @@ module SvelteOnRails
98
65
  end
99
66
  end
100
67
 
101
- def validate_props(props, _props)
102
- if props.present? && _props.present?
103
- raise "you can only pass props as the first argument OR use the _props keyword argument. the latter only is made as workaround for Apps <= ruby-3"
104
- end
105
- if RUBY_VERSION.split('.').first.to_i >= 3 && _props.present?
106
- raise "The _props keyword-argument is only meant as workaround for Apps <= ruby-3 because of avoiding misinterpreting hash as keyword arguments"
68
+ def validate_ruby2_props(args)
69
+ last_is_kwargs = false
70
+ kw_err = false
71
+
72
+ if args.length > 2
73
+ kw_err = true
74
+ elsif args.length >= 1
75
+ if args.last.keys & [:html, :options]
76
+ last_is_kwargs = true
77
+ if (args.last.keys - [:html, :options]).present?
78
+ kw_err = true
79
+ end
80
+ end
107
81
  end
108
- if props.present?
109
- props
110
- else
111
- _props
82
+
83
+ if kw_err
84
+ raise "Invalid arguments: First argument can be a hash as svelte-properties, second and third arguments are keyword arguments: :html and/or :options"
112
85
  end
86
+
87
+ first_is_props = args.length == 1 && !last_is_kwargs || args.length == 2
88
+
89
+ [
90
+ (first_is_props ? args.first : nil),
91
+ (last_is_kwargs ? args[1][:html] : nil),
92
+ (last_is_kwargs ? args[1][:options] : nil),
93
+ ]
113
94
  end
114
95
 
115
96
  end
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: 7.1.2
4
+ version: 8.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christian Sedlmair