Skip to content

Commit bea1190

Browse files
committed
Handle <script async> transparently #1099
Fixes #290, transparently handling <script async> tag. If the document was already loaded, the DOMContentLoaded event handler no longer fires, so we just execute it directly, the same way as jQuery does in $.ready(). Co-authored-by: Alexey Makhotkin
1 parent 9436605 commit bea1190

File tree

2 files changed

+29
-12
lines changed

2 files changed

+29
-12
lines changed

CHANGELOG.md

+8-7
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,24 @@ See [Upgrading React on Rails](./docs/basics/upgrading-react-on-rails.md) for mo
99
Changes since last non-beta release.
1010

1111
*Please add entries here for your pull requests that are not yet released.*
12-
### [11.0.9] - 2018-05-22
12+
13+
### [11.0.9] - 2018-06-24
14+
- Handle <script async> for Webpack bundle transparently. Closes [issue #290](https://github.com/shakacode/react_on_rails/issues/290) [PR 1099](https://github.com/shakacode/react_on_rails/pull/1099) by [squadette](https://github.com/squadette).
15+
16+
### [11.0.8] - 2018-06-15
1317
#### Fixed
1418
- HashWithIndifferent access for props threw if used for props. [PR 1100](https://github.com/shakacode/react_on_rails/pull/1100) by [justin808](https://github.com/justin808).
1519
- Test helper for detecting stale bundles did not properly handle the case of a server-bundle.js without a hash.[PR 1102](https://github.com/shakacode/react_on_rails/pull/1102) by [justin808](https://github.com/justin808).
16-
17-
### [11.0.8] - 2018-05-22
18-
#### Fixed
1920
- Fix test helper determination of stale assets. [PR 1093](https://github.com/shakacode/react_on_rails/pull/1093) by [justin808](https://github.com/justin808).
2021

2122
#### Changed
2223
- Document how to manually rehydrate XHR-substituted components on client side. [PR 1095](https://github.com/shakacode/react_on_rails/pull/1095) by [hchevalier](https://github.com/hchevalier).
2324

24-
### [11.0.7] - 2018-05-11
25+
### [11.0.7] - 2018-05-16
2526
#### Fixed
2627
- Fix npm publshing. [PR 1090](https://github.com/shakacode/react_on_rails/pull/1090) by [justin808](https://github.com/justin808).
2728

28-
### [11.0.6] - 2018-05-11
29+
### [11.0.6] - 2018-05-15
2930
#### Changed
3031
- Even more detailed errors for Honeybadger and Sentry when there's a JSON parse error on server rendering. [PR 1086](https://github.com/shakacode/react_on_rails/pull/1086) by [justin808](https://github.com/justin808).
3132

@@ -777,7 +778,7 @@ Best done with Object destructing:
777778
- Fix several generator related issues.
778779

779780
[Unreleased]: https://github.com/shakacode/react_on_rails/compare/11.0.9...master
780-
[11.0.8]: https://github.com/shakacode/react_on_rails/compare/11.0.8...11.0.9
781+
[11.0.9]: https://github.com/shakacode/react_on_rails/compare/11.0.8...11.0.9
781782
[11.0.8]: https://github.com/shakacode/react_on_rails/compare/11.0.7...11.0.8
782783
[11.0.7]: https://github.com/shakacode/react_on_rails/compare/11.0.6...11.0.7
783784
[11.0.6]: https://github.com/shakacode/react_on_rails/compare/11.0.5...11.0.6

node_package/src/clientStartup.js

+21-5
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,27 @@ export function clientStartup(context) {
196196

197197
debugTurbolinks('Adding DOMContentLoaded event to install event listeners.');
198198

199+
window.setTimeout(() => {
200+
if (!turbolinksInstalled() || !turbolinksSupported()) {
201+
if (document.readyState === 'complete' ||
202+
(document.readyState !== 'loading' && !document.documentElement.doScroll)
203+
) {
204+
debugTurbolinks(
205+
'NOT USING TURBOLINKS: DOM is already loaded, calling reactOnRailsPageLoaded',
206+
);
207+
208+
reactOnRailsPageLoaded();
209+
} else {
210+
document.addEventListener('DOMContentLoaded', () => {
211+
debugTurbolinks(
212+
'NOT USING TURBOLINKS: DOMContentLoaded event, calling reactOnRailsPageLoaded',
213+
);
214+
reactOnRailsPageLoaded();
215+
});
216+
}
217+
}
218+
});
219+
199220
document.addEventListener('DOMContentLoaded', () => {
200221
// Install listeners when running on the client (browser).
201222
// We must do this check for turbolinks AFTER the document is loaded because we load the
@@ -216,11 +237,6 @@ export function clientStartup(context) {
216237
document.addEventListener('page:before-unload', reactOnRailsPageUnloaded);
217238
document.addEventListener('page:change', reactOnRailsPageLoaded);
218239
}
219-
} else {
220-
debugTurbolinks(
221-
'NOT USING TURBOLINKS: DOMContentLoaded event, calling reactOnRailsPageLoaded',
222-
);
223-
reactOnRailsPageLoaded();
224240
}
225241
});
226242
}

0 commit comments

Comments
 (0)