svelte-on-rails 3.0.6 → 3.1.1
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 +46 -7
- data/lib/generators/svelte_on_rails/install/install_generator.rb +1 -1
- data/lib/svelte_on_rails/configuration.rb +9 -0
- data/lib/svelte_on_rails/installer/npm.rb +52 -29
- data/lib/svelte_on_rails/installer/vite.rb +2 -2
- data/lib/svelte_on_rails/view_helpers.rb +6 -1
- data/templates/config_base/config/svelte_on_rails.yml +5 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a8c8933b55b56b0a2adcc4c06542b0a9078b8e36408ae6afc9316289f2f2f924
|
4
|
+
data.tar.gz: 777395f46db7c8e2c88df9090b646088e39cb10cf9c2881ff7cd2f7a78dc8065
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 803bb9b519cc14baa104b41689c56dbe168fae17a1ec540a07dedcb8acc1200418f231446fe6ccd611f84d1c0607ffa335a312bed905512b5a95e12592bb6805
|
7
|
+
data.tar.gz: 51d06bdd84b8272d5af59bd9bbc4efd407d39a9b57c343a8d96a68e504770a135a951e18defa50b18e95443c319194c33e31287d922d63e7af4520f35345b85a
|
data/README.md
CHANGED
@@ -25,6 +25,8 @@ On every app there are parts where you want it to shine. This is where Svelte co
|
|
25
25
|
- Super fast
|
26
26
|
- **Compared to Stimulus**
|
27
27
|
- No more writing double logic of the initial HTML state
|
28
|
+
- One Logic in one file!
|
29
|
+
- Render logic and script logic is in one file and, maybe, some css too.
|
28
30
|
- Stimulus is great for rails views with some javascript, but, complex parts: You will love svelte.
|
29
31
|
- **Compared to React**
|
30
32
|
- No more shadow dom and all those packages that are supposed to improve performance (e.g. useCallback...)
|
@@ -66,16 +68,19 @@ Thanks to [shakacode](https://github.com/shakacode/react_on_rails)
|
|
66
68
|
and [ElMassimo](https://github.com/ElMassimo) for inspiring and helping me with their gems.
|
67
69
|
|
68
70
|
**STATUS:** This gem is in the early stages of development, but is ready for use.
|
69
|
-
It has nearly 100% test coverage
|
70
|
-
|
71
|
-
of this company.
|
71
|
+
It has nearly 100% test coverage, with all tests passing. It has been in use since May/June 2025
|
72
|
+
on the systems of my two biggest customers.
|
72
73
|
|
73
74
|
If you have issues, please open one, and contributors are welcome!
|
74
75
|
|
75
76
|
## Requirements
|
76
77
|
|
77
78
|
- actual node installed on the server
|
78
|
-
- tested on
|
79
|
+
- tested on
|
80
|
+
- ruby 3.2.2 and rails 7.1
|
81
|
+
- ruby 2.7.5 and rails 6.1
|
82
|
+
- vite@6 (v7 not supported, see issues)
|
83
|
+
- turbolinks and hotwired/turbo
|
79
84
|
- vite_rails (the installer will install it by option --full or --vite)
|
80
85
|
- svelte v5 (see: [how to install svelte on rails/vite](https://dev.to/chmich/setup-inertia-and-svelte-on-rails-7-3glk))
|
81
86
|
- turbo (recommended / [how to install turbo on rails](https://github.com/hotwired/turbo-rails?tab=readme-ov-file#installation))
|
@@ -257,10 +262,8 @@ after any `*.svelte` file within the configured `components_folder` changed.
|
|
257
262
|
|
258
263
|
`ssr: :auto` is the default option, as set on config file and can be overridden on the view helper.
|
259
264
|
|
260
|
-
Works with `hotwired/turbo` only
|
261
|
-
|
262
265
|
By passing the `ssr: :auto` option to the view helper,
|
263
|
-
it checks if the request is an initial request (request header `X-Turbo-Request-ID` is present):
|
266
|
+
it checks if the request is an initial request (request header `X-Turbo-Request-ID` is present // configurable by `non_ssr_request_header`):
|
264
267
|
|
265
268
|
On Initial-Request, it adds the attribute `data-svelte-on-rails-initialize-action="hydrate"` and
|
266
269
|
returns a server side rendered component that will be hydrated on the frontend by the npm package.
|
@@ -275,6 +278,20 @@ loaded on very first load to the frontend, then the most work is done
|
|
275
278
|
in frontend and the server is relieved, except on initial request.
|
276
279
|
You will see no unpleasant «blink» on the page.
|
277
280
|
|
281
|
+
**Turbolinks**
|
282
|
+
|
283
|
+
If you are working on turbolinks, you can config the header or set something like
|
284
|
+
|
285
|
+
```
|
286
|
+
document.addEventListener('turbolinks:request-start', (event) => {
|
287
|
+
var xhr = event.data.xhr
|
288
|
+
xhr.setRequestHeader("X-Turbo-Request-ID", "any-content-for-skip-svelte-ssr")
|
289
|
+
});
|
290
|
+
```
|
291
|
+
|
292
|
+
to your javascript file.
|
293
|
+
|
294
|
+
|
278
295
|
**Tip: Performance optimisation for dropdowns**
|
279
296
|
|
280
297
|
For example, with dropdowns, you will never see the unpleasant «blink» effect because
|
@@ -294,6 +311,28 @@ rails svelte_on_rails:add_hello_world
|
|
294
311
|
rails svelte_on_rails:remove_hello_world
|
295
312
|
```
|
296
313
|
|
314
|
+
## Deploy
|
315
|
+
|
316
|
+
For example, by deploying with capistrano, you may add a step like
|
317
|
+
|
318
|
+
```ruby
|
319
|
+
before 'deploy:assets:precompile', 'deploy:npm:install'
|
320
|
+
namespace :deploy do
|
321
|
+
namespace :npm do
|
322
|
+
desc 'Install Node.js dependencies'
|
323
|
+
task :install do
|
324
|
+
on roles(:app) do
|
325
|
+
within release_path do
|
326
|
+
execute :npm, 'install'
|
327
|
+
end
|
328
|
+
end
|
329
|
+
end
|
330
|
+
end
|
331
|
+
end
|
332
|
+
```
|
333
|
+
|
334
|
+
to `deploy.rb` for making sure the ssr compilation is working.
|
335
|
+
|
297
336
|
## Contributors Guide
|
298
337
|
|
299
338
|
Contributors welcome!
|
@@ -83,6 +83,15 @@ module SvelteOnRails
|
|
83
83
|
end
|
84
84
|
end
|
85
85
|
|
86
|
+
def non_ssr_request_header
|
87
|
+
rss = @configs['non_ssr_request_header']
|
88
|
+
if rss.present?
|
89
|
+
rss
|
90
|
+
else
|
91
|
+
'X-Turbo-Request-ID'
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
86
95
|
# def dist_folder(app_root = nil)
|
87
96
|
# rails_root(app_root).join('public', 'svelteDist')
|
88
97
|
# end
|
@@ -2,19 +2,44 @@ module SvelteOnRails
|
|
2
2
|
module Installer
|
3
3
|
module Npm
|
4
4
|
|
5
|
-
def self.install_or_update_package(package_name, minimal_version: nil, update_to_latest: true, dev_dependency: false
|
5
|
+
def self.install_or_update_package(package_name, minimal_version: nil, major_version: nil, update_to_latest: true, dev_dependency: false)
|
6
6
|
pkg = inspect_package(package_name)
|
7
|
-
|
7
|
+
puts "#{package_name} already installed (#{pkg[:version].join('.')})" if pkg
|
8
|
+
|
9
|
+
to_do = if !pkg
|
10
|
+
true
|
11
|
+
elsif major_version
|
12
|
+
r = pkg[:version].first != major_version
|
13
|
+
if r
|
14
|
+
puts "updating to major version #{major_version}"
|
15
|
+
else
|
16
|
+
puts "nothing to do (major version #{major_version} OK)"
|
17
|
+
end
|
18
|
+
r
|
19
|
+
elsif minimal_version
|
20
|
+
r = pkg[:version].first < minimal_version.first || pkg[:version].second < minimal_version.second
|
21
|
+
puts "updating to minimal version #{minimal_version}" if r
|
22
|
+
r
|
23
|
+
else
|
24
|
+
puts 'nothing to do'
|
25
|
+
false
|
26
|
+
end
|
27
|
+
|
8
28
|
save_dev = (dev_dependency ? ' --save-dev' : '')
|
9
29
|
if to_do
|
10
30
|
|
11
|
-
cmd = if
|
31
|
+
cmd = if major_version
|
32
|
+
"npm install #{package_name}@#{major_version}#{save_dev}"
|
33
|
+
elsif update_to_latest
|
12
34
|
"npm install #{package_name}@latest#{save_dev}"
|
13
35
|
else
|
14
36
|
raise "ERROR: not implemented"
|
15
37
|
end
|
16
38
|
|
17
39
|
stdout, stderr, status = Open3.capture3(cmd)
|
40
|
+
|
41
|
+
puts cmd
|
42
|
+
|
18
43
|
if stderr.present?
|
19
44
|
raise "ERROR #{cmd} => #{stderr}"
|
20
45
|
end
|
@@ -52,7 +77,6 @@ module SvelteOnRails
|
|
52
77
|
end
|
53
78
|
end
|
54
79
|
|
55
|
-
|
56
80
|
def self.inspect_package(package_name)
|
57
81
|
pkg = nil
|
58
82
|
version = nil
|
@@ -75,34 +99,33 @@ module SvelteOnRails
|
|
75
99
|
|
76
100
|
end
|
77
101
|
|
78
|
-
def self.check_version(current_version, minimal_version)
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
end
|
102
|
+
# def self.check_version(current_version, minimal_version)
|
103
|
+
# if !current_version
|
104
|
+
# return false
|
105
|
+
# elsif !minimal_version.present?
|
106
|
+
# return true
|
107
|
+
# else
|
108
|
+
# compare_version_arrays(current_version, minimal_version)
|
109
|
+
# end
|
110
|
+
# end
|
87
111
|
|
88
112
|
private
|
89
113
|
|
90
|
-
def self.compare_version_arrays(current_version, minimal_version)
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
end
|
114
|
+
# def self.compare_version_arrays(current_version, minimal_version)
|
115
|
+
# raise "ERROR: current_version must be an array" unless current_version.is_a?(Array)
|
116
|
+
#
|
117
|
+
# current_version.each_with_index do |v, i|
|
118
|
+
# raise "ERROR: current_version entries must be an integer" unless v.is_a?(Integer)
|
119
|
+
#
|
120
|
+
# if minimal_version[i]
|
121
|
+
# raise "ERROR: minimal_version entries must be an integer" unless minimal_version[i].is_a?(Integer)
|
122
|
+
# if v < minimal_version[i]
|
123
|
+
# return false
|
124
|
+
# end
|
125
|
+
# end
|
126
|
+
# end
|
127
|
+
# true
|
128
|
+
# end
|
106
129
|
|
107
130
|
end
|
108
131
|
end
|
@@ -4,7 +4,7 @@ module SvelteOnRails
|
|
4
4
|
|
5
5
|
module Vite
|
6
6
|
|
7
|
-
def self.install_vite
|
7
|
+
def self.install_vite(major_version: nil, minimal_version: nil)
|
8
8
|
iu = SvelteOnRails::Installer::Utils
|
9
9
|
|
10
10
|
puts '-' * 80
|
@@ -44,7 +44,7 @@ module SvelteOnRails
|
|
44
44
|
# check npm package version
|
45
45
|
|
46
46
|
ni = SvelteOnRails::Installer::Npm
|
47
|
-
ni.install_or_update_package('vite',
|
47
|
+
ni.install_or_update_package('vite', major_version: major_version, minimal_version: minimal_version)
|
48
48
|
|
49
49
|
end
|
50
50
|
|
@@ -26,7 +26,12 @@ module SvelteOnRails
|
|
26
26
|
unless [true, false, :auto].include?(rss)
|
27
27
|
raise "Only true, false or auto are allowed for the argument #ssr"
|
28
28
|
end
|
29
|
-
|
29
|
+
|
30
|
+
ssr = if [:auto, 'auto'].include?(rss)
|
31
|
+
request.headers[conf.non_ssr_request_header].blank?
|
32
|
+
else
|
33
|
+
rss
|
34
|
+
end
|
30
35
|
|
31
36
|
hydrate = if props.key?(:hydrate)
|
32
37
|
props[:hydrate]
|
@@ -11,6 +11,11 @@ ssr: :auto
|
|
11
11
|
# :auto: render server side if request is initial request (works only with turbo, because it checks for the X-Turbo-Request-ID header)
|
12
12
|
# when not server-side rendered the components must be built as custom elements, see node-package @csedl/svelte-on-rails
|
13
13
|
|
14
|
+
non_ssr_request_header: 'X-Turbo-Request-ID'
|
15
|
+
# when ssr: :auto =>
|
16
|
+
# when this header contains any value (not blank), we are skipping server-side rendering
|
17
|
+
# when this header is empty we assume initial request and doing server-side rendering
|
18
|
+
|
14
19
|
development:
|
15
20
|
watch_changes: true
|
16
21
|
# Check on every request if any file within the svelte components folder have changed, for recompiling
|