svelte-on-rails 7.1.3 → 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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b2251e235c9c3ef4744e197e276e77ada5f0530baaf3b17ecbf9fc09a130cf98
|
4
|
+
data.tar.gz: b4359024b0fa5ce9ebf02b2384b1ae7931cbb4982670a2a3a000c8b2cdd20d42
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ab0dd3ba7377a4e485840bdddca3425ecacec267d98a37c74224450d2acffd34265dbb6021eaa443ced8e92205b70b4bfac6bc6e42d060670868730d81b7d586
|
7
|
+
data.tar.gz: c665f45bbe57aab35cec4601dff88160a6818ad4f7055b8163d26b470feec82c76f75c024779913fc96bfe67aa4f6cfce451f22f78165b051379adf3b6d9dcd3
|
@@ -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
|
-
|
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
|
|
@@ -2,80 +2,47 @@
|
|
2
2
|
module SvelteOnRails
|
3
3
|
module ViewHelpers
|
4
4
|
|
5
|
-
def
|
5
|
+
def svelte_component_ruby2(path, *args)
|
6
6
|
|
7
|
-
|
7
|
+
props, html, options = validate_ruby2_props(args)
|
8
8
|
|
9
|
-
support = SvelteOnRails::Lib::ViewHelperSupport.new(path,
|
9
|
+
support = SvelteOnRails::Lib::ViewHelperSupport.new(path, props, html, options, request, false)
|
10
10
|
|
11
|
-
support.
|
12
|
-
|
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
|
24
|
-
|
25
|
-
prp = validate_props(props, _props)
|
17
|
+
def cached_svelte_component_ruby2(path, *args)
|
26
18
|
|
27
|
-
|
19
|
+
props, html, options = validate_ruby2_props(args)
|
28
20
|
|
29
|
-
|
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
|
-
|
34
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
73
|
-
r
|
74
|
-
end
|
39
|
+
def cached_svelte_component(path, props = {}, html: {}, options: {})
|
75
40
|
|
76
|
-
support.
|
41
|
+
support = SvelteOnRails::Lib::ViewHelperSupport.new(path, props, html, options, request, true)
|
77
42
|
|
78
|
-
|
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
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
if
|
106
|
-
|
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
|
-
|
109
|
-
|
110
|
-
|
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
|