Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

react_component_hash always has pre-render true #1077

Merged
merged 2 commits into from
May 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Changes since last non-beta release.
- Fix the setup for tests for spec/dummy so they automatically rebuild by correctly setting the source_path in the webpacker.yml
- Updated documentation for the testing setup.
- Above in [PR 1072](https://github.com/shakacode/react_on_rails/pull/1072) by [justin808](https://github.com/justin808).
- `react_component_hash` has implicit `prerender: true` because it makes no sense to have react_component_hash not use prerrender. Improved docs on `react_component_hash`. Also, fixed issue where checking gem existence. [PR 1077](https://github.com/shakacode/react_on_rails/pull/1077) by [justin808](https://github.com/justin808).

### [11.0.3] - 2018-04-24

Expand Down
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -357,10 +357,16 @@ You have 2 ways to specify your React components. You can either register the Re

ReactOnRails will automatically detect a registered generator function. Thus, there is no difference between registering a React Component versus a "generator function."

#### react_component_hash for Generator Functions
Another reason to use a generator function is that sometimes in server rendering, specifically with React Router, you need to return the result of calling ReactDOMServer.renderToString(element). You can do this by returning an object with the following shape: { renderedHtml, redirectLocation, error }. Make sure you use this function with `react_component_hash`.

For server rendering, if you wish to return multiple HTML strings from a generator function, you may return an Object from your generator function with a single top level property of `renderedHtml`. Inside this Object, place a key called `componentHtml`, along with any other needed keys. An example scenario of this is when you are using side effects libraries like [React Helmet](https://github.com/nfl/react-helmet). Your Ruby code will get this Object as a Hash containing keys componentHtml and any other custom keys that you added:

```js
{ renderedHtml: { componentHtml, customKey1, customKey2} }
```

For details on using react_component_hash with react-helmet, see the docs below for the helper API and [docs/additional-reading/react-helmet.md](../docs/additional-reading/react-helmet.md).

### Rails Context and Generator Functions
When you use a "generator function" to create react components (or renderedHtml on the server), or you used shared redux stores, you get two params passed to your function that creates a React component:
Expand Down Expand Up @@ -535,7 +541,44 @@ All options except `props, id, html_options` will inherit from your `react_on_ra
+ **replay_console:** Default is true. False will disable echoing server-rendering logs to the browser. While this can make troubleshooting server rendering difficult, so long as you have the configuration of `logging_on_server` set to true, you'll still see the errors on the server.
+ **logging_on_server:** Default is true. True will log JS console messages and errors to the server.
+ **raise_on_prerender_error:** Default is false. True will throw an error on the server side rendering. Your controller will have to handle the error.

### react_component_hash
`react_component_hash` is used to return multiple HTML strings for server rendering, such as for
adding meta-tags to a page. It is exactly like react_component except for the following:

1. `prerender: true` is automatically added to options, as this method doesn't make sense for
client only rendering.
2. Your JavaScript for server rendering must return an Object for the key `server_rendered_html`.
3. Your view code must expect an object and not a string.

Here is an example of ERB view code:

```erb
<% react_helmet_app = react_component_hash("ReactHelmetApp", prerender: true,
props: { helloWorldData: { name: "Mr. Server Side Rendering"}},
id: "react-helmet-0", trace: true) %>
<% content_for :title do %>
<%= react_helmet_app['title'] %>
<% end %>
<%= react_helmet_app["componentHtml"] %>
```

And here is the JavaScript code:

```js
export default (props, _railsContext) => {
const componentHtml = renderToString(<ReactHelmet {...props} />);
const helmet = Helmet.renderStatic();

const renderedHtml = {
componentHtml,
title: helmet.title.toString(),
};
return { renderedHtml };
};

```

### redux_store
#### Controller Extension
Include the module `ReactOnRails::Controller` in your controller, probably in ApplicationController. This will provide the following controller method, which you can call in your controller actions:
Expand Down
30 changes: 24 additions & 6 deletions lib/react_on_rails/react_on_rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,17 +120,35 @@ def react_component(component_name, options = {})

else
msg = <<-MSG.strip_heredoc
ReactOnRails: server_rendered_html is expected to be a String. If you're trying to
use a generator function to return a Hash to your ruby view code, then use
ReactOnRails: server_rendered_html is expected to be a String for #{component_name}. If you're
trying to use a generator function to return a Hash to your ruby view code, then use
react_component_hash instead of react_component and see
https://github.com/shakacode/react_on_rails/blob/master/spec/dummy/client/app/startup/ReactHelmetServerApp.jsx
for an example of the necessary javascript configuration."
for an example of the JavaScript code."
MSG
raise ReactOnRails::Error, msg
end
end

# react_component_hash is used to return multiple HTML strings for server rendering, such as for
# adding meta-tags to a page.
# It is exactly like react_component except for the following:
# 1. prerender: true is automatically added, as this method doesn't make sense for client only
# rendering.
# 2. Your JavaScript for server rendering must return an Object for the key server_rendered_html.
# 3. Your view code must expect an object and not a string.
#
# Here is an example of the view code:
# <% react_helmet_app = react_component_hash("ReactHelmetApp", prerender: true,
# props: { helloWorldData: { name: "Mr. Server Side Rendering"}},
# id: "react-helmet-0", trace: true) %>
# <% content_for :title do %>
# <%= react_helmet_app['title'] %>
# <% end %>
# <%= react_helmet_app["componentHtml"] %>
#
def react_component_hash(component_name, options = {})
options[:prerender] = true
internal_result = internal_react_component(component_name, options)
server_rendered_html = internal_result[:result]["html"]
console_script = internal_result[:result]["consoleReplayScript"]
Expand All @@ -148,9 +166,9 @@ def react_component_hash(component_name, options = {})
)
else
msg = <<-MSG.strip_heredoc
Generator function used by react_component_hash is expected to return an Object. See
https://github.com/shakacode/react_on_rails/blob/master/spec/dummy/client/app/startup/ReactHelmetServerApp.jsx
for an example of the necessary javascript configuration.
Generator function used by react_component_hash for #{component_name} is expected to return
an Object. See https://github.com/shakacode/react_on_rails/blob/master/spec/dummy/client/app/startup/ReactHelmetServerApp.jsx
for an example of the JavaScript code."
MSG
raise ReactOnRails::Error, msg
end
Expand Down
4 changes: 2 additions & 2 deletions lib/react_on_rails/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,11 @@ def self.generated_assets_full_path
end

def self.gem_available?(name)
Gem::Specification.find_by_name(name)
Gem::Specification.find_by_name(name).present?
rescue Gem::LoadError
false
rescue StandardError
Gem.available?(name)
Gem.available?(name).present?
end

def self.react_on_rails_pro?
Expand Down
2 changes: 1 addition & 1 deletion spec/dummy/app/views/pages/broken_app.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<%= react_component_hash("BrokenApp", prerender: true)['componentHtml'] %>
<%= react_component_hash("BrokenApp")['componentHtml'] %>

<hr/>

Expand Down
1 change: 0 additions & 1 deletion spec/dummy/app/views/pages/react_helmet.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@

<% react_helmet_app = react_component_hash("ReactHelmetApp",
prerender: true,
props: { helloWorldData: { name: "Mr. Server Side Rendering"}},
id: "react-helmet-0",
trace: true) %>
Expand Down