-
-
Notifications
You must be signed in to change notification settings - Fork 631
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Return markup hash form react component helper (#800)
* Allow to return object as a value of htmlResult from generator function on the server * Implement handling of Hash result of server_rendered_react_component_html in react_on_rails helper * Implement additional example page for server rendered HTML that shows usage of htmlResult object * Implement test for page title rendered in generator function on server * Add conditional title to dummy app layout * Implement rendering title string in generator function on server * Add test for conditional title to integration_spec * Refactor code in react_on_rails helper * Add some notes to API doc * Implement example with ReactHelmet * Now test page yields title rendered by ReactHelmet * Add tests with disabled JS to integration spec to ensure correct title rendering on server * Mark HTML strings other than component HTML (composed with props and console script) as html_safe * Fix for dummy application title * Pass params hash to #build_react_component_result_for_... methods * Now component key in renderedHtml object expected to match component registration key * Renamed example page and component registration * Renamed example page to react_helmet * Changed route to example page * Renamed component registration to ReactHelmetApp * Additional refactoring * Add docs and fix comments * Create additional-reading/react-helmet.mb * Fix comments for generator function * Update javascript-api.md * Update docs * Update README.md * Update CHANGELOG.md * Small fixes for JS tests from previous PR * Updated CHANGELOG.md and README.md * Assign params to local variables in #build_react_component_result_for_... methods * Now rendered HTML for React component goes under the constant key componentHtml * Implement constant key usage in #build_react_component_result_for_server_rendered_hash method of react_on_rails helper * Updated generator function for server rendering * Fixed additional-reading/react-helmet.md * Updated react_helmet.erb template accordingly * Updated javascript-api.md * Implement required params * Implement required params for #build_react_component_result_for... methods * Moved component_html_key to the constant * Returned back checking for "componentHtml" key presence * Updated CHANGELOG.md * Update react-helmet to v5.0.3 to resolve PropTypes warnings
- Loading branch information
Showing
19 changed files
with
303 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# Using React Helmet to build `<head>` content | ||
|
||
## Installation and general usage | ||
See https://github.com/nfl/react-helmet for details. Run `yarn add react-helmet` in your `client` directory to add this package to your application. | ||
|
||
## Example | ||
Here is what you need to do in order to configure your Rails application to work with **ReactHelmet**. | ||
|
||
Create generator function for server rendering like this: | ||
|
||
```javascript | ||
export default (props, _railsContext) => { | ||
const componentHtml = renderToString(<App {...props} />); | ||
const helmet = Helmet.renderStatic(); | ||
|
||
const renderedHtml = { | ||
componentHtml, | ||
title: helmet.title.toString(), | ||
}; | ||
return { renderedHtml }; | ||
}; | ||
``` | ||
You can add more **helmet** properties to result, e.g. **meta**, **base** and so on. See https://github.com/nfl/react-helmet#server-usage. | ||
|
||
Use regular component or generator function for client-side: | ||
|
||
```javascript | ||
export default (props, _railsContext) => ( | ||
<App {...props} /> | ||
); | ||
``` | ||
|
||
Put **ReactHelmet** component somewhere in your `<App>`: | ||
```javascript | ||
import { Helmet } from 'react-helmet'; | ||
|
||
const App = (props) => ( | ||
<div> | ||
<Helmet> | ||
<title>Custom page title</title> | ||
</Helmet> | ||
... | ||
</div> | ||
); | ||
|
||
export default App; | ||
``` | ||
Register your generators for client and server sides: | ||
|
||
```javascript | ||
import ReactHelmetApp from '../ReactHelmetClientApp'; | ||
|
||
ReactOnRails.register({ | ||
ReactHelmetApp | ||
}); | ||
``` | ||
```javascript | ||
import ReactHelmetApp from '../ReactHelmetServerApp'; | ||
|
||
ReactOnRails.register({ | ||
ReactHelmetApp | ||
}); | ||
``` | ||
Now when `react_component` helper will be called with **"ReactHelmetApp"** as a first argument it will return a hash instead of HTML string: | ||
```ruby | ||
<% react_helmet_app = react_component("ReactHelmetApp", prerender: true, props: { hello: "world" }, trace: true) %> | ||
<% content_for :title do %> | ||
<%= react_helmet_app['title'] %> | ||
<% end %> | ||
|
||
<%= react_helmet_app["componentHtml"] %> | ||
``` | ||
So now we're able to insert received title tag to our application layout: | ||
```ruby | ||
<%= yield(:title) if content_for?(:title) %> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<%= render "header" %> | ||
|
||
<% react_helmet_app = react_component("ReactHelmetApp", prerender: true, props: { hello: "world" }, trace: true) %> | ||
|
||
<% content_for :title do %> | ||
<%= react_helmet_app['title'] %> | ||
<% end %> | ||
|
||
<%= react_helmet_app["componentHtml"] %> | ||
|
||
<hr/> | ||
|
||
This page demonstrates a generator function that returns htmlResult as an object | ||
with HTML strings on the server side. It is useful to manipulating <head> | ||
content. Check out the page title! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import React from 'react'; | ||
import { Helmet } from 'react-helmet'; | ||
|
||
const EchoProps = (props) => ( | ||
<div> | ||
<Helmet> | ||
<title>Custom page title</title> | ||
</Helmet> | ||
Props: {JSON.stringify(props)} | ||
</div> | ||
); | ||
|
||
export default EchoProps; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// Top level component for simple client side only rendering | ||
import React from 'react'; | ||
import ReactHelmet from '../components/ReactHelmet'; | ||
|
||
export default (props, _railsContext) => ( | ||
<ReactHelmet {...props} /> | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Top level component for simple client side only rendering | ||
import React from 'react'; | ||
import { renderToString } from 'react-dom/server'; | ||
import { Helmet } from 'react-helmet'; | ||
import ReactHelmet from '../components/ReactHelmet'; | ||
|
||
/* | ||
* Export a function that takes the props and returns an object with { renderedHtml } | ||
* This example shows returning renderedHtml as an object itself that contains rendered | ||
* component markup and additional HTML strings. | ||
* | ||
* This is imported as "ReactHelmetApp" by "serverRegistration.jsx". Note that rendered | ||
* component markup must go under "componentHtml" key. | ||
*/ | ||
export default (props, _railsContext) => { | ||
const componentHtml = renderToString(<ReactHelmet {...props} />); | ||
const helmet = Helmet.renderStatic(); | ||
|
||
const renderedHtml = { | ||
componentHtml, | ||
title: helmet.title.toString(), | ||
}; | ||
return { renderedHtml }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.