reactssr-rails 0.1.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 412dd6289c7a0285914f65ba6fac234bea374e76
4
- data.tar.gz: e3844a13ec50ca4c5efc97a19fdc94ed8bcc7c66
3
+ metadata.gz: b2e420b41a0ac2cbd7a80791eac20d8e54656917
4
+ data.tar.gz: 182e72687c06107c02c01322ee64a23277ba5564
5
5
  SHA512:
6
- metadata.gz: a6c5dcef77c33be7987366253b1538bcc9d833199e9d82cdf84529747c5f49294568b6a3770e5152e29151bfb18d890e3cb828f02c24e834661570368683d403
7
- data.tar.gz: 6737f09d1b615fd61da4edc142af155957c68d6ea0743f9033bee231a02c2bbecd0a211987ed3cb6e3d5c155ee71569ad824fb6025f6a620bbb566b06e7886a3
6
+ metadata.gz: 0239fd847a61cac5c7b901075b8946585ed4bd82fa637eab8ef7cb9da40ecfd4dadcdef33cc4607adf02ae2598a85d022904f78858f1b97cf58c10180d6ff496
7
+ data.tar.gz: 9f8f6a3980afa5e3c21e7fb043e2fc5deae38bc628f89843a7138e51dc3c4a619d3ab15e6a146b41c183b1ac1a71e69821859d1ae625a1e6256816bf0a0fafd5
data/README.md CHANGED
@@ -4,8 +4,14 @@
4
4
 
5
5
  # Reactssr::Rails
6
6
 
7
- Works with `react-rails` and `webpackrails` to server render react components by
8
- views.
7
+ React SSR in Rails.
8
+
9
+ This gem is to solve one problem, that's `react-rails` will compile all React
10
+ components in one file.
11
+
12
+ This gem will works well with `webpackrails` to let you use commonjs feature,
13
+ and only evaluate js code as few as possible, and in production environment it
14
+ will use precompiled static file **instead of** compiling the files again and again.
9
15
 
10
16
  ## Installation
11
17
 
@@ -25,19 +31,9 @@ Or install it yourself as:
25
31
 
26
32
  ## Usage
27
33
 
28
- The javascript assets folder structure is like views folder, but the base folder
29
- should be `components`, for example:
30
-
31
- ```
32
- javascripts -|
33
- components -|
34
- home -|
35
- index -|
36
- ```
37
-
38
- In the above folder structure, the home folder is the controller name, if your
39
- controller is under `some_namespace`, then the home folder should under `some_namespace`
40
- folder, just like `views` folder structure.
34
+ The SSR file is a js file whose name ends with `ssr.js` under base folder, and
35
+ base folder is the folder where you put all your SSR files at, the default base
36
+ folder name is `components`.
41
37
 
42
38
  You can config the base folder, put this within your `config/application.rb` file,
43
39
  `config.reactssr.assets_base = 'folder_name'`.
@@ -48,41 +44,25 @@ You can config the base folder, put this within your `config/application.rb` fil
48
44
  an example how to use it:
49
45
 
50
46
  ```js
51
- <%= react_ssr('IndexView', {props}, {prerender: true}) %>
47
+ <%= react_ssr('IndexView', {props}) %>
52
48
  ```
53
- If you specific `prerender: false` in the options, the `react_ssr` will use
54
- `react_component` instead (will be changed in future). For now, please just use
55
- `prerender: true`.
56
49
 
57
- So, how reactssr look up the `IndexView` react component in assets? Well, it will
58
- look up the view by controller name and action name, so the controller name is `home`
59
- and action name is `index`, then it will look up a file named `index.ssr.js` in
60
- folder `components/home/index/`. You just put your components in that file and
61
- expose those components on `Components` js global object. Here is an example of
62
- `index.ssr.js`:
50
+ So, suppose the current `controller_path` is `home`, then reactssr-rails will look
51
+ up a ssr file in components named `home.ssr.js`, just like below:
63
52
 
64
53
  ```js
65
- // index.ssr.js
54
+ // home.ssr.js
66
55
 
67
56
  Components.IndexView = require('./IndexView.jsx');
68
57
  ```
69
58
 
70
- And put your `IndexView.jsx` file under `components/home/index/` folder. Such
71
- file would be like this:
72
-
73
- ```js
74
- // IndexView.jsx
59
+ ## Example
75
60
 
76
- module.exports = React.createClass({
77
- render: function () {
78
- return <p>Hello world</p>
79
- }
80
- })
81
- ```
61
+ http://github.com/towry/reactssr-rails-example
82
62
 
83
- ## NOTICE
63
+ ## TODO
84
64
 
85
- Not tested in production environment.
65
+ - [x] In production env, do not run `webpackrails`
86
66
 
87
67
  ## Development
88
68
 
@@ -92,7 +72,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
92
72
 
93
73
  ## Contributing
94
74
 
