javascript - 如何指定可以在哪些页面加载stripejs脚本?
问题描述
Stripe 的脚本导致大量主线程阻塞,如 Google Insights 中的图片所示
我的问题是受影响的页面只是一个不需要条纹的页面。但显然 stripejs 库将其第三方脚本插入页面的头部,所以这是一个 SPA,这意味着它在每个页面中。
如何使该第三方脚本仅插入到积极使用该库的组件中?
其他改善或缓解阻塞的方法将不胜感激,谢谢
解决方案
You could use script tag with async
inside next/head
component on the needed page:
import Head from 'next/head';
// ...
<Head>
<script async src="https://js.stripe.com/v3" />
</Head>
It would only be downloaded if user requests that page.
Although Stripe recommends to use it on every page:
To best leverage Stripe’s advanced fraud functionality, include this script on every page, not just the checkout page.
推荐阅读
- ios - unable to read properly list file from Info.plist
- javascript - 如何“排队”请求在没有 setInterval 的情况下一直运行?
- reactjs - 我不能在 React 上使用鼠标事件来更改背景颜色
- python - IndexError:使用深度学习进行音频分类时列出超出范围的索引
- python - 尝试在我的 Col 标题顶部添加另一行以 Wkdys 的名称,并在 b/w 中插入 cols,其中所有 cols 为 8 hrs b/w
- r - 在 map_dfr 中指定路径名
- c++ - 创建 openCL 缓冲区会导致延迟
- apache-spark - Spark SQL 中的相关子查询
- flutter - 如何在颤动中向警报对话框添加选项卡
- ios - 如何使用 iOS 14+ 的 ScrollViewReader 和 iOS 13 的部署目标