diff --git a/components/script/loader/demo/playground b/components/script/loader/demo/playground index 5b20791ac..11f5b9eea 100644 --- a/components/script/loader/demo/playground +++ b/components/script/loader/demo/playground @@ -2,9 +2,20 @@ const src = 'https://cdns.gigya.com/JS/gigya.js?apikey=3_mguHvyO40mAI8ozGJLSFiYAMicGHRYkOaz56NX5uvYlRjrI5qHdfBxw25Q-SYYz8' // This API key is a demo key, please don't use it in your site :D const verifier = () => window && window.gigya const render = () => 'Gigya is loaded! You can now interact with the sdk functions!!' +const renderDefer = () => 'Gigya is loaded with defer attribute! You can now interact with the sdk functions!!' return (
+

Demo

+

+ +

+

{ +const loadScript = ({ + src, + verifier, + isAsync, + isDefer, + detectionDelay, + stylesheet +}) => { scriptPromises[src] = scriptPromises[src] || new Promise((resolve, reject) => { @@ -21,7 +29,7 @@ const loadScript = ({src, verifier, isAsync, detectionDelay, stylesheet}) => { return } - injectScript({src, isAsync}) + injectScript({src, isAsync, isDefer}) stylesheet && injectStyles(stylesheet) waitUntil(verifier, resolve, reject, detectionDelay) }) @@ -34,13 +42,16 @@ const loadScript = ({src, verifier, isAsync, detectionDelay, stylesheet}) => { * @param {object} params * @param {string} params.src * @param {boolean} params.isAsync + * @param {boolean} params.isDefer * @return {void} */ -const injectScript = ({src, isAsync}) => { +const injectScript = ({src, isAsync, isDefer}) => { const script = document.createElement('script') script.src = src - script.async = isAsync + script.defer = isDefer + + if (!isDefer) script.async = isAsync document.body.appendChild(script) } diff --git a/components/script/loader/src/index.js b/components/script/loader/src/index.js index 7a6642ebc..63af18c4f 100644 --- a/components/script/loader/src/index.js +++ b/components/script/loader/src/index.js @@ -7,6 +7,7 @@ const ScriptLoader = ({ src, verifier, isAsync = true, + isDefer = false, detectionDelay = 5000, onTimeout = () => {}, stylesheet, @@ -17,7 +18,7 @@ const ScriptLoader = ({ const [timeout, seTimeout] = useState(false) const initLoad = () => { - loadScript({src, verifier, isAsync, detectionDelay, stylesheet}) + loadScript({src, verifier, isAsync, isDefer, detectionDelay, stylesheet}) .then(() => setReadyToRender(true)) .catch(() => seTimeout(true)) } @@ -59,6 +60,11 @@ ScriptLoader.propTypes = { * If the script should be marked as async or not */ isAsync: PropTypes.bool, + /** + * If the script should be marked as defer or not. + * If isDefer is true, isAsync will be false + */ + isDefer: PropTypes.bool, /** * Detection delay time (in miliseconds) */