You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 14:57:24