Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(components/script/loader): add isDefer prop to load scripts deferred #444

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions components/script/loader/demo/playground
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<div>
<h3>Demo</h3>
<p>
<ScriptLoader
src={src}
verifier={verifier}
isDefer
render={renderDefer}
/>
</p>

<p>
<ScriptLoader
src={src}
Expand Down
19 changes: 15 additions & 4 deletions components/script/loader/src/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,19 @@ const scriptPromises = []
* @param {string} params.src
* @param {function} params.verifier
* @param {boolean} params.isAsync
* @param {boolean} params.isDefer
* @param {number} params.detectionDelay
* @param {string} params.stylesheet
* @return {Promise}
*/
const loadScript = ({src, verifier, isAsync, detectionDelay, stylesheet}) => {
const loadScript = ({
src,
verifier,
isAsync,
isDefer,
detectionDelay,
stylesheet
}) => {
scriptPromises[src] =
scriptPromises[src] ||
new Promise((resolve, reject) => {
Expand All @@ -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)
})
Expand All @@ -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)
}
Expand Down
8 changes: 7 additions & 1 deletion components/script/loader/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const ScriptLoader = ({
src,
verifier,
isAsync = true,
isDefer = false,
detectionDelay = 5000,
onTimeout = () => {},
stylesheet,
Expand All @@ -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))
}
Expand Down Expand Up @@ -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)
*/
Expand Down