svelte-on-rails 0.0.4 → 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: 63fd8216a8e992b4357b94950c46cdba9d107b64d1b3d3d6b15a1b7c7f3e92ea
4
- data.tar.gz: 034fc6c768eb1645acd934374df229f3eca5c9484fd32af8260be3118214225e
3
+ metadata.gz: 652cbedb8f5796094fde7e81e8e548de0065d33efcc9ccedac89772626b6c937
4
+ data.tar.gz: 324c08b71751b554662183e190dfe690cbe8fda05146a91c4fe42875956ce367
5
5
  SHA512:
6
- metadata.gz: ac2920e177aa42e613519a5a06617c475186a97b838526df66857b7048a365107c8121c6a00d7c52ae6c1d85042b84712ca7add36d0a06d1c0ca19b933d51afc
7
- data.tar.gz: a0cec2eda153ab725622906c0242c1563a626dd1b27e36d7cc5d7ee911dd41f14ac84f782c9e004bb6c86ff3b3b27c4aac2c92f64c06ce22bcad1793c8f55355
6
+ metadata.gz: be5231d79d34ed7ce9b5086c653b6f16d85bddffbf0c9ebf4fe7f8a8beb3e2841d26c9f86950aa4b17855b6530c68554eb696fd2e259c818f0505a4f86591eea
7
+ data.tar.gz: 4c428ca838a17cfe7609a0734b6419a4fb12feda2fed52aadbf72639bfa1f41eb0abf1cc2fa346a2456fc3f9a8af671f1696f5ccde045cab4a2ddbe58ae348b0
data/README.md CHANGED
@@ -1,23 +1,26 @@
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
- Server-side rendering is the bottleneck in this pipeline. Although rollup is mighty,
9
- this gem is in a early state and there sure are limitations.
10
- So, please always test your components early if they pass ssr.
10
+ Server-side rendering is the bottleneck on such a pipeline.
11
+ Although rollup is powerful, this gem is in an early state
12
+ and there are certainly limitations.
13
+ So please always test your components early if they pass ssr.
11
14
 
12
15
  I have written everything in ESM syntax.
13
- Although the common-js plugin is installed, I did not test it,
14
- so `require` for example may not work in Svelte components.
15
- To check which import statements work, please check the components in the
16
- `spec/rails-vite-test-app/app/frontend/javascript/components` gem.
16
+ Although the common-js plugin is installed, I have not tested it,
17
+ so for example `require` may not work in Svelte components.
18
+ To check which import statements work, please check the components
19
+ in the `spec/rails-vite-test-app/app/frontend/javascript/components` gem.
17
20
 
18
21
  This all is developed on Rails-7 together with `vite_rails`.
19
22
 
20
- 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)
21
24
  and [ElMassimo](https://github.com/ElMassimo) because they all helped me with theyr gems.
22
25
 
23
26
  ## Requirements
@@ -40,9 +43,9 @@ and run
40
43
  $ rails svelte_on_rails:install
41
44
  ```
42
45
 
43
- this creates a config file, please check the comments there.
46
+ This will create a little config file, please read the comments there.
44
47
 
45
- setup the npm module [@csedl/svelte-on-rails](https://www.npmjs.com/package/@csedl/svelte-on-rails) on the client side
48
+ Set up the npm module [@csedl/svelte-on-rails](https://www.npmjs.com/package/@csedl/svelte-on-rails) on the client side.
46
49
 
47
50
  ## Usage
48
51
 
@@ -50,6 +53,27 @@ setup the npm module [@csedl/svelte-on-rails](https://www.npmjs.com/package/@cse
50
53
  = svelte_component("myComponent", items: ['item1', 'item2'])
51
54
  ```
52
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
+
53
77
  ### Import statements
54
78
 
55
79
  For check which statements are working and actively tested, please check the
@@ -62,26 +86,25 @@ Among others there are
62
86
  - `import { someFunction } from '../customJavascript.js';`
63
87
  - `import Child from './Child.svelte';`
64
88
 
65
- ## Option render_server_side: :auto
89
+ ## Option `render_server_side: :auto`
66
90
 
67
91
  Works with `hotwired/turbo` only
68
92
 
69
93
  By passing the `render_server_side: :auto` option to the view helper,
70
- it checks if the request is an initial request (request header `X-Turbo-Request-ID` is not present),
71
- then does nothing server-side but 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):
72
95
 
73
- ```haml
74
- = svelte_component("HelloWorld", message: "Property from the server", render_server_side: :auto, class: "box")
75
- #=> output on the rails-console:
76
- Added Svelte-Tag for custom element: «<hello-world-svelte class='box svelte-component'></hello-world>»
77
- ```
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.
78
101
 
79
- 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.
80
103
 
81
- This works perfectly with hotwired/turbo because the custom elements are built on the
82
- first page load and survive page visits from turbo. If you then visit a page that
83
- has the `%hello-world` tag, the component will appear immediately.
84
- 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.
85
108
 
86
109
  ## Styles
87
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.4
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
@@ -1415,7 +1415,5 @@ requirements: []
1415
1415
  rubygems_version: 3.1.6
1416
1416
  signing_key:
1417
1417
  specification_version: 4
1418
- summary: Seamless and robust Svelte in Rails integration. Can render server-side,
1419
- always or only on first request, and handle subsequent turbo requests as custom
1420
- elements without shadow-dom. Server-side compilation via rollup.
1418
+ summary: Seamlessly integrate Svelte Components into Rails views.
1421
1419
  test_files: []