简体   繁体   English

使用 Javascript 将 React 应用程序嵌入为小部件失败,并在 Firefox 和 Chrome 上显示不同的消息

[英]Embedding React app as a widget with Javascript is failing with different messages on Firefox and Chrome

Following the steps on this site I'm able to embed a react app as a widget at another site with iframe:按照本网站上的步骤,我可以使用 iframe 在另一个网站上嵌入一个反应应用程序作为小部件:

<html>
  <iframe src="http://localhost:3000"><iframe>
</html>

Embedding with Javascript (the second method) is failing with the following error message on Firefox:嵌入 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 '<'

Following is my code to embed the react app:以下是我嵌入反应应用程序的代码:

<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>

I can confirm that the js source I specified is correct and running:我可以确认我指定的 js 源是正确的并且正在运行: 在此处输入图像描述

On Chrome, the warning message is displayed as below:在 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.

Seeking guidance to solve this.寻求指导以解决此问题。 Thank you for reading.感谢您的阅读。

Update: Referring to @Thai's answer, I'm specifying my source file as the one generated by the react build operation.更新:参考@Thai 的回答,我将我的源文件指定为由 react 构建操作生成的文件。 How can I specify the right js file to solve my issue?如何指定正确的 js 文件来解决我的问题? 在此处输入图像描述

I can confirm that the js source I specified is correct and running我可以确认我指定的 js 源是正确的并且正在运行

Thanks for posting that screenshot, because it is crucial to figuring out what the problem is.感谢您发布该屏幕截图,因为这对于找出问题所在至关重要。 Contrary to what you said, the JS source is not correct.与您所说的相反,JS 来源不正确。 Your JavaScript source is just a text file, and it cannot run on its own inside a browser.您的 JavaScript 源代码只是一个文本文件,它不能在浏览器中自行运行。

This means that when you view the JavaScript source file, you should see the source code, and not a working application.这意味着当您查看 JavaScript 源文件时,您应该看到源代码,而不是工作应用程序。

如果 JavaScript 文件的 URL 正确,您应该会看到 JavaScript 代码而不是工作应用程序

However in your screenshot it shows the actual application and not the JavaScript source.但是,在您的屏幕截图中,它显示了实际应用程序,而不是 JavaScript 源。

浏览器截图

What you're actually getting is an HTML page.您实际得到的是 HTML 页面。 Your development server probably serves this page when a file you requested couldn't be found.当找不到您请求的文件时,您的开发服务器可能会提供此页面。 So it serves the index page as a fallback.所以它将索引页面作为后备。

Next time, when confirming if something is correct, may I suggest that you try intentionally making it incorrect, and then see if the result differs?下次在确认是否正确的时候,可不可以建议你故意弄错,然后看看结果有没有不同?

Figured it out with assistance - the issue was due to the way I was creating the build.在帮助下解决了这个问题 - 问题是由于我创建构建的方式造成的。

We can't use the default build tool that comes with CRA.我们不能使用 CRA 自带的默认构建工具。 Use rescripts/cli instead.请改用rescripts/cli

  1. Install @rescripts/cli as a dev dependency:安装 @rescripts/cli 作为开发依赖项:

npm install @rescripts/cli --save-dev

  1. Update package.json "scripts" to run with 'rescripts':更新 package.json “脚本”以使用“脚本”运行:

    "scripts": { "start": "rescripts start", "build": "rescripts build", "test": "rescripts test", "eject": "rescripts eject" },

  2. Build your app with构建您的应用程序

npm run buiild

  1. Embed the app with:嵌入应用程序:

    <script src="https://localhost:3000/static/js/bundle.js"></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 javascript 在 chrome 和 firefox 中的不同行为 - javascript different behaviour in chrome and firefox chrome和firefox上的javascript null比较有所不同 - javascript null comparison different on chrome and firefox A javascript function 在 Chrome 和 Firefox 中返回不同的结果 - A javascript function returns different results in Chrome and Firefox 推送通知在 Firefox 中有效,但在 React 应用程序中的 Chrome 中无效 - Push notifications working in Firefox but not Chrome in React app 将react小部件嵌入到另一个网站 - Embedding react widget into another website JavaScript无法在Firefox / Firebug中运行? - Javascript failing in Firefox/Firebug? WebExtension在Firefox中无提示失败,但在Chrome中没有失败 - WebExtension failing silently in Firefox but not Chrome 与Google Chrome相比,Mozilla Firefox中的JavaScript排序功能输出有所不同 - JavaScript Sort function Output is different in Mozilla Firefox Compared to Google Chrome Javascript生成的日历在mozilla firefox和chrome中显示不同 - Javascript generated calendar showing different in mozilla firefox and chrome Chrome和Firefox在CSS变量的JavaScript文件导入中给出了不同的结果 - Chrome and Firefox giving different results on JavaScript file import of CSS variable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM