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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e44ccc4689a527118303fecadbf493f88f3ac7c697aa11a9d63d7b481df0631c
4
- data.tar.gz: e4db1b64136757c45a80a080c43e6624d9ad45a3069398a57056c7826d2ec7e7
3
+ metadata.gz: 652cbedb8f5796094fde7e81e8e548de0065d33efcc9ccedac89772626b6c937
4
+ data.tar.gz: 324c08b71751b554662183e190dfe690cbe8fda05146a91c4fe42875956ce367
5
5
  SHA512:
6
- metadata.gz: a8ee735aff772931a5f7fd44d61ff4bf57b997b3264399ad50a227fccde6bc38d468b5c35691f0ca29a9074faf4b299618d760b7e72b232bba2b795e1ae2e333
7
- data.tar.gz: 966685f0dfab7e6ced316b7f2ebfa51a589b0c4881ad23e4364edfad1637e2188a565bf5973a169167c7cd287e82c0c102b4c4edc6cb65cc2ef44bbf35fe86d1
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
- Can render server-side, always or only on first request, and
5
- handle subsequent turbo requests as custom elements without shadow-dom.
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 [chrisward](https://www.npmjs.com/~crisward), [shakacode](https://github.com/shakacode/react_on_rails)
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 not present),
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
- ```haml
75
- = svelte_component("HelloWorld", message: "Property from the server", render_server_side: :auto, class: "box")
76
- #=> output on the rails-console:
77
- Added Svelte-Tag for custom element: «<hello-world-svelte class='box svelte-component'></hello-world>»
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
- If configured on the npm module, it instaniates a custom element and adopts it to the element.
102
+ More details to this point you can find on the npm package.
81
103
 
82
- This works perfectly with hotwired/turbo because the custom elements are
83
- built on the first page load and survive page visits from turbo.
84
- If you then visit a page that has the `%hello-world` tag, the component will appear immediately.
85
- This is not a default setting as it requires more configuration.
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
- unless filename.match(/[a-z|A-Z][a-z|A-Z|0-9]+$/)
7
- raise "FileName/TagName must be in camelCase format and without .svelte suffix. It must not start with a number."
8
- end
9
-
10
- render_server_side = if props.key?(:render_server_side)
11
- props.delete(:render_server_side)
12
- else
13
- SvelteOnRails::Configuration.instance.render_server_side
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
- html_class = props.delete(:class).to_s + " svelte-component"
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
- # render
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 == true || (render_server_side == :auto && is_initial_request)
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 + '.svelte')
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
- data = { props: props }
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 for custom-element
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
- tag.html_safe
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.5
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-15 00:00:00.000000000 Z
11
+ date: 2025-04-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties