Skip to content

Commit

Permalink
expose reactOnRailsPageLoaded to ReactOnRails
Browse files Browse the repository at this point in the history
  • Loading branch information
SqueezedLight committed Mar 22, 2016
1 parent 6d50159 commit d73954a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
18 changes: 18 additions & 0 deletions docs/additional-reading/turbolinks.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,24 @@ Turbolinks 5 is now being supported. React on Rails will automatically detect wh

For more information on Turbolinks 5: [https://github.com/turbolinks/turbolinks](https://github.com/turbolinks/turbolinks)

### async script loading
Generally async script loading can be done like:
```erb
<%= javascript_include_tag 'application', async: Rails.env.production? %>
```
If you use ```document.addEventListener("turbolinks:load", function() {...});``` somewhere in your code, you will notice, that Turbolinks 5 does not fire ```turbolinks:load``` on initial page load. A quick workaround is to use ```defer``` instead of ```async```:
```erb
<%= javascript_include_tag 'application', defer: Rails.env.production? %>
```
More information on this issue can be found here: https://github.com/turbolinks/turbolinks/issues/28

When loading your scripts asynchronously you may experience, that your Components are not registered correctly. Call ```ReactOnRails.reactOnRailsPageLoaded()``` to re-initialize like so:
```
document.addEventListener("turbolinks:load", function() {
ReactOnRails.reactOnRailsPageLoaded();
});
```

## Troubleshooting
To turn on tracing of Turbolinks events, put this in your registration file, where you register your components.

Expand Down
8 changes: 6 additions & 2 deletions node_package/src/ReactOnRails.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import clientStartup from './clientStartup';
import * as ClientStartup from './clientStartup';
import handleError from './handleError';
import ComponentRegistry from './ComponentRegistry';
import StoreRegistry from './StoreRegistry';
Expand Down Expand Up @@ -63,6 +63,10 @@ ctx.ReactOnRails = {
}
},

reactOnRailsPageLoaded() {
ClientStartup.reactOnRailsPageLoaded();
},

////////////////////////////////////////////////////////////////////////////////
// INTERNALLY USED APIs
////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -175,6 +179,6 @@ ctx.ReactOnRails = {

ReactOnRails.resetOptions();

clientStartup(ctx);
ClientStartup.clientStartup(ctx);

export default ctx.ReactOnRails;
4 changes: 2 additions & 2 deletions node_package/src/clientStartup.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ You should return a React.Component always for the client side entry point.`);
}
}

function reactOnRailsPageLoaded() {
export function reactOnRailsPageLoaded() {
debugTurbolinks('reactOnRailsPageLoaded');

forEachStore(initializeStore);
Expand All @@ -103,7 +103,7 @@ function reactOnRailsPageUnloaded() {
forEachComponent(unmount);
}

export default function clientStartup(context) {
export function clientStartup(context) {
const document = context.document;

// Check if server rendering
Expand Down

0 comments on commit d73954a

Please sign in to comment.