Last active
January 20, 2019 18:01
-
-
Save hozefaj/009f98a4b3e23d922342f6cfeb833b79 to your computer and use it in GitHub Desktop.
delay loading for react bundle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// defer loading of non-essential JS until DOM loaded event | |
function() { | |
window.addEventListener("load", function() { | |
var s, t; | |
s = document.createElement("script"); | |
s.type = "text/javascript"; | |
s.src = | |
"https://www.paypalobjects.com/digitalassets/c/website/marketing/global/kui/js/opinionLab-2.1.0.js"; | |
t = document.getElementsByTagName("body")[0]; | |
t.appendChild(s); | |
}); | |
}); | |
// defer loading for the client side JS after DOM loaded event | |
// this is because we are doing SSR | |
function(){ | |
function loadScript(src, cb) { | |
var s, r, t; | |
r = false; | |
s = document.createElement("script"); | |
s.type = "text/javascript"; | |
s.src = src; | |
s.onload = s.onreadystatechange = function() { | |
if (!r && (!this.readyState || this.readyState == "complete")) { | |
r = true; | |
cb(); | |
} | |
}; | |
t = document.getElementsByTagName("body")[0]; | |
t.appendChild(s); | |
} | |
function mountCode() { | |
if (typeof window !== "undefined" && window.document && window.document.createElement) { | |
var appElement = React.createElement(window.PageBundle.default, {modelData: window.modelData}); | |
ReactDOM.hydrate(appElement, window.document.getElementById("app-element-mountpoint")); | |
} | |
} | |
window.addEventListener('load', function() { | |
loadScript("https://www.paypalobjects.com/eboxapps/js/5f/7981d3071109ab488093d2ba7e4ca87ff7629e.js", mountCode); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment