Skip to content

Commit

Permalink
Fallback to setInterval if DOMContentLoaded fails
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanseddon committed Jul 6, 2022
1 parent 41c94d3 commit 28584e8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/Frame.jsx
Expand Up @@ -56,7 +56,10 @@ export class Frame extends Component {
componentWillUnmount() {
this._isMounted = false;

this.nodeRef.current.removeEventListener('load', this.handleLoad);
this.nodeRef.current.removeEventListener(
'DOMContentLoaded',
this.handleLoad
);
}

getDoc() {
Expand All @@ -83,12 +86,19 @@ export class Frame extends Component {
};

handleLoad = () => {
clearInterval(this.loadCheck);
// Bail update as some browsers will trigger on both DOMContentLoaded & onLoad ala firefox
if (!this.state.iframeLoaded) {
this.setState({ iframeLoaded: true });
}
};

// In certain situations on a cold cache DOMContentLoaded never gets called
// fallback to an interval to check if that's the case
loadCheck = setInterval(function loadCheckCallback() {
this.handleLoad();
}, 500);

renderFrameContents() {
if (!this._isMounted) {
return null;
Expand Down Expand Up @@ -135,6 +145,7 @@ export class Frame extends Component {
delete props.contentDidMount;
delete props.contentDidUpdate;
delete props.forwardedRef;

return (
<iframe {...props} ref={this.setRef} onLoad={this.handleLoad}>
{this.state.iframeLoaded && this.renderFrameContents()}
Expand Down
5 changes: 5 additions & 0 deletions webpack.config.js
Expand Up @@ -24,5 +24,10 @@ module.exports = {
loaders: [
{ test: /\.js(x|)/, loaders: ['babel-loader'], exclude: /node_modules/ }
]
},
devServer: {
headers: {
'Cache-Control': 'max-age=10'
}
}
};

0 comments on commit 28584e8

Please sign in to comment.