svelte-on-rails 0.0.44 → 1.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 +4 -4
- data/README.md +87 -66
- 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: af9b4125161ea52c5785989db9e7f4b250a4bbe993f49264403680c5aa5467fb
|
4
|
+
data.tar.gz: 1d21a733aa52d57f892159baeb88c162cb78f46fca648dd2f31b45e3db8819e0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 72c0797453be3020409b6e28f5b7f472e8e0c1a22e010c401925ad2a81ac360c522612a7e52bac3d7a8da5a4c51f62668a68d46a33b3a955ed9e072366a567fd
|
7
|
+
data.tar.gz: 7f377d2c359daa8a9a2e18cd7c147187dd4249bbd0a8b5987f0feddb4a7b72020bc49591e04502055e4a7223e78e05b8e832c21c345dc93ac1d943440587dfc0
|
data/README.md
CHANGED
@@ -3,18 +3,22 @@
|
|
3
3
|
Seamless and robust Svelte in Rails integration.
|
4
4
|
|
5
5
|
By default, and when installed together with `@hotwired/turbo-rails`, it renders
|
6
|
-
svelte components on first request server side («SSR») and for subsequent
|
7
|
-
requests it provides a empty tag
|
6
|
+
svelte components on the first request server side («SSR») and for subsequent
|
7
|
+
requests it provides a empty tag which is mounted on the frontend
|
8
|
+
by the associated npm package [@csedl/svelte-on-rails](https://www.npmjs.com/package/@csedl/svelte-on-rails).
|
8
9
|
|
9
10
|
This way svelte works perfectly together with turbo. You will never notice
|
10
|
-
this unpleasant «blink» on the frontend and
|
11
|
-
|
11
|
+
this unpleasant «blink» on the frontend and is optimised for maximum
|
12
|
+
performance. SSR compilation is done by vite.
|
12
13
|
|
13
|
-
|
14
|
-
This all is developed on Rails-7 together with `vite_rails`.
|
14
|
+
All this is developed on Rails-7 together with `vite_rails`.
|
15
15
|
|
16
16
|
Thanks to [shakacode](https://github.com/shakacode/react_on_rails)
|
17
|
-
and [ElMassimo](https://github.com/ElMassimo)
|
17
|
+
and [ElMassimo](https://github.com/ElMassimo) for inspiring and helping me with their gems.
|
18
|
+
|
19
|
+
**STATUS** This gem is in early development but ready to use for curious developers.
|
20
|
+
It has a 100% test coverage, all tests pass.
|
21
|
+
It is not yet implemented on my customer projects, but this will follow soon.
|
18
22
|
|
19
23
|
If you have issues, please open one and contributors are welcome!
|
20
24
|
|
@@ -30,9 +34,7 @@ If you have issues, please open one and contributors are welcome!
|
|
30
34
|
|
31
35
|
## Installation from cero
|
32
36
|
|
33
|
-
|
34
|
-
|
35
|
-
If you want to start from a new rails app, follow theese tutorials
|
37
|
+
With haml, vite, svelte and turbo:
|
36
38
|
|
37
39
|
```bash
|
38
40
|
rails new my-test-app --skip-javascript
|
@@ -71,14 +73,17 @@ And there is the option `--force` that would not ask you whether it should overw
|
|
71
73
|
This means, if you want all, except haml, you can run:
|
72
74
|
|
73
75
|
```bash
|
74
|
-
rails g svelte_on_rails:install --vite --turbo --svelte --hello-world
|
76
|
+
rails g svelte_on_rails:install --vite --turbo --svelte --hello-world
|
75
77
|
```
|
76
78
|
|
77
|
-
The installer is written carefully: It does not overwrite a file without asking
|
78
|
-
|
79
|
+
The installer is written carefully: It does not overwrite a file without asking
|
80
|
+
and tells you all what he is doing.
|
79
81
|
|
80
82
|
At the end, you just have to (re-) start your server
|
81
|
-
and you will see a Svelte component.
|
83
|
+
and you will see a Svelte Hello World component.
|
84
|
+
|
85
|
+
This hello world has a second page where the most common import statements are demonstrated,
|
86
|
+
separated for server and client side rendering.
|
82
87
|
|
83
88
|
Then, you can run
|
84
89
|
|
@@ -111,34 +116,27 @@ Within the config file, there are mainly two important tags:
|
|
111
116
|
|
112
117
|
## Check if it all works
|
113
118
|
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
- Rendered HelloWorld.svelte server-side: 0.075ms
|
119
|
+
Server Side Rendering (SSR) is a parallel pipeline to client side rendering.
|
120
|
+
Both should return the same html. And your global styles should be applied same way
|
121
|
+
for both cases. For 95% of use cases this is the case.
|
118
122
|
|
119
|
-
|
120
|
-
|
121
|
-
Styles you find within the svelte component.
|
122
|
-
Click this button and see that the component is alive.
|
123
|
+
For check the **ssr pipeline** you can pass the options `ssr: true` and `hydrate: false`
|
124
|
+
to the view helper. This way you will see a «dead» html (no javascript is working).
|
123
125
|
|
124
|
-
|
125
|
-
|
126
|
+
For check the **client side pipeline** you can pass the option `ssr: false` and
|
127
|
+
`hydrate: true` to the view helper.
|
126
128
|
|
127
|
-
|
128
|
-
|
129
|
-
you would see the same html, and the styled button,
|
130
|
-
but the increment button would not work.
|
131
|
-
|
132
|
-
I inserted very detailed error messages on rails and on the
|
133
|
-
npm package, for making sure that thisk setup is working.
|
134
|
-
So please check the logs on your browser.
|
129
|
+
If both are looking similar, you are good to go. Then, remove theese options, the defaults are
|
130
|
+
`ssr: :auto` and `hydrate: true`.
|
135
131
|
|
136
132
|
### Import statements
|
137
133
|
|
138
|
-
|
139
|
-
|
134
|
+
The most importand import statements that are served by this gem are included in the
|
135
|
+
hello world component and by that they are also within the testing scope. So you can
|
136
|
+
be sure that they are working. If importand statements are missing there, pelase
|
137
|
+
tell me.
|
140
138
|
|
141
|
-
Among others
|
139
|
+
Among others, working statements are:
|
142
140
|
|
143
141
|
- `import svg from '../example.svg?raw'`
|
144
142
|
- `<script lang="ts">` (svelte component with typescript syntax)
|
@@ -149,7 +147,7 @@ Among others there are
|
|
149
147
|
|
150
148
|
Usual vite has a `vite.config.ts` file, that is used for the client side precompilation.
|
151
149
|
|
152
|
-
By running this installer it adds a npm runner so that you can do `npm run build:ssr`
|
150
|
+
By running this installer it adds `vite-ssr.config.ts` and a npm runner so that you can do `npm run build:ssr`
|
153
151
|
which does the server side precompilation.
|
154
152
|
|
155
153
|
The same job is triggered alongside `rails assets:precompile` for production environments.
|
@@ -179,33 +177,6 @@ loaded on very first load to the frontend, then the most work is done
|
|
179
177
|
in frontend and the server is relieved, except on initial request.
|
180
178
|
You will see no unpleasant «blink» on the page.
|
181
179
|
|
182
|
-
## Styles
|
183
|
-
|
184
|
-
For 99% of use cases you can just skip this chapter.
|
185
|
-
|
186
|
-
You can simply work with global styles as well as styles within the svelte component.
|
187
|
-
|
188
|
-
A server-side rendered svelte component has 2 states:
|
189
|
-
|
190
|
-
#### Before hydration
|
191
|
-
|
192
|
-
- The `svelte_component` view helper renders the styles contained within the component
|
193
|
-
into a style tag within the component's wrapper element. This has to be done this way because of Turbo.
|
194
|
-
- In very, very rare cases, global styles are not applied in the same way as after hydration.
|
195
|
-
|
196
|
-
#### After hydration
|
197
|
-
|
198
|
-
- Svelte adds a style tag inside the header
|
199
|
-
- Svelte renders the component again, which removes the style tag inside the component wrapper.
|
200
|
-
|
201
|
-
#### If you notice a "blink"
|
202
|
-
|
203
|
-
For the app to look stable, both states must appear in the same way.
|
204
|
-
Normally this is the case. But if there are problems,
|
205
|
-
or you want to see the state before hydration, for development purposes, you can pass
|
206
|
-
the `hydrate: false` option to the view helper,
|
207
|
-
and no hydration will happen for this component.
|
208
|
-
|
209
180
|
## More rake tasks
|
210
181
|
|
211
182
|
This tasks are more for testing/playground purposes
|
@@ -222,19 +193,28 @@ rails svelte_on_rails:remove_hello_world
|
|
222
193
|
|
223
194
|
Contributors welcome!
|
224
195
|
|
225
|
-
|
196
|
+
**Download the code and run the tests**
|
197
|
+
|
198
|
+
Download the source code from the repository, and within the project folder run:
|
226
199
|
|
227
200
|
```bash
|
228
201
|
rake svelte_on_rails:create_contributor_configs_file
|
229
202
|
```
|
230
203
|
and define a `generated_test_app_folder_path` (required) for apps, generated for the testings.
|
231
204
|
|
232
|
-
For development of the **npm package @csedl/svelte-on-rails**
|
233
|
-
|
205
|
+
For development of the **npm package @csedl/svelte-on-rails** (optional) please download the source code of the
|
206
|
+
npm package and set
|
207
|
+
`local_npm_package_path` and set the path to the npm package on your local machine.
|
234
208
|
This will cause the installer, to install the npm package from a local path instead from the npm registry.
|
235
209
|
|
210
|
+
Then run the tests and start contributing.
|
211
|
+
|
236
212
|
Tests are based on the included templates, like the `hello world template` and on the installer.
|
237
213
|
|
214
|
+
Here I learned how to write tests! Testing installers is heavy, you will read about it in the
|
215
|
+
test_helpers. But I have done my best to give all the code a clear structure.
|
216
|
+
I hope you like it, and improvements are welcome.
|
217
|
+
|
238
218
|
**Installer tests** starting with completely destroy the rails app within the `generated_test_app_path`,
|
239
219
|
generating a new rails app and running the installer and test by `playwright` if the components are working.
|
240
220
|
|
@@ -260,6 +240,47 @@ The current test cases including (among others):
|
|
260
240
|
create a completely new rails app, running the full installer and check if a hello World component is visible and javascript is working.
|
261
241
|
run assets:precompile within a rails app and check if the gem does its precompiling too.
|
262
242
|
|
243
|
+
## Understanding the process
|
244
|
+
|
245
|
+
**Why not client and server rendering in one process?**
|
246
|
+
|
247
|
+
That was my idea! `application.js` which is the usual entry point for the client
|
248
|
+
side could live on the same assets and manifest like svelte components that are
|
249
|
+
compiled as chunks which each is its own entry point. This failed:
|
250
|
+
|
251
|
+
- The `vite-plugin-ruby` did not support this: it constrained all to one entry point.
|
252
|
+
- See how fat the `vite-ssr.config.ts`. For the client side this is not necessary.
|
253
|
+
|
254
|
+
At the end, I decided to split the process. But that is not the last decision.
|
255
|
+
|
256
|
+
**Why not compiling server side purely by rollup?**
|
257
|
+
|
258
|
+
Advantages would be much slimmer packages and faster compilation. On the first
|
259
|
+
step i did this and i backed up a working and tested code on the branch [rollup-ssr](https://gitlab.com/sedl/svelte-on-rails/-/blob/rollup-ssr/BRANCH_INFO.md?ref_type=heads).
|
260
|
+
|
261
|
+
I decided to use Vite to bring the client and server side rendering
|
262
|
+
closer together.
|
263
|
+
|
264
|
+
But this, too, is not the last decision.
|
265
|
+
|
266
|
+
For now we proceed with vite.
|
267
|
+
|
268
|
+
**How does it work now?**
|
269
|
+
|
270
|
+
Client side rendering is done by vite like usual.
|
271
|
+
|
272
|
+
Server side rendering is triggered, similar to [vite_rails](https://vite-ruby.netlify.app/guide/deployment.html)
|
273
|
+
on `assets:precompile`, and, if `watch_changes` is configured,
|
274
|
+
which is default for development, it is triggered
|
275
|
+
on every change of a `*.svelte` file within the configured `components_folder`.
|
276
|
+
|
277
|
+
On the server side only the `*.svelte` files are served. Theyr included
|
278
|
+
assets are linked to the client side assets folder, which is mapped by `manifest.json`.
|
279
|
+
|
280
|
+
Then, vite has two output folders: `vite-dev` for development and `vite` for production.
|
281
|
+
Within `vite-ssr.config.ts`, by the `RAILS_ENV` variable, is decided which one is used.
|
282
|
+
|
283
|
+
|
263
284
|
## Licence
|
264
285
|
|
265
286
|
License is MIT
|