humid 0.6.0 → 0.7.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 +36 -29
  3. data/lib/humid/version.rb +1 -1
  4. data/lib/humid.rb +2 -0
  5. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 961eafadcfe1132cdd132c096a28aba48155c740b224c3cbc32b4fd9731c10a6
4
- data.tar.gz: 8a3635e896a4915c508a096c3b9f60e7d51265b39c6c448150fb89a627a3ab06
3
+ metadata.gz: 4c7f343b4b1518296d59917fb37ce2f8a60ee6b1a57b71f3fd378ce2ad55a4d4
4
+ data.tar.gz: e11384d2812e0de2cb7e676fa237957d44fbe63cf788e73adf997c9a14b2015e
5
5
  SHA512:
6
- metadata.gz: 7f9f13d45e1b4023cccdc88300c151024dbb08a5bcc73c24b1a81e089f1ad1d2a4ec1faea12feb81e13949e2c423a8e8a392d73bf45b547ecdcd20a6462b74b6
7
- data.tar.gz: fba2e9ea74fdeab8a90f5503a0b34cf977ddb0006dcaf234ceff8b1d06548079461e0e5d17466cace1c68937b7fa290a5823ef0fe01352886b545b0fe87c9cc2
6
+ metadata.gz: d5ca07bdd2c64b97f51c45dd14de734ab37e608704414a494461cce0645a10d4fcfb3c1e414152066c16b9be3f98c2f55fdcb6cd1cb6e3e7464e3ebb64e7ed8f
7
+ data.tar.gz: 52923317087b7439e80c347022acc55e102435367f3f0d54d52079f9e40129271aae60cba245bfe4d5d4352441988abd30882429f04d817e2528c029564a8cc3
data/README.md CHANGED
@@ -43,25 +43,25 @@ Add an initializer to configure the default options for `Humid.render`. These
43
43
  are overridable on `Humid.render`.
44
44
 
45
45
  ```ruby
46
- # app/initializers/humid.rb
46
+ # config/initializers/humid.rb
47
47
 
48
48
  Humid.configure do |config|
49
49
  # Path to your build file located in `app/assets/builds/`. You should use a
50
50
  # separate build apart from your `application.js`.
51
51
  #
52
52
  # Required
53
- config.application_path = Rails.root.join('app', 'assets', 'builds', 'server_rendering.js')
53
+ config.application_path = Rails.root.join("app/assets/builds/server_rendering.js")
54
54
 
55
55
  # Path to your source map file
56
56
  #
57
57
  # Optional
58
- config.source_map_path = Rails.root.join('app', 'assets', 'builds', 'server_rendering.js.map')
58
+ config.source_map_path = Rails.root.join("app/assets/builds/server_rendering.js.map")
59
59
 
60
60
  # Raise errors if JS rendering failed. If false, the error will be
61
- # logged out to Rails log and Humid.render will return an empty string
61
+ # logged and Humid.render will return an empty string.
62
62
  #
63
63
  # Defaults to true.
64
- config.raise_render_errors = Rails.env.development? || Rails.env.test?
64
+ config.raise_render_errors = Rails.env.local?
65
65
 
66
66
  # The logger instance.
67
67
  # `console.log` and friends (`warn`, `error`) are delegated to
@@ -74,8 +74,18 @@ end
74
74
  if Rails.env.local?
75
75
  # Use single_threaded mode for Spring and other forked envs.
76
76
  MiniRacer::Platform.set_flags! :single_threaded
77
- ctx = MiniRacer::Context.new(timeout: 100, ensure_gc_after_idle: 2000)
78
- MINI_RACER_CONTEXT = Humid.prepare(ctx)
77
+ MINI_RACER_SSR = { context: MiniRacer::Context.new(timeout: 1000, ensure_gc_after_idle: 2000) }
78
+
79
+ # Reload the context when the SSR bundle changes
80
+ ssr_checker = ActiveSupport::FileUpdateChecker.new([Humid.config.application_path.to_s]) do
81
+ MINI_RACER_SSR[:context].dispose
82
+ MINI_RACER_SSR[:context] = MiniRacer::Context.new(timeout: 1000, ensure_gc_after_idle: 2000)
83
+ end
84
+
85
+ Rails.application.reloaders << ssr_checker
86
+ Rails.application.reloader.to_run do
87
+ ssr_checker.execute_if_updated
88
+ end
79
89
  end
80
90
  ```
81
91
 
