Skip to content

Commit 174a3ea

Browse files
committed
Address some of the comments
1 parent 6bcf72f commit 174a3ea

File tree

5 files changed

+51
-31
lines changed

5 files changed

+51
-31
lines changed

CHANGELOG.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ All notable changes to this project will be documented in this file. Items under
44
Contributors: please follow the recommendations outlined at [keepachangelog.com](http://keepachangelog.com/). Please use the existing headings and styling as a guide, and add a link for the version diff at the bottom of the file. Also, please update the `Unreleased` link to compare to the latest release version.
55

66
## [Unreleased]
7+
8+
## [6.2.0]
79
##### Added
8-
- New API registerRenderer which allows a user to manually render their app to the DOM by [jtibbertsma](https://github.com/jtibbertsma)
10+
- New API registerRenderer which allows a user to manually render their app to the DOM [#581](https://github.com/shakacode/react_on_rails/pull/581) by [jtibbertsma](https://github.com/jtibbertsma).
911

1012
## [6.1.2] 2016-10-24
1113
##### Fixed
@@ -376,7 +378,8 @@ Best done with Object destructing:
376378
##### Fixed
377379
- Fix several generator related issues.
378380

379-
[Unreleased]: https://github.com/shakacode/react_on_rails/compare/6.1.2...master
381+
[Unreleased]: https://github.com/shakacode/react_on_rails/compare/6.2.0...master
382+
[6.2.0]: https://github.com/shakacode/react_on_rails/compare/6.1.2...6.2.0
380383
[6.1.2]: https://github.com/shakacode/react_on_rails/compare/6.1.1...6.1.2
381384
[6.1.1]: https://github.com/shakacode/react_on_rails/compare/6.1.0...6.1.1
382385
[6.1.0]: https://github.com/shakacode/react_on_rails/compare/6.0.5...6.1.0

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,7 @@ If you are using [jquery-ujs](https://github.com/rails/jquery-ujs) for AJAX call
454454

455455
1. [React on Rails docs for react-router](docs/additional-reading/react-router.md)
456456
1. Examples in [spec/dummy/app/views/react_router](spec/dummy/app/views/react_router) and follow to the JavaScript code in the [spec/dummy/client/app/startup/ServerRouterApp.jsx](spec/dummy/client/app/startup/ServerRouterApp.jsx).
457+
1. [Code Splitting docs](docs/additional-reading/code-splitting.md) for information about how to set up code splitting for server rendered routes.
457458

458459
## Deployment
459460
* Version 6.0 puts the necessary precompile steps automatically in the rake precompile step. You can, however, disable this by setting certain values to nil in the [config/initializers/react_on_rails.rb](spec/dummy/config/initializers/react_on_rails.rb).
@@ -484,6 +485,7 @@ Node.js can be used as the backend for server-side rendering instead of [execJS]
484485
+ [Developing with the Webpack Dev Server](docs/additional-reading/webpack-dev-server.md)
485486
+ [Node Server Rendering](docs/additional-reading/node-server-rendering.md)
486487
+ [Server Rendering Tips](docs/additional-reading/server-rendering-tips.md)
488+
+ [Code Splitting](docs/additional-reading/code-splitting.md)
487489

488490
+ **Development**
489491
+ [React on Rails Basic Installation Tutorial](docs/tutorial.md) ([live demo](https://hello-react-on-rails.herokuapp.com))

docs/additional-reading/code-splitting.md

+24-9
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ What is code splitting? From the webpack documentation:
88

99
Let's say you're requesting a page that needs to fetch a code chunk from the server before it's able to render. If you do all your rendering on the client side, you don't have to do anything special. However, if the page is rendered on the server, you'll find that React will spit out the following error:
1010

11-
```
12-
Warning: React attempted to reuse markup in a container but the checksum was invalid. This generally means that you are using server rendering and the markup generated on the server was not what the client was expecting. React injected new markup to compensate which works but you have lost many of the benefits of server rendering. Instead, figure out why the markup being generated is different on the client or server:
13-
(client) <!-- react-empty: 1 -
14-
(server) <div data-reactroot="
15-
```
11+
> Warning: React attempted to reuse markup in a container but the checksum was invalid. This generally means that you are using server rendering and the markup generated on the server was not what the client was expecting. React injected new markup to compensate which works but you have lost many of the benefits of server rendering. Instead, figure out why the markup being generated is different on the client or server:
12+
13+
> (client) <!-- react-empty: 1 -
14+
15+
> (server) <div data-reactroot="
1616
<!--This comment is here because the comment beginning on line 13 messes up Sublime's markdown parsing-->
1717
1818
Different markup is generated on the client than on the server. Why does this happen? When you register a component with `ReactOnRails.register`, react on rails will render the component as soon as the page loads. However, react-router renders a comment while waiting for the code chunk to be fetched from the server. This means that react will tear all of the server rendered code out of the DOM, and then rerender it a moment later once the code chunk arrives from the server, defeating most of the purpose of server rendering.
@@ -23,14 +23,14 @@ To prevent this, you have to wait until the code chunk is fetched before doing t
2323

2424
Here's an example of how you might use this in practice:
2525

26-
page.html.erb
26+
#### page.html.erb
2727
```erb
2828
<%= redux_store_hydration_data %>
2929
<%= react_component("NavigationApp", prerender: true) %>
3030
<%= react_component("RouterApp", prerender: true) %>
3131
```
3232

33-
clientRegistration.js
33+
#### clientRegistration.js
3434
```js
3535
import ReactOnRails from 'react-on-rails';
3636
import NavigationApp from './NavigationApp';
@@ -42,7 +42,22 @@ ReactOnRails.register({NavigationApp});
4242
ReactOnRails.registerRenderer({RouterApp});
4343
```
4444

45-
RouterAppRenderer.jsx
45+
#### serverRegistration.js
46+
```js
47+
import ReactOnRails from 'react-on-rails';
48+
import NavigationApp from './NavigationApp';
49+
import RouterApp from './RouterAppServer';
50+
import applicationStore from '../store/applicationStore';
51+
52+
ReactOnRails.registerStore({applicationStore});
53+
ReactOnRails.register({
54+
NavigationApp,
55+
RouterApp,
56+
});
57+
```
58+
Note that you should not use `registerRenderer` on the server. Instead, use `register` like normal. For an example of how to set up an app for server rendering, see the [react router docs](react-router.md).
59+
60+
#### RouterAppRenderer.jsx
4661
```jsx
4762
import ReactOnRails from 'react-on-rails';
4863
import React from 'react';
@@ -82,7 +97,7 @@ Note that in page.html.erb, we call `react_component` in the exact same way as i
8297

8398
### Caveats
8499

85-
If you're going to try to do code splitting with server rendered routes, it's important that you have seperate webpack configurations for client and server. The code splitting happens for the client, but the server should one big file.
100+
If you're going to try to do code splitting with server rendered routes, you'll probably need to use seperate route definitions for client and server to prevent code splitting from happening for the server bundle. The server bundle should be one file containing all the JavaScript code.
86101

87102
The reason is we do server rendering with ExecJS, which is not capable of doing anything asynchronous. See [this issue](https://github.com/shakacode/react_on_rails/issues/477) for a discussion.
88103

docs/api/javascript-api.md

+9-9
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ The best source of docs is the main [ReactOnRails.js](../../node_package/src/Rea
1717
*/
1818
registerStore(stores)
1919

20+
/**
21+
* Allows registration of renderers. A renderer is a function that accept three args:
22+
* props, railsContext, and domNodeId, and is responsible for rendering a component
23+
* to the DOM. Not available on the server. For one possible use case see:
24+
https://github.com/shakacode/react_on_rails/blob/master/docs/additional-reading/code-splitting.md
25+
* @param renderers (key is component name, value is renderer)
26+
*/
27+
registerRenderer(renderers)
28+
2029
/**
2130
* Allows retrieval of the store by name. This store will be hydrated by any Rails form props.
2231
* Pass optional param throwIfMissing = false if you want to use this call to get back null if the
@@ -57,13 +66,4 @@ The best source of docs is the main [ReactOnRails.js](../../node_package/src/Rea
5766
*/
5867

5968
authenticityHeaders(otherHeaders = {})
60-
61-
/**
62-
* Allows registration of renderers. A renderer is a function that accept three args:
63-
* props, railsContext, and domNodeId, and is responsible for rendering a component
64-
* to the DOM. Not available on the server. For one possible use case see:
65-
https://github.com/shakacode/react_on_rails/blob/master/docs/additional-reading/code-splitting.md
66-
* @param renderers (key is component name, value is renderer)
67-
*/
68-
registerRenderer(renderers)
6969
```

node_package/src/ReactOnRails.js

+11-11
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,17 @@ ctx.ReactOnRails = {
4141
StoreRegistry.register(stores);
4242
},
4343

44+
/**
45+
* Allows registration of renderers. A renderer is a function that accept three args:
46+
* props, railsContext, and domNodeId, and is responsible for rendering a component
47+
* to the DOM. Not available on the server. For one possible use case see:
48+
https://github.com/shakacode/react_on_rails/blob/master/docs/additional-reading/code-splitting.md
49+
* @param renderers (key is component name, value is renderer)
50+
*/
51+
registerRenderer(renderers) {
52+
ComponentRegistry.registerRenderer(renderers);
53+
},
54+
4455
/**
4556
* Allows retrieval of the store by name. This store will be hydrated by any Rails form props.
4657
* Pass optional param throwIfMissing = false if you want to use this call to get back null if the
@@ -101,17 +112,6 @@ ctx.ReactOnRails = {
101112
return Authenticity.authenticityHeaders(otherHeaders);
102113
},
103114

104-
/**
105-
* Allows registration of renderers. A renderer is a function that accept three args:
106-
* props, railsContext, and domNodeId, and is responsible for rendering a component
107-
* to the DOM. Not available on the server. For one possible use case see:
108-
https://github.com/shakacode/react_on_rails/blob/master/docs/additional-reading/code-splitting.md
109-
* @param renderers (key is component name, value is renderer)
110-
*/
111-
registerRenderer(renderers) {
112-
ComponentRegistry.registerRenderer(renderers);
113-
},
114-
115115
// /////////////////////////////////////////////////////////////////////////////
116116
// INTERNALLY USED APIs
117117
// /////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)