Skip to content

Commit

Permalink
Fix failure to render during interactive readyState
Browse files Browse the repository at this point in the history
  • Loading branch information
ryan.kelley committed Oct 3, 2018
1 parent 3283f3d commit 7bde2d1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 41 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Please follow the recommendations outlined at [keepachangelog.com](http://keepac
Changes since last non-beta release.

*Please add entries here for your pull requests that are not yet released.*
- Fix additional cases of client startup failing during interactive readyState

### [11.1.5] - 2018-09-12
#### Fixed
Expand Down
72 changes: 31 additions & 41 deletions node_package/src/clientStartup.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,32 @@ function reactOnRailsPageUnloaded() {
forEachComponent(unmount);
}

function renderInit() {
// Install listeners when running on the client (browser).
// We must do this check for turbolinks AFTER the document is loaded because we load the
// Webpack bundles first.
if (!turbolinksInstalled() || !turbolinksSupported()) {
debugTurbolinks('NOT USING TURBOLINKS: calling reactOnRailsPageLoaded');
reactOnRailsPageLoaded();
return;
}

if (turbolinksVersion5()) {
debugTurbolinks(
'USING TURBOLINKS 5: document added event listeners ' +
'turbolinks:before-render and turbolinks:render.');
document.addEventListener('turbolinks:before-render', reactOnRailsPageUnloaded);
document.addEventListener('turbolinks:render', reactOnRailsPageLoaded);
reactOnRailsPageLoaded();
} else {
debugTurbolinks(
'USING TURBOLINKS 2: document added event listeners page:before-unload and ' +
'page:change.');
document.addEventListener('page:before-unload', reactOnRailsPageUnloaded);
document.addEventListener('page:change', reactOnRailsPageLoaded);
}
}

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

Expand All @@ -196,45 +222,9 @@ export function clientStartup(context) {

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

window.setTimeout(() => {
if (!turbolinksInstalled() || !turbolinksSupported()) {
if (document.readyState === 'complete') {
debugTurbolinks(
'NOT USING TURBOLINKS: DOM is already loaded, calling reactOnRailsPageLoaded',
);

reactOnRailsPageLoaded();
} else {
document.addEventListener('DOMContentLoaded', () => {
debugTurbolinks(
'NOT USING TURBOLINKS: DOMContentLoaded event, calling reactOnRailsPageLoaded',
);
reactOnRailsPageLoaded();
});
}
}
});

document.addEventListener('DOMContentLoaded', () => {
// Install listeners when running on the client (browser).
// We must do this check for turbolinks AFTER the document is loaded because we load the
// Webpack bundles first.

if (turbolinksInstalled() && turbolinksSupported()) {
if (turbolinksVersion5()) {
debugTurbolinks(
'USING TURBOLINKS 5: document added event listeners ' +
'turbolinks:before-render and turbolinks:render.');
document.addEventListener('turbolinks:before-render', reactOnRailsPageUnloaded);
document.addEventListener('turbolinks:render', reactOnRailsPageLoaded);
reactOnRailsPageLoaded();
} else {
debugTurbolinks(
'USING TURBOLINKS 2: document added event listeners page:before-unload and ' +
'page:change.');
document.addEventListener('page:before-unload', reactOnRailsPageUnloaded);
document.addEventListener('page:change', reactOnRailsPageLoaded);
}
}
});
if (document.readyState === 'complete') {
window.setTimeout(renderInit);
} else {
document.addEventListener('DOMContentLoaded', renderInit);
}
}

0 comments on commit 7bde2d1

Please sign in to comment.