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.
- checksums.yaml +4 -4
- data/README.md +36 -29
- data/lib/humid/version.rb +1 -1
- data/lib/humid.rb +2 -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: 4c7f343b4b1518296d59917fb37ce2f8a60ee6b1a57b71f3fd378ce2ad55a4d4
|
|
4
|
+
data.tar.gz: e11384d2812e0de2cb7e676fa237957d44fbe63cf788e73adf997c9a14b2015e
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
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
|
-
#
|
|
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(
|
|
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(
|
|
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
|
|
61
|
+
# logged and Humid.render will return an empty string.
|
|
62
62
|
#
|
|
63
63
|
# Defaults to true.
|
|
64
|
-
config.raise_render_errors = Rails.env.
|
|
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
|
-
|
|
78
|
-
|
|
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
|
|
121
|
-
|
|
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:
|
|
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
|
-
|
|
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
|
-
|
|
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(
|
|
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
|
|
260
|
-
|
|
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
|
-
|
|
269
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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 =
|
|
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
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
|