Skip to content

Commit a05c5af

Browse files
author
Robert Mosolgo
committed
Merge pull request #89 from meleyal/disable-turbolinks-cache
disable the Turbolinks cache, fixes #87
2 parents d0d62c5 + 06e4c2d commit a05c5af

File tree

2 files changed

+31
-17
lines changed

2 files changed

+31
-17
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,9 @@ You can use the `--harmony` or `--strip-types` options by adding a configuration
9595

9696
`react_ujs` will also scan DOM elements and call `React.unmountComponentAtNode` on page unload. If you want to disable this behavior, remove `data-react-class` attribute in `componentDidMount`.
9797

98-
To use `react_ujs`, simply `require` it after `react` (and after `turbolinks` if [Turbolinks](https://github.com/rails/turbolinks) is used):
98+
To use `react_ujs`, simply `require` it after `react` (and after `turbolinks` if [Turbolinks](https://github.com/rails/turbolinks) is used).
99+
100+
**Note:** _Turbolinks >= 2.4.0 is recommended. For older versions `react_ujs` will disable the Turbolinks cache to ensure components are correctly unmounted. See [#87](https://github.com/reactjs/react-rails/issues/87) for details._
99101

100102
```js
101103
// app/assets/javascripts/application.js

lib/assets/javascripts/react_ujs.js renamed to lib/assets/javascripts/react_ujs.js.erb

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,47 +4,48 @@
44
(function(document, window) {
55
// jQuery is optional. Use it to support legacy browsers.
66
var $ = (typeof window.jQuery !== 'undefined') && window.jQuery;
7-
7+
88
// create the namespace
99
window.ReactRailsUJS = {
1010
CLASS_NAME_ATTR: 'data-react-class',
1111
PROPS_ATTR: 'data-react-props',
12-
// helper method for the mount and unmount methods to find the
12+
RAILS_ENV_DEVELOPMENT: <%= Rails.env == "development" %>,
13+
// helper method for the mount and unmount methods to find the
1314
// `data-react-class` DOM elements
1415
findDOMNodes: function() {
15-
// we will use fully qualified paths as we do not bind the callbacks
16+
// we will use fully qualified paths as we do not bind the callbacks
1617
var selector = '[' + window.ReactRailsUJS.CLASS_NAME_ATTR + ']';
17-
18+
1819
if ($) {
1920
return $(selector);
2021
} else {
2122
return document.querySelectorAll(selector);
2223
}
2324
},
24-
25+
2526
mountComponents: function() {
2627
var nodes = window.ReactRailsUJS.findDOMNodes();
27-
28+
2829
for (var i = 0; i < nodes.length; ++i) {
2930
var node = nodes[i];
3031
var className = node.getAttribute(window.ReactRailsUJS.CLASS_NAME_ATTR);
31-
32+
3233
// Assume className is simple and can be found at top-level (window).
3334
// Fallback to eval to handle cases like 'My.React.ComponentName'.
3435
var constructor = window[className] || eval.call(window, className);
3536
var propsJson = node.getAttribute(window.ReactRailsUJS.PROPS_ATTR);
3637
var props = propsJson && JSON.parse(propsJson);
37-
38+
3839
React.render(React.createElement(constructor, props), node);
3940
}
4041
},
41-
42+
4243
unmountComponents: function() {
4344
var nodes = window.ReactRailsUJS.findDOMNodes();
44-
45+
4546
for (var i = 0; i < nodes.length; ++i) {
4647
var node = nodes[i];
47-
48+
4849
React.unmountComponentAtNode(node);
4950
// now remove the `data-react-class` wrapper as well
5051
node.parentElement && node.parentElement.removeChild(node);
@@ -55,26 +56,37 @@
5556
// functions not exposed publicly
5657
function handleTurbolinksEvents () {
5758
var handleEvent;
58-
59+
var unmountEvent;
60+
5961
if ($) {
6062
handleEvent = function(eventName, callback) {
6163
$(document).on(eventName, callback);
6264
};
63-
65+
6466
} else {
6567
handleEvent = function(eventName, callback) {
6668
document.addEventListener(eventName, callback);
6769
};
6870
}
71+
72+
if (Turbolinks.EVENTS) {
73+
unmountEvent = Turbolinks.EVENTS.BEFORE_UNLOAD;
74+
} else {
75+
unmountEvent = 'page:receive';
76+
Turbolinks.pagesCached(0);
77+
78+
if (window.ReactRailsUJS.RAILS_ENV_DEVELOPMENT) {
79+
console.warn('The Turbolinks cache has been disabled (Turbolinks >= 2.4.0 is recommended). See https://github.com/reactjs/react-rails/issues/87 for more information.');
80+
}
81+
}
6982
handleEvent('page:change', window.ReactRailsUJS.mountComponents);
70-
handleEvent('page:receive', window.ReactRailsUJS.unmountComponents);
83+
handleEvent(unmountEvent, window.ReactRailsUJS.unmountComponents);
7184
}
7285

7386
function handleNativeEvents() {
74-
if ($) {
87+
if ($) {
7588
$(window.ReactRailsUJS.mountComponents);
7689
$(window).unload(window.ReactRailsUJS.unmountComponents);
77-
7890
} else {
7991
document.addEventListener('DOMContentLoaded', window.ReactRailsUJS.mountComponents);
8092
window.addEventListener('unload', window.ReactRailsUJS.unmountComponents);

0 commit comments

Comments
 (0)