humid 0.7.0 → 1.1.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.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +73 -42
  3. data/lib/humid/version.rb +1 -1
  4. data/lib/humid.rb +5 -0
  5. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4c7f343b4b1518296d59917fb37ce2f8a60ee6b1a57b71f3fd378ce2ad55a4d4
4
- data.tar.gz: e11384d2812e0de2cb7e676fa237957d44fbe63cf788e73adf997c9a14b2015e
3
+ metadata.gz: 4dace2fdf5d12412e01d86b520b5d77fd3fd04dd207b2815d93d0473283fa6a8
4
+ data.tar.gz: d8c40b486faa9f905c04c3d31c4eabd0cd304d0ad98d1666b1d76ad2aae32e0c
5
5
  SHA512:
6
- metadata.gz: d5ca07bdd2c64b97f51c45dd14de734ab37e608704414a494461cce0645a10d4fcfb3c1e414152066c16b9be3f98c2f55fdcb6cd1cb6e3e7464e3ebb64e7ed8f
7
- data.tar.gz: 52923317087b7439e80c347022acc55e102435367f3f0d54d52079f9e40129271aae60cba245bfe4d5d4352441988abd30882429f04d817e2528c029564a8cc3
6
+ metadata.gz: afc89db87f4b324b01b08a1f907ae74fd88a420b8c275095b9e7e44b8cf21e711b31dc18dbc0b021d85d8debca4e50986be1a16a2b43eec2a27ad1de6cc668c2
7
+ data.tar.gz: 1564ce03048d337b38891607afbbc360e47e5898601cb78a984ce4a003aafe1b3ff95dc6dc0a3faa70a6b6703446b11d7ddb86185250ad68e36e65401bf3dcb8
data/README.md CHANGED
@@ -1,4 +1,9 @@
1
- # Humid
1
+ <div align="center">
2
+ <img src="./images/svg/humid-icon.svg" alt="Humid" width="128" />
3
+ <h1>Humid</h1>
4
+ <p>A few helpers for Javascript server-side rendering on Rails with <a
5
+ href="https://github.com/rubyjs/mini_racer">mini_racer</a></p>
6
+ </div>
2
7
 
