next
Version:
The React Framework
4 lines • 3.19 kB
JavaScript
var _interopRequireWildcard=require("@babel/runtime/helpers/interopRequireWildcard");var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");exports.__esModule=true;exports.default=Script;var _extends2=_interopRequireDefault(require("@babel/runtime/helpers/extends"));var _objectWithoutPropertiesLoose2=_interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose"));var _react=_interopRequireWildcard(require("react"));var _headManagerContext=require("../next-server/lib/head-manager-context");var _headManager=require("./head-manager");var _requestIdleCallback=_interopRequireDefault(require("./request-idle-callback"));const ScriptCache=new Map();const LoadCache=new Set();const loadScript=props=>{const{src='',onLoad=()=>{},dangerouslySetInnerHTML,children='',id,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){continue;}const attr=_headManager.DOMAttributeNames[k]||k.toLowerCase();el.setAttribute(attr,value);}document.body.appendChild(el);};function Script(props){const{src='',onLoad=()=>{},dangerouslySetInnerHTML,children='',strategy='defer',onError,preload=false}=props,restProps=(0,_objectWithoutPropertiesLoose2.default)(props,["src","onLoad","dangerouslySetInnerHTML","children","strategy","onError","preload"]);// Context is available only during SSR
const{updateScripts,scripts}=(0,_react.useContext)(_headManagerContext.HeadManagerContext);(0,_react.useEffect)(()=>{if(strategy==='defer'){loadScript(props);}else if(strategy==='lazy'){window.addEventListener('load',()=>{(0,_requestIdleCallback.default)(()=>loadScript(props));});}},[strategy,props]);if(strategy==='dangerouslyBlockRendering'){const syncProps=(0,_extends2.default)({},restProps);for(const[k,value]of Object.entries({src,onLoad,onError,dangerouslySetInnerHTML,children})){if(!value){continue;}if(k==='children'){syncProps.dangerouslySetInnerHTML={__html:typeof value==='string'?value:Array.isArray(value)?value.join(''):''};}else{;syncProps[k]=value;}}return/*#__PURE__*/_react.default.createElement("script",syncProps);}else if(strategy==='defer'){if(updateScripts&&preload){scripts.defer=(scripts.defer||[]).concat([src]);updateScripts(scripts);}}else if(strategy==='eager'){if(updateScripts){scripts.eager=(scripts.eager||[]).concat([(0,_extends2.default)({src,onLoad,onError},restProps)]);updateScripts(scripts);}}return null;}
//# sourceMappingURL=experimental-script.js.map
;