[英]Embedding React app as a widget with Javascript is failing with different messages on Firefox and Chrome
按照本網站上的步驟,我可以使用 iframe 在另一個網站上嵌入一個反應應用程序作為小部件:
<html>
<iframe src="http://localhost:3000"><iframe>
</html>
嵌入 Javascript(第二種方法)失敗,並在 Firefox 上顯示以下錯誤消息:
The script from “http://localhost:3000/static/js/main.dbfc58d6.chunk.js” was loaded even though its MIME type (“text/html”) is not a valid JavaScript MIME type.
SyntaxError: expected expression, got '<'
以下是我嵌入反應應用程序的代碼:
<html lang="en">
<body><noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<script type="text/javascript" src="http://localhost:3000/static/js/main.dbfc58d6.chunk.js"></script>
</body>
</html>
在 Chrome 上,警告消息顯示如下:
index.html:1 A cookie associated with a cross-site resource at http://localhost/ was set without the `SameSite` attribute. A future release of Chrome will only deliver cookies with cross-site requests if they are set with `SameSite=None` and `Secure`. You can review cookies in developer tools under Application>Storage>Cookies and see more details at https://www.chromestatus.com/feature/5088147346030592 and https://www.chromestatus.com/feature/5633521622188032.
index.html:10 Cross-Origin Read Blocking (CORB) blocked cross-origin response http://localhost:3000/static/js/main.dbfc58d6.chunk.js with MIME type text/html. See https://www.chromestatus.com/feature/5629709824032768 for more details.
尋求指導以解決此問題。 感謝您的閱讀。
更新:參考@Thai 的回答,我將我的源文件指定為由 react 構建操作生成的文件。 如何指定正確的 js 文件來解決我的問題?
在幫助下解決了這個問題 - 問題是由於我創建構建的方式造成的。
我們不能使用 CRA 自帶的默認構建工具。 請改用rescripts/cli 。
npm install @rescripts/cli --save-dev
更新 package.json “腳本”以使用“腳本”運行:
"scripts": { "start": "rescripts start", "build": "rescripts build", "test": "rescripts test", "eject": "rescripts eject" },
構建您的應用程序
npm run buiild
嵌入應用程序:
<script src="https://localhost:3000/static/js/bundle.js"></script>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.