简体   繁体   English

如何从对讲快照预渲染中排除对讲脚本

[英]How to exclude Intercom scripts from react-snap prerender

I want to exclude the Intercom scripts (JS and CSS, I think, but is 400KBs of stringified code) from a react-snap prerender build. 我想从react-snap prerender构建中排除Intercom脚本(我认为是JS和CSS,但是是400KB的字符串化代码)。

All other scripts are already excluded but Intercom cannot be and is disrupting my prerender and SEO and making prerendered scripts far bigger than need to be. 所有其他脚本均已被排除在外,但Intercom不能并且正在干扰我的预渲染和SEO,并使预渲染的脚本远远超出需要的范围。

I have set the exclusion parameter in the package.json with: 我已经在package.json中设置了排除参数:
"reactSnap": { "destination": "build/pre-rendered", "removeScriptTags": true }, and this is exlcuding all other scripts. “ reactSnap”:{“ destination”:“ build / pre-rendered”,“ removeScriptTags”:true},这排除了所有其他脚本。

Intercom is loaded with either in index.html: 对讲机在index.html中加载:

window.intercomSettings = { app_id: 'APP_ID' }; window.intercomSettings = {app_id:'APP_ID'};

(function(){var w=window;var ic=w.Intercom;if(typeof ic==="function"){ic('reattach_activator');ic('update',w.intercomSettings);}else{var d=document;var i=function(){ic(arguments);};iq=[];ic=function(args){iqpush(args);};w.Intercom=i;var l=function(){var s=d.createElement('script');s.type='text/javascript';s.async=true;s.src=' https://widget.intercom.io/widget/ ' + 'APP_ID';var x=d.getElementsByTagName('script')[0];x.parentNode.insertBefore(s,x);};if(w.attachEvent){w.attachEvent('onload',l);}else{w.addEventListener('load',l,false);}}})(); (function(){var w = window; var ic = w.Intercom; if(ic的type ===“ function”){ic('reattach_activator'); ic('update',w.intercomSettings);}其他{ var d = document; var i = function(){ic(arguments);}; iq = []; ic = function(args){iqpush(args);}; w.Intercom = i; var l = function() {var s = d.createElement('script'); s.type ='text / javascript'; s.async = true; s.src =' https: //widget.intercom.io/widget/'+'APP_ID '; var x = d.getElementsByTagName('script')[0]; x.parentNode.insertBefore(s,x);}; if(w.attachEvent){w.attachEvent('onload',l);} else {w.addEventListener('load',l,false);}}})();

or the react-intercom component in the sub-component via: >Intercom appID= 'APP_ID'/> 或子组件中的react-intercom组件通过:> Intercom appID ='APP_ID'/>

Any help would be much appreciated - thanks 任何帮助将不胜感激-谢谢

I had the exact same issue. 我有完全相同的问题。 You can detect pre-rendering by looking at the user agent: 您可以通过查看用户代理来检测预渲染:

function isPreRendering() {
    return navigator.userAgent === 'ReactSnap';
};

Then just put your entire script above in a if (!isPreRendering) block. 然后将整个脚本放在if (!isPreRendering)块中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM