Next.js中Trustpilot Widget间歇性加载失败问题求助
Next.js中Trustpilot Widget间歇性加载失败问题
在Next.js应用中遇到Trustpilot Widget间歇性加载失败的问题:有时加载正常,有时无法加载。已查阅Trustpilot集成文档并尝试多种解决方案,但均无效果。此前基于WordPress的网站未出现该问题,当前Next.js项目却存在此异常。
相关代码
1. _app.js的head标签中引入的脚本
<Head> <script type="text/javascript" src="//widget.trustpilot.com/bootstrap/v5/tp.widget.bootstrap.min.js" async ></script> </Head>
2. Trustpilot Widget组件代码
加载失败时console.log(window.trustpilot)输出undefined:
import React, { useEffect, useRef } from "react"; const TrustBox = ({ mode}) => { const option = { horizontal: { "data-template-id": "the id", "data-businessunit-id": "the id", "data-style-height": "150px", "data-style-width": "100%", "data-theme": "white", "data-text-size":"30px", }, carousel: { "data-template-id": "the id", "data-businessunit-id": "the id", "data-style-height": "250px", "data-style-width": "100%", "data-theme": "white", "data-stars": "5", "data-review-languages": "en", }, }; const ref = useRef(null); useEffect(() => { if (window.Trustpilot) { console.log('loaded') // 返回模块 window.Trustpilot.loadFromElement(ref.current, true); } else{ console.log(window.trustpilot) // undefined } }, [mode]); return ( <div ref={ref} data-locale="en-GB" {...option[mode]} /> ); }; export default TrustBox;
加载状态截图
- 加载失败时:

- 加载成功时:

内容的提问来源于stack exchange,提问作者Zia Yamin
相关产品推荐
相关产品推荐

