2020-12-01 19:10:16 +01:00
|
|
|
import React, { useEffect, useContext } from 'react'
|
|
|
|
import { ScriptHTMLAttributes } from 'react'
|
2021-06-30 11:43:31 +02:00
|
|
|
import { HeadManagerContext } from '../shared/lib/head-manager-context'
|
2020-12-01 19:10:16 +01:00
|
|
|
import { DOMAttributeNames } from './head-manager'
|
2021-02-11 19:51:41 +01:00
|
|
|
import { requestIdleCallback } from './request-idle-callback'
|
2020-12-01 19:10:16 +01:00
|
|
|
|
|
|
|
const ScriptCache = new Map()
|
|
|
|
const LoadCache = new Set()
|
|
|
|
|
2021-07-07 18:35:50 +02:00
|
|
|
export interface ScriptProps extends ScriptHTMLAttributes<HTMLScriptElement> {
|
2021-05-12 13:37:57 +02:00
|
|
|
strategy?: 'afterInteractive' | 'lazyOnload' | 'beforeInteractive'
|
2020-12-01 19:10:16 +01:00
|
|
|
id?: string
|
|
|
|
onLoad?: () => void
|
|
|
|
onError?: () => void
|
|
|
|
children?: React.ReactNode
|
|
|
|
}
|
|
|
|
|
2021-07-07 18:35:50 +02:00
|
|
|
/**
|
|
|
|
* @deprecated Use `ScriptProps` instead.
|
|
|
|
*/
|
|
|
|
export type Props = ScriptProps
|
|
|
|
|
2021-03-02 20:17:33 +01:00
|
|
|
const ignoreProps = [
|
|
|
|
'onLoad',
|
|
|
|
'dangerouslySetInnerHTML',
|
|
|
|
'children',
|
|
|
|
'onError',
|
|
|
|
'strategy',
|
|
|
|
]
|
|
|
|
|
2021-07-07 18:35:50 +02:00
|
|
|
const loadScript = (props: ScriptProps): void => {
|
2020-12-01 19:10:16 +01:00
|
|
|
const {
|
2021-03-02 20:17:33 +01:00
|
|
|
src,
|
|
|
|
id,
|
2020-12-01 19:10:16 +01:00
|
|
|
onLoad = () => {},
|
|
|
|
dangerouslySetInnerHTML,
|
|
|
|
children = '',
|
|
|
|
onError,
|
|
|
|
} = props
|
|
|
|
|
|
|
|
const cacheKey = id || src
|
|
|
|
if (ScriptCache.has(src)) {
|
|
|
|
if (!LoadCache.has(cacheKey)) {
|
|
|
|
LoadCache.add(cacheKey)
|
|
|
|
// Execute onLoad since the script loading has begun
|
|
|
|
ScriptCache.get(src).then(onLoad, onError)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const el = document.createElement('script')
|
|
|
|
|
2021-06-18 00:43:25 +02:00
|
|
|
const loadPromise = new Promise<void>((resolve, reject) => {
|
2020-12-01 19:10:16 +01:00
|
|
|
el.addEventListener('load', function () {
|
|
|
|
resolve()
|
|
|
|
if (onLoad) {
|
|
|
|
onLoad.call(this)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
el.addEventListener('error', function () {
|
|
|
|
reject()
|
|
|
|
if (onError) {
|
|
|
|
onError()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
if (src) {
|
|
|
|
ScriptCache.set(src, loadPromise)
|
|
|
|
LoadCache.add(cacheKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dangerouslySetInnerHTML) {
|
|
|
|
el.innerHTML = dangerouslySetInnerHTML.__html || ''
|
|
|
|
} else if (children) {
|
|
|
|
el.textContent =
|
|
|
|
typeof children === 'string'
|
|
|
|
? children
|
|
|
|
: Array.isArray(children)
|
|
|
|
? children.join('')
|
|
|
|
: ''
|
|
|
|
} else if (src) {
|
|
|
|
el.src = src
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const [k, value] of Object.entries(props)) {
|
2021-03-02 20:17:33 +01:00
|
|
|
if (value === undefined || ignoreProps.includes(k)) {
|
2020-12-01 19:10:16 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
const attr = DOMAttributeNames[k] || k.toLowerCase()
|
|
|
|
el.setAttribute(attr, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
document.body.appendChild(el)
|
|
|
|
}
|
|
|
|
|
2021-07-07 18:35:50 +02:00
|
|
|
function handleClientScriptLoad(props: ScriptProps) {
|
2021-05-12 13:37:57 +02:00
|
|
|
const { strategy = 'afterInteractive' } = props
|
|
|
|
if (strategy === 'afterInteractive') {
|
2021-03-02 20:17:33 +01:00
|
|
|
loadScript(props)
|
2021-05-12 13:37:57 +02:00
|
|
|
} else if (strategy === 'lazyOnload') {
|
2021-03-02 20:17:33 +01:00
|
|
|
window.addEventListener('load', () => {
|
|
|
|
requestIdleCallback(() => loadScript(props))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-07 18:35:50 +02:00
|
|
|
function loadLazyScript(props: ScriptProps) {
|
2021-03-02 20:17:33 +01:00
|
|
|
if (document.readyState === 'complete') {
|
|
|
|
requestIdleCallback(() => loadScript(props))
|
|
|
|
} else {
|
|
|
|
window.addEventListener('load', () => {
|
|
|
|
requestIdleCallback(() => loadScript(props))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-07 18:35:50 +02:00
|
|
|
export function initScriptLoader(scriptLoaderItems: ScriptProps[]) {
|
2021-03-02 20:17:33 +01:00
|
|
|
scriptLoaderItems.forEach(handleClientScriptLoad)
|
|
|
|
}
|
|
|
|
|
2021-07-07 18:35:50 +02:00
|
|
|
function Script(props: ScriptProps): JSX.Element | null {
|
2020-12-01 19:10:16 +01:00
|
|
|
const {
|
|
|
|
src = '',
|
|
|
|
onLoad = () => {},
|
|
|
|
dangerouslySetInnerHTML,
|
2021-05-12 13:37:57 +02:00
|
|
|
strategy = 'afterInteractive',
|
2020-12-01 19:10:16 +01:00
|
|
|
onError,
|
|
|
|
...restProps
|
|
|
|
} = props
|
|
|
|
|
|
|
|
// Context is available only during SSR
|
|
|
|
const { updateScripts, scripts } = useContext(HeadManagerContext)
|
|
|
|
|
|
|
|
useEffect(() => {
|
2021-05-12 13:37:57 +02:00
|
|
|
if (strategy === 'afterInteractive') {
|
2020-12-01 19:10:16 +01:00
|
|
|
loadScript(props)
|
2021-05-12 13:37:57 +02:00
|
|
|
} else if (strategy === 'lazyOnload') {
|
2021-03-02 20:17:33 +01:00
|
|
|
loadLazyScript(props)
|
2020-12-01 19:10:16 +01:00
|
|
|
}
|
2021-03-02 20:17:33 +01:00
|
|
|
}, [props, strategy])
|
|
|
|
|
2021-05-12 13:37:57 +02:00
|
|
|
if (strategy === 'beforeInteractive') {
|
2020-12-01 19:10:16 +01:00
|
|
|
if (updateScripts) {
|
2021-05-12 13:37:57 +02:00
|
|
|
scripts.beforeInteractive = (scripts.beforeInteractive || []).concat([
|
2020-12-01 19:10:16 +01:00
|
|
|
{
|
|
|
|
src,
|
|
|
|
onLoad,
|
|
|
|
onError,
|
|
|
|
...restProps,
|
|
|
|
},
|
|
|
|
])
|
|
|
|
updateScripts(scripts)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null
|
|
|
|
}
|
2021-03-02 20:17:33 +01:00
|
|
|
|
|
|
|
export default Script
|