You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Mostly additions to code-splitting.md. Also linked to the
renderer function section in the readme from the error message
in serverRenderReactComponent. Added a line to that section in
README to address the error.
Copy file name to clipboardExpand all lines: README.md
+2
Original file line number
Diff line number
Diff line change
@@ -316,6 +316,8 @@ Why would you create a function that returns a React component? For example, you
316
316
#### Renderer Functions
317
317
A renderer function is a generator function that accepts three arguments: `(props, railsContext, domNodeId) => { ... }`. Instead of returning a React component, a renderer is responsible for calling `ReactDOM.render` to manually render a React component into the dom. Why would you want to call `ReactDOM.render` yourself? One possible use case is [code splitting](docs/additional-reading/code-splitting.md).
318
318
319
+
Renderer functions are not meant to be used on the server, since there's no DOM on the server. Instead, use a generator function. Attempting to server render with a renderer function will cause an error.
320
+
319
321
## ReactOnRails View Helpers API
320
322
Once the bundled files have been generated in your `app/assets/webpack` folder and you have exposed your components globally, you will want to run your code in your Rails views using the included helper method.
Note that you should not register a renderer on the server, since there won't be a domNodeId when we're server rendering. For an example of how to set up an app for server rendering, see the [react router docs](react-router.md).
65
+
Note that you should not register a renderer on the server, since there won't be a domNodeId when we're server rendering. Note that the `RouterApp` imported by `serverRegistration.js` is from a different file. For an example of how to set up an app for server rendering, see the [react router docs](react-router.md).
61
66
62
67
#### RouterAppRenderer.jsx
63
68
```jsx
@@ -100,9 +105,22 @@ The idea is that match from react-router is async; it fetches the component usin
100
105
101
106
The server render matches the deferred render because the server bundle is a single file, and so it doesn't need to wait for anything to be fetched.
102
107
108
+
### Working Example
109
+
110
+
There's an implemented example of code splitting in the `spec/dummy` folder of this repository.
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.
123
+
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. This will require you to have seperate webpack configurations for client and server.
106
124
107
125
The reason is we do server rendering with ExecJS, which is not capable of doing anything asynchronous. It would be impossible to asyncronously fetch a code chunk while server rendering. See [this issue](https://github.com/shakacode/react_on_rails/issues/477) for a discussion.
108
126
@@ -123,3 +141,13 @@ config = {
123
141
This causes Webpack to prepend the code chunk filename with `/assets/` in the request url. The react on rails sets up the webpack config to put webpack bundles in `app/assets/javascripts/webpack`, and modifies `config/initializers/assets.rb` so that rails detects the bundles. This means that when we prepend the request url with `/assets/`, rails will know what webpack is asking for.
124
142
125
143
See [rails-assets.md](./rails-assets.md) to learn more about static assets.
144
+
145
+
If you forget to set the public path, webpack will request the code chunk at `/{filename}`. This will cause the request to be handled by the Rails router, which will send back a 404 response, assuming that you don't have a catch-all route. In your javascript console, you'll get the following error:
146
+
147
+
> GET http://localhost:3000/1.1-bundle.js
148
+
149
+
You'll also see the following in your Rails development log:
150
+
151
+
> Started GET "/1.1-bundle.js" for 127.0.0.1 at 2016-11-29 15:21:55 -0800
152
+
>
153
+
> ActionController::RoutingError (No route matches [GET] "/1.1-bundle.js")
0 commit comments