95
- 1. Fork it ( https://github.com/[my-github-username]/reactssr-rails/fork )
75
+ 1. Fork it ( https://github.com/towry/reactssr-rails/fork )
96
76
  2. Create your feature branch (`git checkout -b my-new-feature`)
97
77
  3. Commit your changes (`git commit -am 'Add some feature'`)
98
78
  4. Push to the branch (`git push origin my-new-feature`)
@@ -1,5 +1,6 @@
1
1
  require "reactssr/rails/version"
2
2
  require "reactssr/rails/view_helper"
3
3
  require "reactssr/rails/railtie"
4
+ require "reactssr/rails/engine"
4
5
  require "reactssr/server_rendering/ssr_renderer"
5
6
 
@@ -0,0 +1,11 @@
1
+ module Reactssr
2
+ module Rails
3
+ class Engine < ::Rails::Engine
4
+ initializer 'reactssr.assets.precompile' do |app|
5
+ base = app.config.reactssr.assets_base
6
+ precompile = File.join(base, '*.ssr.js')
7
+ app.config.assets.precompile += [precompile]
8
+ end
9
+ end
10
+ end
11
+ end
@@ -6,10 +6,7 @@ module Reactssr
6
6
  # The folder that contains all the stuff
7
7
  config.reactssr.assets_base = 'components'
8
8
 
9
- # entry js file
10
- config.reactssr.entry = 'index.ssr.js'
11
-
12
- initializer "react_rails_ssr.setup_view_helpers", group: :all do |app|
9
+ initializer "reactssr.setup_view_helpers", group: :all do |app|
13
10
  ActiveSupport.on_load(:action_view) do
14
11
  include ::Reactssr::Rails::ViewHelper
15
12
  end
@@ -1,5 +1,5 @@
1
1
  module Reactssr
2
2
  module Rails
3
- VERSION = "0.1.0"
3
+ VERSION = "1.0.0"
4
4
  end
5
5
  end
@@ -8,10 +8,7 @@ module Reactssr
8
8
  def react_ssr(name, props = {}, options = {}, &block)
9
9
  options = {:tag => options} if options.is_a?(Symbol)
10
10
 
11
- prerender_options = options[:prerender]
12
- if !prerender_options and Rails.application.config.react.server_renderer != ::Reactssr::ServerRendering::SsrRenderer
13
- return react_component(name, props, options, &block)
14
- end
11
+ prerender_options = true
15
12
 
16
13
  # All the below stuff is to send the `controller_name`
17
14
  # and `action_name` to our ssr_renderer.
@@ -1,4 +1,5 @@
1
1
 
2
+ require 'multi_json'
2
3
  require 'react-rails'
3
4
 
4
5
  module Reactssr
@@ -15,6 +16,9 @@ module Reactssr
15
16
  end
16
17
 
17
18
  @before_render_code ||= File.read("#{File.dirname __FILE__}/../assets/before_render.js")
19
+
20
+ @manifest_base ||= File.join(::Rails.public_path, ::Rails.application.config.assets.prefix)
21
+ load_asset_on_init
18
22
 
19
23
  super(options.merge(code: js_code))
20
24
  end
@@ -23,9 +27,10 @@ module Reactssr
23
27
  prerender_options = pre_options[:prerender_options]
24
28
 
25
29
  @controller_path = pre_options.fetch(:controller_path, nil)
26
- @action_name = pre_options.fetch(:action_name, nil)
30
+ # @action_name = pre_options.fetch(:action_name, nil)
27
31
 
28
- if @controller_path.nil? or @action_name.nil?
32
+ # if @controller_path.nil? or @action_name.nil?
33
+ if @controller_path.nil?
29
34
  raise Reactssr::ServerRendering::RuntimeCommonError.new("Could not found the controller path or action name in current context.")
30
35
  end
31
36
 
@@ -50,8 +55,18 @@ module Reactssr
50
55
  # So, we will gather all the code for that controller#action
51
56
  # in here.
52
57
  def before_render(component_name, props, prerender_options)
53
- jscode = @before_render_code.dup
54
- jscode << ::Rails.application.assets[entry].to_s
58
+ jscode = @before_render_code.dup
59
+
60
+ success = false
61
+ if ::Rails.env.production?
62
+ content, success = load_asset(entry)
63
+ end
64
+
65
+ if not success
66
+ jscode << ::Rails.application.assets[entry].to_s
67
+ else
68
+ jscode << content
69
+ end
55
70
 
56
71
  # the result is jscode
57
72
  jscode
@@ -88,9 +103,64 @@ module Reactssr
88
103
 
89
104
  def entry
90
105
  components = ::Rails.application.config.reactssr.assets_base || 'components'
91
- entry_file = ::Rails.application.config.reactssr.entry || 'index.ssr.js'
92
- sub_path = File.join(@controller_path, @action_name)
93
- entry = File.join(components, sub_path, 'index.ssr.js')
106
+ entry = File.join(components, "#{@controller_path}.ssr.js")
107
+ end
108
+
109
+ private
110
+
111
+ # Return asset content
112
+ def load_asset(filename)
113
+ digest_path = assets[filename]
114
+
115
+ if digest_path.nil?
116
+ return '', false
117
+ end
118
+
119
+ file = File.join(@manifest_base, digest_path)
120
+ if not File.exist?(file)
121
+ return '', false
122
+ end
123
+
124
+ return File.read(file), true
125
+ end
126
+
127
+ def assets
128
+ @data['assets'] ||= {}
129
+ end
130
+
131
+ def files
132
+ @data['files'] ||= {}
133
+ end
134
+
135
+ # load assets from Rails.public_path
136
+ def load_asset_on_init
137
+ paths = Dir[File.join(@manifest_base, "manifest*.json")]
138
+ if paths.any?
139
+ path = paths.first
140
+ else
141
+ # No precompile
142
+ return {}
143
+ end
144
+
145
+ begin
146
+ if File.exist?(path)
147
+ data = json_decode(File.read(path))
148
+ end
149
+ rescue ::MultiJson::DecodeError => e
150
+ return {}
151
+ end
152
+
153
+ @data = data.is_a?(Hash) ? data : {}
154
+ end
155
+
156
+ if ::MultiJson.respond_to?(:dump)
157
+ def json_decode(obj)
158
+ ::MultiJson.load(obj)
159
+ end
160
+ else
161
+ def json_decode(obj)
162
+ ::MultiJson.decode(obj)
163
+ end
94
164
  end
95
165
  end
96
166
 
@@ -9,7 +9,7 @@ Gem::Specification.new do |spec|
9
9
  spec.authors = ["towry"]
10
10
  spec.email = ["tovvry@gmail.com"]
11
11
 
12
- spec.summary = %q{Server render React component by view.}
12
+ spec.summary = %q{React SSR in Rails}
13
13
  spec.description = %q{Works with `react-rails` and `webpackrails` to server render react components by
14
14
  views.}
15
15
  spec.homepage = "https://github.com/towry/reactssr-rails"
@@ -33,4 +33,5 @@ views.}
33
33
 
34
34
  # This gem depend on `react-rails`
35
35
  spec.add_dependency 'react-rails', '>= 1.1.0'
36
+ spec.add_dependency 'multi_json', '>= 1.0'
36
37
  end
metadata CHANGED
@@ -1,57 +1,71 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: reactssr-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - towry
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-08-18 00:00:00.000000000 Z
11
+ date: 2015-08-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ~>
18
18
  - !ruby/object:Gem::Version
19
19
  version: '1.9'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ~>
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.9'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ~>
32
32
  - !ruby/object:Gem::Version
33
33
  version: '10.0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ~>
39
39
  - !ruby/object:Gem::Version
40
40
  version: '10.0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: react-rails
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ">="
45
+ - - '>='
46
46
  - !ruby/object:Gem::Version
47
47
  version: 1.1.0
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ">="
52
+ - - '>='
53
53
  - !ruby/object:Gem::Version
54
54
  version: 1.1.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: multi_json
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '1.0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '1.0'
55
69
  description: |-
56
70
  Works with `react-rails` and `webpackrails` to server render react components by
57
71
  views.
@@ -61,8 +75,8 @@ executables: []
61
75
  extensions: []
62
76
  extra_rdoc_files: []
63
77
  files:
64
- - ".gitignore"
65
- - ".travis.yml"
78
+ - .gitignore
79
+ - .travis.yml
66
80
  - CODE_OF_CONDUCT.md
67
81
  - Gemfile
68
82
  - LICENSE.txt
@@ -72,6 +86,7 @@ files:
72
86
  - bin/setup
73
87
  - lib/reactssr/assets/before_render.js
74
88
  - lib/reactssr/rails.rb
89
+ - lib/reactssr/rails/engine.rb
75
90
  - lib/reactssr/rails/railtie.rb
76
91
  - lib/reactssr/rails/version.rb
77
92
  - lib/reactssr/rails/view_helper.rb
@@ -88,18 +103,18 @@ require_paths:
88
103
  - lib
89
104
  required_ruby_version: !ruby/object:Gem::Requirement
90
105
  requirements:
91
- - - ">="
106
+ - - '>='
92
107
  - !ruby/object:Gem::Version
93
108
  version: '0'
94
109
  required_rubygems_version: !ruby/object:Gem::Requirement
95
110
  requirements:
96
- - - ">="
111
+ - - '>='
97
112
  - !ruby/object:Gem::Version
98
113
  version: '0'
99
114
  requirements: []
100
115
  rubyforge_project:
101
- rubygems_version: 2.4.7
116
+ rubygems_version: 2.4.6
102
117
  signing_key:
103
118
  specification_version: 4
104
- summary: Server render React component by view.
119
+ summary: React SSR in Rails
105
120
  test_files: []