@@ -117,22 +127,20 @@ require("source-map-support").install({
117
127
  ### Your webserver
118
128
 
119
129
  On production, keep in mind that `mini_racer` is **thread safe, but not fork
120
- safe**. When using with web servers that employ forking, create a
121
- `MINI_RACER_CONTEXT` with options of your choosing on worker boot. **There
122
- should be no context created on the master process.**
130
+ safe**. When using with web servers that employ forking, create the context
131
+ on worker boot. **There should be no context created on the master process.**
123
132
 
124
133
  For example with puma:
125
134
 
126
135
  ```ruby
127
136
  # config/puma.rb
128
137
  on_worker_boot do
129
- ctx = MiniRacer::Context.new(timeout: 100, ensure_gc_after_idle: 2000)
130
-
131
- MINI_RACER_CONTEXT = Humid.prepare(ctx)
138
+ ctx = MiniRacer::Context.new(timeout: 1000, ensure_gc_after_idle: 2000)
139
+ MINI_RACER_SSR = { context: Humid.prepare(ctx) }
132
140
  end
133
141
 
134
142
  on_worker_shutdown do
135
- MINI_RACER_CONTEXT.dispose
143
+ MINI_RACER_SSR[:context].dispose
136
144
  end
137
145
  ```
138
146
 
@@ -142,13 +150,17 @@ end
142
150
  You can also override config options per-context:
143
151
 
144
152
  ```ruby
145
- MINI_RACER_CONTEXT = Humid.prepare(
153
+ ctx = Humid.prepare(
146
154
  MiniRacer::Context.new(timeout: 1000),
147
155
  application_path: Rails.root.join("other_bundle.js"),
148
156
  logger: nil
149
157
  )
158
+ MINI_RACER_SSR = { context: ctx }
150
159
  ```
151
160
 
161
+ > [!NOTE]
162
+ > If you pass a context that was already prepared, `prepare` will noop and return the context back to you.
163
+
152
164
  See the [sample server_rendering.tsx](./sample/server_rendering.tsx) to see how
153
165
  it is integrated.
154
166
 
@@ -157,7 +169,7 @@ it is integrated.
157
169
  And finally call `render` from ERB.
158
170
 
159
171
  ```ruby
160
- <%= Humid.render(MINI_RACER_CONTEXT, json).html_safe %>
172
+ <%= Humid.render(MINI_RACER_SSR[:context], json).html_safe %>
161
173
  ```
162
174
 
163
175
  Instrumentation is included:
@@ -256,23 +268,19 @@ to get around these issues.
256
268
 
257
269
  ## Testing
258
270
 
259
- When running in test environments that also forks, you may need to set up new mini_racer
260
- contexts for each parallel worker. For example:
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:
261
274
 
262
275
  ```ruby
263
276
  ActiveSupport.on_load(:action_dispatch_integration_test) do
264
- include ActionView::Helpers::TranslationHelper
265
- include Devise::Test::IntegrationHelpers
266
-
267
277
  parallelize_setup do
268
- MINI_RACER_CONTEXT.dispose if defined?(MINI_RACER_CONTEXT)
269
- ctx = MiniRacer::Context.new(timeout: 1000, ensure_gc_after_idle: 2000)
270
- Object.send(:remove_const, :MINI_RACER_CONTEXT) if defined?(MINI_RACER_CONTEXT)
271
- Object.const_set(:MINI_RACER_CONTEXT, Humid.prepare(ctx))
278
+ MINI_RACER_SSR[:context].dispose
279
+ MINI_RACER_SSR[:context] = MiniRacer::Context.new(timeout: 1000, ensure_gc_after_idle: 2000)
272
280
  end
273
281
 
274
282
  parallelize_teardown do
275
- MINI_RACER_CONTEXT.dispose if defined?(MINI_RACER_CONTEXT)
283
+ MINI_RACER_SSR[:context].dispose
276
284
  end
277
285
  end
278
286
  ```
@@ -283,7 +291,7 @@ The `MiniRacer::Context` gives you access to V8 heap statistics for monitoring
283
291
  memory usage over time.
284
292
 
285
293
  ```ruby
286
- MINI_RACER_CONTEXT.heap_stats
294
+ MINI_RACER_SSR[:context].heap_stats
287
295
  # {:total_heap_size=>3100672,
288
296
  # :total_heap_size_executable=>4194304,
289
297
  # :total_physical_size=>1280640,
@@ -302,7 +310,7 @@ render_histogram = meter.create_histogram("humid.render.duration", unit: "ms", d
302
310
  heap_gauge = meter.create_gauge("humid.heap.used_bytes", unit: "By", description: "V8 heap used bytes")
303
311
 
304
312
  ActiveSupport::Notifications.subscribe("render.humid") do |event|
305
- stats = MINI_RACER_CONTEXT.heap_stats
313
+ stats = MINI_RACER_SSR[:context].heap_stats
306
314
  attributes = { "worker.pid" => Process.pid.to_s }
307
315
 
308
316
  render_histogram.record(event.duration, attributes: attributes)
@@ -338,7 +346,6 @@ We are [available for hire][hire].
338
346
  [community]: https://thoughtbot.com/community?utm_source=github
339
347
  [hire]: https://thoughtbot.com/hire-us?utm_source=github
340
348
 
341
-
342
349
  <!-- END /templates/footer.md -->
343
350
 
344
351
  [mini_racer]: https://github.com/rubyjs/mini_racer
data/lib/humid/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Humid
2
- VERSION = "0.6.0".freeze
2
+ VERSION = "0.7.0".freeze
3
3
  end
data/lib/humid.rb CHANGED
@@ -30,6 +30,8 @@ module Humid
30
30
  end
31
31
 
32
32
  def prepare(ctx, options = {})
33
+ return ctx if ctx.respond_to?(:humid_prepared?) && ctx.humid_prepared?
34
+
33
35
  effective_config = config.merge(options)
34
36
  logger = effective_config.logger
35
37
  log_formatter = effective_config.log_formatter
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.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Johny Ho