问题描述
是接手的别人写的官网项目。采用Next.js编写。在加载百度商桥客服代码时,经常无法正常加载。
刷新十次页面可能有三次加载成功。系统刚发布的一段时间都是正常的,最近就老是出问题,线上代码也没有发布过新版本。问题好像是运营人员在商桥后台改过一次商桥的图标(只是样式)后出现的,不知道是不是这个引起的。但是现在所有的都恢复默认了,(包括新建一个张哈)依然有加载问题。
打开调试,能看到商桥的js都是成功加载了的,但是页面中并没有商桥图标。element中也没有相关元素。(如下图所示)
问题出现的环境背景及自己尝试过哪些方法
尝试过把加载的代码放到其他非next的项目中,每次都能正常加载。也尝试过新建一个商桥账号,在本地开发环境的next项目中也无法正常加载。(奇怪的是线上环境偶尔还能加载成功,开发环境现在基本没成功过)。
怀疑过是网络问题,我手机流量开WiFi访问也是一样无法加载。
相关代码
// 请把代码文本粘贴到下方(请勿用图片代替代码)
import React from 'react'
import Head from 'next/head'
import Header from './Header'
import Footer from './Footer'
import FreeBottom from './FreeBottom'
const Layout = (props) => (
<div>
<Head>
<meta name="viewport" content="width=device-width,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<script>{renderCommunication()}</script>
<script>{renderStatistic()}</script>
<script>{renderPush()}</script>
</Head>
{props.isShow? <Header headStyle={props.headStyle}/> : null}
{props.children}
{props.isShow? <Footer linkList={props.linkList} showLink={props.showLink}/> : null}
<FreeBottom/>
</div>
)
//商桥
const renderCommunication = function(){
if (typeof window !== 'undefined') {
let _hmt = _hmt || []
let hm = document.createElement("script");
hm.src = "https://hm.baidu.com/hm.js?xxxxxxxxxxxxxxxxxxx";
let s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(hm, s);
}
}
const renderStatistic = function(){
if (typeof window !== 'undefined') {
var _hmt = _hmt || [];
(function() {
var hm = document.createElement("script");
hm.src = "https://hm.baidu.com/hm.js?XXXXXXXXXXXXXXXXX";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(hm, s);
})();
}
}
const renderPush = function(){
if (typeof window !== 'undefined') {
(function(){
var bp = document.createElement('script');
var curProtocol = window.location.protocol.split(':')[0];
if (curProtocol === 'https') {
bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
}
else {
bp.src = 'http://push.zhanzhang.baidu.com/push.js';
}
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(bp, s);
})();
}
}
export default Layout
你期待的结果是什么?实际看到的错误信息又是什么?
是不是在商桥的外部JS还没加载还没成功,服务端就渲染成功,并返回代码了。这个项目之前不是我写的,对next并不是很清楚,望大神指点
这个应该是百度商桥的问题,现在你换了一家做客服咨询的。完全没有问题