humid 0.5.0 → 0.6.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 +42 -54
  3. data/lib/humid/version.rb +1 -1
  4. data/lib/humid.rb +11 -3
  5. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1b62f6d6c3c7ffac0ba1c609965742a6a09dafebe0ebb9fc05cbae3c9200299e
4
- data.tar.gz: 56557384d6768ada9b8fa9e9403720b5e75f03a579e39483892afcb631cb04a4
3
+ metadata.gz: 961eafadcfe1132cdd132c096a28aba48155c740b224c3cbc32b4fd9731c10a6
4
+ data.tar.gz: 8a3635e896a4915c508a096c3b9f60e7d51265b39c6c448150fb89a627a3ab06
5
5
  SHA512:
6
- metadata.gz: 4465a746beab25d302782cc71b8a70cd7fe827dc6ce3058e6720056c121bb86357733a9d31a7c266ceee6a53f938b174fec1eef111dbc634aa6699ffccb30ac9
7
- data.tar.gz: c5d8a1c61b84b49a14a78c7d198af09871bbdf9cebc8c9bbd1d4b32c2cdd95ac7de2bad06214f80ec12a8507158b8bb95a592eff313e51e454dcd768e5a2cdc1
6
+ metadata.gz: 7f9f13d45e1b4023cccdc88300c151024dbb08a5bcc73c24b1a81e089f1ad1d2a4ec1faea12feb81e13949e2c423a8e8a392d73bf45b547ecdcd20a6462b74b6
7
+ data.tar.gz: fba2e9ea74fdeab8a90f5503a0b34cf977ddb0006dcaf234ceff8b1d06548079461e0e5d17466cace1c68937b7fa290a5823ef0fe01352886b545b0fe87c9cc2
data/README.md CHANGED
@@ -71,33 +71,54 @@ Humid.configure do |config|
71
71
  config.logger = Rails.env.local? ? Rails.logger : nil
72
72
  end
73
73
 