3
8
  ![Build Status](https://github.com/thoughtbot/humid/actions/workflows/build.yml/badge.svg?branch=main)
4
9
 
@@ -12,16 +17,20 @@ returns an HTML string.
12
17
 
13
18
  ## Design
14
19
 
15
- Humid is designed for the common case where all data is gathered before
16
- rendering. Your application fetches everything needed, passes it as props, and
17
- Humid returns the rendered HTML in a single synchronous call. It does not
18
- support streaming or async data fetching during render.
20
+ Humid is designed with 2 goals in mind:
19
21
 
20
- ## Caution
22
+ 1. Its for the common case where all data is gathered before rendering. Your
23
+ application fetches everything needed, passes it as props, and Humid returns
24
+ the rendered HTML in a single synchronous call. It does not support streaming
25
+ or async data fetching during render.
21
26
 
22
- This project is in its early phases of development. Its interface,
23
- behavior, and name are likely to change drastically before a major version
24
- release.
27
+ 2. Its a stepping stone for when you want to scale on the edge using
28
+ [Cloudflare V8 isolates](https://developers.cloudflare.com/workers/reference/how-workers-works/)
29
+ .`mini_racer` is a bare V8 environment, if your JS bundle works with
30
+ `mini_racer`, it'll work on Cloudflare V8 isolates.
31
+
32
+ > [!NOTE]
33
+ > See the [sample](./sample/) for a complete working example.
25
34
 
26
35
  ## Installation
27
36
 
@@ -69,10 +78,18 @@ Humid.configure do |config|
69
78
  #
70
79
  # Defaults to `nil`
71
80
  config.logger = Rails.env.local? ? Rails.logger : nil
81
+
82
+ # Path to a JavaScript file to eval into the MiniRacer context
83
+ # before the application bundle. Use this to provide globals that
84
+ # bare V8 doesn't have (TextEncoder, URL, MessageChannel, etc.).
85
+ #
86
+ # Optional. Defaults to nil (no prepend).
87
+ # config.prepend = Rails.root.join("path/to/shim.js")
72
88
  end
73
89
 
74
90
  if Rails.env.local?
75
- # Use single_threaded mode for Spring and other forked envs.
91
+ # Use single_threaded mode for dev and test environments.
92
+ # This will also work for mini_test parallel tests
76
93
  MiniRacer::Platform.set_flags! :single_threaded
77
94
  MINI_RACER_SSR = { context: MiniRacer::Context.new(timeout: 1000, ensure_gc_after_idle: 2000) }
78
95
 
@@ -161,9 +178,6 @@ MINI_RACER_SSR = { context: ctx }
161
178
  > [!NOTE]
162
179
  > If you pass a context that was already prepared, `prepare` will noop and return the context back to you.
163
180
 
164
- See the [sample server_rendering.tsx](./sample/server_rendering.tsx) to see how
165
- it is integrated.
166
-
167
181
  ### Call `Humid.render`
168
182
 
169
183
  And finally call `render` from ERB.
@@ -221,17 +235,19 @@ The default formatter returns `message` unchanged.
221
235
 
222
236
  ## Server-side libraries that detect node.js envs.
223
237
 
224
- You may need webpacker to create aliases for server friendly libraries that can
225
- not detect the `mini_racer` environment. For example, in `webpack.config.js`.
226
-
227
- ```diff
228
- ...
229
- resolve: {
230
- alias: {
231
- 'html-dom-parser': path.resolve(__dirname, '../../node_modules/html-dom-parser/lib/html-to-dom-server')
232
- }
238
+ Some libraries check for Node.js or browser globals to decide which code path
239
+ to use. In `mini_racer`, neither environment is detected. You may need to
240
+ configure your bundler to alias server-friendly versions. For example, in
241
+ esbuild:
242
+
243
+ ```js
244
+ // build_ssr.mjs
245
+ await esbuild.build({
246
+ // ...
247
+ alias: {
248
+ 'html-dom-parser': 'html-dom-parser/lib/html-to-dom-server'
233
249
  }
234
- ...
250
+ })
235
251
  ```
236
252
 
237
253
  ## Writing universal code
@@ -260,31 +276,45 @@ moving the `require` to `useEffect` in your component.
260
276
  }, [])
261
277
  ```
262
278
 
263
- ## Polyfills
279
+ ## MiniRacer Shim
264
280
 
265
- React SSR may import node.js dependencies that you need to polyfill for. See
266
- a sample esbuild [build script](./sample/bulid_ssr.js) and a [shim.js](./sample/shim.js)
267
- to get around these issues.
281
+ MiniRacer runs bare V8 no browser APIs, no Node APIs. React SSR and
282
+ libraries like `whatwg-url` expect globals that don't exist. Humid ships
283
+ a pre-built shim in [`shim/dist/shim.js`](./shim/) that provides:
268
284
 
269
- ## Testing
285
+ - **TextEncoder / TextDecoder** — real implementation from `text-encoding`
286
+ (not stubs — `whatwg-url` needs them to actually encode strings)
287
+ - **URL / URLSearchParams** — from `whatwg-url`
288
+ - **MessageChannel** — stub (React's scheduler references it)
289
+ - **navigator** — stub with `language: 'en-us'`
290
+ - **source-map-support** — rewrites JS stack traces using source maps,
291
+ so Humid errors show original filenames and line numbers
292
+ (`app/views/posts/show.html.tsx:42`) instead of bundled positions
293
+ (`server_rendering.js:12345`)
270
294
 
271
- The snippet When running in test environments that fork (e.g., parallel tests), each
272
- worker needs its own context since MiniRacer is not fork-safe. Using
273
- `MINI_RACER_SSR` as a hash makes this straightforward:
295
+ Use it with the `prepend` config option:
274
296
 
275
297
  ```ruby
276
- ActiveSupport.on_load(:action_dispatch_integration_test) do
277
- parallelize_setup do
278
- MINI_RACER_SSR[:context].dispose
279
- MINI_RACER_SSR[:context] = MiniRacer::Context.new(timeout: 1000, ensure_gc_after_idle: 2000)
280
- end
281
-
282
- parallelize_teardown do
283
- MINI_RACER_SSR[:context].dispose
284
- end
298
+ Humid.configure do |config|
299
+ config.prepend = Rails.root.join("path/to/shim.js")
285
300
  end
286
301
  ```
287
302
 
303
+ The shim is eval'd into the MiniRacer context before your SSR bundle,
304
+ so all globals are available when your code initializes.
305
+
306
+ To rebuild the shim (e.g., after updating dependencies):
307
+
308
+ ```sh
309
+ cd shim
310
+ npm install
311
+ npm run build
312
+ # outputs shim/dist/shim.js
313
+ ```
314
+
315
+ See the [shim README](./shim/README.md) for details on the two-stage
316
+ build process.
317
+
288
318
  ## Telemetry
289
319
 
290
320
  The `MiniRacer::Context` gives you access to V8 heap statistics for monitoring
@@ -321,13 +351,14 @@ end
321
351
  A steadily climbing `used_heap_size` across requests indicates a memory leak in
322
352
  your JavaScript bundle.
323
353
 
354
+
324
355
  ## Contributing
325
356
 
326
357
  Please see [CONTRIBUTING.md](/CONTRIBUTING.md).
327
358
 
328
359
  ## License
329
360
 
330
- Humid is Copyright © 2021-2024 Johny Ho.
361
+ Humid is Copyright © 2021-2026 Johny Ho.
331
362
  It is free software, and may be redistributed under the terms specified in the
332
363
  [LICENSE](/LICENSE.md) file.
333
364
 
@@ -350,4 +381,4 @@ We are [available for hire][hire].
350
381
 
351
382
  [mini_racer]: https://github.com/rubyjs/mini_racer
352
383
  [vue_ssr]: https://ssr.vuejs.org/
353
- [sample]: ./webpack.config.js
384
+ [sample]: ./sample/
data/lib/humid/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Humid
2
- VERSION = "0.7.0".freeze
2
+ VERSION = "1.1.0".freeze
3
3
  end
data/lib/humid.rb CHANGED
@@ -44,6 +44,11 @@ module Humid
44
44
  ctx.attach("console.warn", proc { |*args| logger.warn(fmt.call(:warn, *args)) })
45
45
  end
46
46
 
47
+ prepend_path = effective_config.prepend
48
+ if prepend_path
49
+ ctx.eval(File.read(prepend_path))
50
+ end
51
+
47
52
  js = remove_functions + renderer
48
53
  ctx.eval(js)
49
54
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: humid
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Johny Ho