svelte-on-rails 0.0.5 → 0.0.7
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/README.md +39 -17
- data/lib/svelte_on_rails/view_helpers.rb +27 -45
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 652cbedb8f5796094fde7e81e8e548de0065d33efcc9ccedac89772626b6c937
|
4
|
+
data.tar.gz: 324c08b71751b554662183e190dfe690cbe8fda05146a91c4fe42875956ce367
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: be5231d79d34ed7ce9b5086c653b6f16d85bddffbf0c9ebf4fe7f8a8beb3e2841d26c9f86950aa4b17855b6530c68554eb696fd2e259c818f0505a4f86591eea
|
7
|
+
data.tar.gz: 4c428ca838a17cfe7609a0734b6419a4fb12feda2fed52aadbf72639bfa1f41eb0abf1cc2fa346a2456fc3f9a8af671f1696f5ccde045cab4a2ddbe58ae348b0
|
data/README.md
CHANGED
@@ -1,8 +1,10 @@
|
|
1
1
|
# Svelte on rails
|
2
2
|
|
3
|
-
Seamless and robust Svelte in Rails integration.
|
4
|
-
|
5
|
-
|
3
|
+
Seamless and robust Svelte in Rails integration.
|
4
|
+
|
5
|
+
Can render server-side (SSR), always or only on first request, and handle
|
6
|
+
subsequent turbo requests by mounting svelte components
|
7
|
+
on an empty element for server-side performance optimisation.
|
6
8
|
Server-side compilation via rollup.
|
7
9
|
|
8
10
|
Server-side rendering is the bottleneck on such a pipeline.
|
@@ -18,7 +20,7 @@ in the `spec/rails-vite-test-app/app/frontend/javascript/components` gem.
|
|
18
20
|
|
19
21
|
This all is developed on Rails-7 together with `vite_rails`.
|
20
22
|
|
21
|
-
Thanks to [
|
23
|
+
Thanks to [shakacode](https://github.com/shakacode/react_on_rails)
|
22
24
|
and [ElMassimo](https://github.com/ElMassimo) because they all helped me with theyr gems.
|
23
25
|
|
24
26
|
## Requirements
|
@@ -51,6 +53,27 @@ Set up the npm module [@csedl/svelte-on-rails](https://www.npmjs.com/package/@cs
|
|
51
53
|
= svelte_component("myComponent", items: ['item1', 'item2'])
|
52
54
|
```
|
53
55
|
|
56
|
+
would render a component like `myComponent.svelte`
|
57
|
+
|
58
|
+
```sveltehtml
|
59
|
+
<script>
|
60
|
+
export let items
|
61
|
+
</script>
|
62
|
+
|
63
|
+
<ul>
|
64
|
+
{#each items as item}
|
65
|
+
<li>{item}</li>
|
66
|
+
{/each}
|
67
|
+
</ul>
|
68
|
+
|
69
|
+
<style>
|
70
|
+
ul {
|
71
|
+
list-style: none;
|
72
|
+
}
|
73
|
+
</style>
|
74
|
+
```
|
75
|
+
|
76
|
+
|
54
77
|
### Import statements
|
55
78
|
|
56
79
|
For check which statements are working and actively tested, please check the
|
@@ -63,26 +86,25 @@ Among others there are
|
|
63
86
|
- `import { someFunction } from '../customJavascript.js';`
|
64
87
|
- `import Child from './Child.svelte';`
|
65
88
|
|
66
|
-
## Option render_server_side: :auto
|
89
|
+
## Option `render_server_side: :auto`
|
67
90
|
|
68
91
|
Works with `hotwired/turbo` only
|
69
92
|
|
70
93
|
By passing the `render_server_side: :auto` option to the view helper,
|
71
|
-
it checks if the request is an initial request (request header `X-Turbo-Request-ID` is
|
72
|
-
then does nothing server-side except add a tag. Assuming the filename is `HelloWorld.svelte`:
|
94
|
+
it checks if the request is an initial request (request header `X-Turbo-Request-ID` is present):
|
73
95
|
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
96
|
+
On Initial-Request, it adds the attribute `data-svelte-on-rails-initialize-action="hydrate"` and
|
97
|
+
returns a server side rendered component that will be hydrated on the frontend by the npm package.
|
98
|
+
|
99
|
+
Otherwise it adds `mount` instead of hydrate, and renders a empty element, but provided with the
|
100
|
+
necessary attributes for the npm package.
|
79
101
|
|
80
|
-
|
102
|
+
More details to this point you can find on the npm package.
|
81
103
|
|
82
|
-
This works perfectly with hotwired/turbo because the
|
83
|
-
|
84
|
-
|
85
|
-
|
104
|
+
This works perfectly with hotwired/turbo because the javascript is only
|
105
|
+
loaded on very first load to the frontend, then the most work is done
|
106
|
+
in frontend and the server is relieved, except on initial request.
|
107
|
+
You will see no unpleasant «blink» on the page.
|
86
108
|
|
87
109
|
## Styles
|
88
110
|
|
@@ -3,80 +3,62 @@ module SvelteOnRails
|
|
3
3
|
|
4
4
|
def svelte_component(filename, props = {})
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
end
|
15
|
-
|
16
|
-
unless [true, false, :auto].include?(render_server_side)
|
6
|
+
# check what to do
|
7
|
+
|
8
|
+
rss = if props.key?(:render_server_side)
|
9
|
+
props.delete(:render_server_side)
|
10
|
+
else
|
11
|
+
SvelteOnRails::Configuration.instance.render_server_side
|
12
|
+
end
|
13
|
+
unless [true, false, :auto].include?(rss)
|
17
14
|
raise "Only true, false or auto are allowed for the argument #render_server_side"
|
18
15
|
end
|
16
|
+
render_server_side = (rss == :auto && request.headers['X-Turbo-Request-ID'].blank? || rss)
|
19
17
|
|
20
18
|
hydrate = if props.key?(:hydrate)
|
21
19
|
props[:hydrate]
|
22
20
|
else
|
23
21
|
true
|
24
22
|
end
|
25
|
-
props.delete(:hydrate)
|
26
23
|
|
27
|
-
|
28
|
-
|
29
|
-
is_initial_request = request.headers['X-Turbo-Request-ID'].blank?
|
24
|
+
# separate hashes
|
30
25
|
|
26
|
+
props.delete(:hydrate)
|
27
|
+
props.delete(:render_server_side)
|
28
|
+
options = props.slice(:class, :id, :style)
|
29
|
+
props.delete(:class)
|
30
|
+
props.delete(:id)
|
31
|
+
props.delete(:style)
|
31
32
|
|
33
|
+
# set up html
|
32
34
|
|
33
|
-
|
35
|
+
options[:class] = options[:class].to_s
|
36
|
+
options[:class] += ' svelte-component svelte-on-rails-not-initialized-component'
|
37
|
+
options[:data] ||= {}
|
38
|
+
options[:data][:props] = props.to_json
|
39
|
+
options[:data][:svelte_component] = filename
|
34
40
|
|
35
|
-
if render_server_side
|
41
|
+
if render_server_side
|
36
42
|
|
37
43
|
# render server side
|
38
44
|
|
39
45
|
start_time = Time.now
|
40
|
-
sv = SvelteOnRails::RenderServerSide.new(filename +
|
46
|
+
sv = SvelteOnRails::RenderServerSide.new(filename + '.svelte')
|
41
47
|
sv.compile_if_changes
|
42
48
|
res = sv.render_compiled_file(props)
|
43
49
|
time = Time.now - start_time
|
44
50
|
Rails.logger.info " Rendered #{filename}.svelte server-side: #{time.round(3)}ms"
|
45
51
|
|
46
|
-
|
47
|
-
|
48
|
-
data[:not_hydrated_svelte_component] = filename if hydrate
|
49
|
-
|
50
|
-
content_tag(:div, class: html_class, data: data) do
|
52
|
+
content_tag(:div, options) do
|
51
53
|
r = content_tag(:style, res['css'], type: 'text/css')
|
52
54
|
r << res['html'].html_safe
|
53
|
-
#res['html'].html_safe
|
54
55
|
end
|
55
56
|
|
56
57
|
else
|
57
58
|
|
58
|
-
# render
|
59
|
-
|
60
|
-
tag_name = filename.gsub('/','--').underscore.gsub('_', '-') + '-svelte'
|
61
|
-
|
62
|
-
prp = []
|
63
|
-
props.each do |k, v|
|
64
|
-
_v = if v.is_a?(Array) || v.is_a?(Hash)
|
65
|
-
v.to_json
|
66
|
-
else
|
67
|
-
v.to_s
|
68
|
-
end
|
69
|
-
prp.push("#{k}='#{_v}'")
|
70
|
-
end
|
71
|
-
if html_class
|
72
|
-
prp.push("class='#{html_class}'")
|
73
|
-
end
|
74
|
-
|
75
|
-
tag = "<#{tag_name} #{prp.join(' ')}></#{tag_name}>"
|
76
|
-
|
77
|
-
Rails.logger.info " Rendered Tag for Svelte custom element: «#{tag}»"
|
59
|
+
# render empty element
|
78
60
|
|
79
|
-
|
61
|
+
content_tag(:div, options) {}
|
80
62
|
|
81
63
|
end
|
82
64
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: svelte-on-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Christian Sedlmair
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2025-04-
|
11
|
+
date: 2025-04-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: railties
|