"use strict";var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");exports.__esModule=true;exports.initScriptLoader=initScriptLoader;exports.default=void 0;var _extends2=_interopRequireDefault(require("@babel/runtime/helpers/extends"));var _objectWithoutPropertiesLoose2=_interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose"));var _react=require("react");var _headManagerContext=require("../next-server/lib/head-manager-context");var _headManager=require("./head-manager");var _requestIdleCallback=require("./request-idle-callback");const ScriptCache=new Map();const LoadCache=new Set();const ignoreProps=['onLoad','dangerouslySetInnerHTML','children','onError','strategy'];const loadScript=props=>{const{src,id,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');const loadPromise=new Promise((resolve,reject)=>{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)){if(value===undefined||ignoreProps.includes(k)){continue;}const attr=_headManager.DOMAttributeNames[k]||k.toLowerCase();el.setAttribute(attr,value);}document.body.appendChild(el);};function handleClientScriptLoad(props){const{strategy='afterInteractive'}=props;if(strategy==='afterInteractive'){loadScript(props);}else if(strategy==='lazyOnload'){window.addEventListener('load',()=>{(0,_requestIdleCallback.requestIdleCallback)(()=>loadScript(props));});}}function loadLazyScript(props){if(document.readyState==='complete'){(0,_requestIdleCallback.requestIdleCallback)(()=>loadScript(props));}else{window.addEventListener('load',()=>{(0,_requestIdleCallback.requestIdleCallback)(()=>loadScript(props));});}}function initScriptLoader(scriptLoaderItems){scriptLoaderItems.forEach(handleClientScriptLoad);}function Script(props){const{src='',onLoad=()=>{},strategy='afterInteractive',onError}=props,restProps=(0,_objectWithoutPropertiesLoose2.default)(props,["src","onLoad","dangerouslySetInnerHTML","strategy","onError"]);// Context is available only during SSR
const{updateScripts,scripts}=(0,_react.useContext)(_headManagerContext.HeadManagerContext);(0,_react.useEffect)(()=>{if(strategy==='afterInteractive'){loadScript(props);}else if(strategy==='lazyOnload'){loadLazyScript(props);}},[props,strategy]);if(!process.env.__NEXT_SCRIPT_LOADER){return null;}if(strategy==='beforeInteractive'){if(updateScripts){scripts.beforeInteractive=(scripts.beforeInteractive||[]).concat([(0,_extends2.default)({src,onLoad,onError},restProps)]);updateScripts(scripts);}}return null;}var _default=Script;exports.default=_default;
//# sourceMappingURL=experimental-script.js.map