74
- # if Rails.env.local?
75
- # # Use single_threaded mode for Spring and other forked envs.
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)
79
- # end
74
+ if Rails.env.local?
75
+ # Use single_threaded mode for Spring and other forked envs.
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)
79
+ end
80
80
  ```
81
81
 
82
82
  ## Usage
83
83
 
84
+ ### Set a renderer
84
85
 
85
- ### Create the MiniRacer Context.
86
+ In your entry file, e.g, `server_rendering.js` (specified in
87
+ `config.application_path`), pass your HTML render function to
88
+ `setHumidRenderer`. There is no need to require the function, its included in
89
+ the environment.
86
90
 
87
- On local development or test environments, uncomment the below.
91
+ ```javascript
92
+ // Set a factory function that will create a new instance of our app
93
+ // for each request.
94
+ setHumidRenderer((json) => {
95
+ const initialState = JSON.parse(json)
88
96
 
89
- ```ruby
90
- if Rails.env.local?
91
- # Use single_threaded mode for Spring and other forked envs.
92
- MiniRacer::Platform.set_flags! :single_threaded
93
- ctx = MiniRacer::Context.new(timeout: 100, ensure_gc_after_idle: 2000)
94
- MINI_RACER_CONTEXT = Humid.prepare(ctx)
95
- end
97
+ return ReactDOMServer.renderToString(
98
+ <Application initialPage={initialState}/>
99
+ )
100
+ })
101
+ ```
102
+
103
+ If you'd like support for source map support, you will need to add the following
104
+ to the same file and set `config.source_map_path` like the configuration above.
105
+
106
+ ```javascript
107
+ require("source-map-support").install({
108
+ retrieveSourceMap: filename => {
109
+ return {
110
+ url: filename,
111
+ map: readSourceMap(filename)
112
+ };
113
+ }
114
+ });
96
115
  ```
97
116
 
117
+ ### Your webserver
118
+
98
119
  On production, keep in mind that `mini_racer` is **thread safe, but not fork
99
120
  safe**. When using with web servers that employ forking, create a
100
- `MINI_RACER_CONTEXT` with options of your choosing **on worker boot. There
121
+ `MINI_RACER_CONTEXT` with options of your choosing on worker boot. **There
101
122
  should be no context created on the master process.**
102
123
 
103
124
  For example with puma:
@@ -106,6 +127,7 @@ For example with puma:
106
127
  # config/puma.rb
107
128
  on_worker_boot do
108
129
  ctx = MiniRacer::Context.new(timeout: 100, ensure_gc_after_idle: 2000)
130
+
109
131
  MINI_RACER_CONTEXT = Humid.prepare(ctx)
110
132
  end
111
133
 
@@ -114,11 +136,8 @@ on_worker_shutdown do
114
136
  end
115
137
  ```
116
138
 
117
- ### Prepare the context with `Humid.prepare`
118
-
119
- `Humid.prepare` will prepare the context's environment by [removing
120
- functions](#functions-not-available), delegate `console.log` and friends to
121
- your logger, load the SSR js bundle, and add the render function.
139
+ `Humid.prepare` will prepare the context's
140
+ [environment](#the-mini_racer-environment).
122
141
 
123
142
  You can also override config options per-context:
124
143
 
@@ -130,41 +149,10 @@ MINI_RACER_CONTEXT = Humid.prepare(
130
149
  )
131
150
  ```
132
151
 
133
- If you'd like support for source map support, you will need to
134
-
135
- 1. Add the following to your entry file, e.g, `server_rendering.js`.
136
- 2. set `config.source_map_path`.
137
-
138
- ```javascript
139
- require("source-map-support").install({
140
- retrieveSourceMap: filename => {
141
- return {
142
- url: filename,
143
- map: readSourceMap(filename)
144
- };
145
- }
146
- });
147
- ```
148
-
149
152
  See the [sample server_rendering.tsx](./sample/server_rendering.tsx) to see how
150
153
  it is integrated.
151
154
 
152
- ### Add a renderer and call `Humid.render`
153
-
154
- In your entry file, e.g, `server_rendering.js`, pass your HTML render function
155
- to `setHumidRenderer`. There is no need to require the function.
156
-
157
- ```javascript
158
- // Set a factory function that will create a new instance of our app
159
- // for each request.
160
- setHumidRenderer((json) => {
161
- const initialState = JSON.parse(json)
162
-
163
- return ReactDOMServer.renderToString(
164
- <Application initialPage={initialState}/>
165
- )
166
- })
167
- ```
155
+ ### Call `Humid.render`
168
156
 
169
157
  And finally call `render` from ERB.
170
158
 
@@ -178,7 +166,7 @@ Instrumentation is included:
178
166
  Completed 200 OK in 14ms (Views: 0.2ms | Humid SSR: 11.0ms | ActiveRecord: 2.7ms)
179
167
  ```
180
168
 
181
- ## The prepared `mini_racer` environment.
169
+ ## The `mini_racer` environment
182
170
 
183
171
  ### Functions not available
184
172
 
data/lib/humid/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Humid
2
- VERSION = "0.5.0".freeze
2
+ VERSION = "0.6.0".freeze
3
3
  end
data/lib/humid.rb CHANGED
@@ -13,6 +13,9 @@ module Humid
13
13
  class FileNotFound < StandardError
14
14
  end
15
15
 
16
+ class NotPrepared < StandardError
17
+ end
18
+
16
19
  mattr_accessor :config
17
20
 
18
21
  self.config = ActiveSupport::OrderedOptions.new.merge({
@@ -39,9 +42,7 @@ module Humid
39
42
  ctx.attach("console.warn", proc { |*args| logger.warn(fmt.call(:warn, *args)) })
40
43
  end
41
44
 
42
- js = ""
43
- js << remove_functions
44
- js << renderer
45
+ js = remove_functions + renderer
45
46
  ctx.eval(js)
46
47
 
47
48
  source_path = effective_config.application_path
@@ -54,10 +55,17 @@ module Humid
54
55
  filename = File.basename(source_path.to_s)
55
56
  ctx.eval(File.read(source_path), filename: filename)
56
57
 
58
+ def ctx.humid_prepared?
59
+ true
60
+ end
61
+
57
62
  ctx
58
63
  end
59
64
 
60
65
  def render(ctx, *args)
66
+ is_prepared = ctx.respond_to?(:humid_prepared?) && ctx.humid_prepared?
67
+ raise Humid::NotPrepared, "Context was not prepared with Humid.prepare" unless is_prepared
68
+
61
69
  ActiveSupport::Notifications.instrument("render.humid") do
62
70
  ctx.call("__renderer", *args)
63
71
  rescue MiniRacer::RuntimeError => e
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.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Johny Ho
@@ -37,7 +37,7 @@ dependencies:
37
37
  - - "~>"
38
38
  - !ruby/object:Gem::Version
39
39
  version: '8.0'
40
- description: Javascript SSR rendering for Rails
40
+ description: Javascript Server Side Rendering (SSR) for Rails
41
41
  email: jho406@gmail.com
42
42
  executables: []
43
43
  extensions: []
@@ -68,5 +68,5 @@ required_rubygems_version: !ruby/object:Gem::Requirement
68
68
  requirements: []
69
69
  rubygems_version: 3.6.9
70
70
  specification_version: 4
71
- summary: Javascript SSR rendering for Rails
71
+ summary: Javascript Server Side Rendering (SSR) for Rails
72
72
  test_files: []