我在正确加载 snipcart 时遇到问题,它会在 localhost 上获取 api 键并显示购物车中的项目数,但是当部署到 netlify 时,它会在页面加载时短暂闪烁购物车中的项目数,指示 api密钥是正确的,但是它会出现错误
public API key not found. Attribute data-api-key must be set on #snipcart tag.
我正在使用 react 来渲染它,并将其降低到最小的样板文件中,以查看是否有任何 react 可能导致此问题,但我仍然遇到同样的问题。
移动 <div hidden id="snipcart" data-api-key={{ API_KEY }}></div>
到app.js
文件并且它可以工作,但index.html
它不起作用
索引.html
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link href ="https://fonts.googleapis.com/css2?family=Open+Sans&family=Lexend+Zetta&family=Poppins:wght@400;500;700&family=Shadows+Into+Light&display=swap" rel="stylesheet">
<link rel="preconnect" href="https://app.snipcart.com">
<link rel="preconnect" href="https://cdn.snipcart.com">
<link rel="stylesheet" href="https://cdn.snipcart.com/themes/v3.0.23/default/snipcart.css" />
</head>
<body>
<div id="root"></div>
<script async src="https://cdn.snipcart.com/themes/v3.0.23/default/snipcart.js"></script>
<div hidden id="snipcart" data-api-key={{ API_KEY }}></div>
<script src="./index.tsx"></script>
</body>
</html>