简体   繁体   English

反应公用文件夹脚本

[英]React public folder scripts

Calling on the react experts haha.呼唤反应专家哈哈。 I am trying to import/include some external javascript files into my React project, these js files cannot be changed or edit as per instructions from my company, as this is a base for many application that uses them.我正在尝试将一些外部 javascript 文件导入/包含到我的 React 项目中,这些 js 文件无法按照我公司的说明进行更改或编辑,因为这是许多使用它们的应用程序的基础。

For reasons i wont be putting the real filenames and folders.由于某些原因,我不会放置真实的文件名和文件夹。 There is a folder in my public folder called "testModules", so path is public/testModules/ all the files / 3 files that i need to include is in there.我的公共文件夹中有一个名为“testModules”的文件夹,所以路径是公共/testModules/我需要包含的所有文件/3 个文件都在那里。

What i have tried so far.到目前为止我所尝试的。

In my index.html file at the bottom i have tried the following在底部的 index.html 文件中,我尝试了以下操作

<script type="text/JavaScript" src="%PUBLIC_URL%/testModules/test1.js"></script>
<script type="text/JavaScript" src="%PUBLIC_URL%/testModules/test2.js"></script>
<script type="text/JavaScript" src="%PUBLIC_URL%/testModules/test3.js"></script>

and

<script type="text/JavaScript" src="./testModules/test1.js"></script>
<script type="text/JavaScript" src="./testModules/test2.js"></script>
<script type="text/JavaScript" src="./testModules/test3.js"></script>

and

<script type="application/javascript" src="./testModules/test1.js"></script>
<script type="application/javascript" src="./testModules/test2.js"></script>
<script type="application/javascript" src="./testModules/test3.js"></script>

and

Then i have also tried using the ScriptTag plugin "react-script-tag"然后我也尝试使用ScriptTag插件“react-script-tag”

with the same examples as above, but instead of %PUBLIC_URL% i used process.env.PUBLIC_URL使用与上面相同的示例,但我使用 process.env.PUBLIC_URL 而不是 %PUBLIC_URL%

When i try any of the above it works on my local development env.当我尝试上述任何方法时,它适用于我的本地开发环境。 For example running npm run start and opening on localhost例如运行npm run start并在本地主机上打开

But as soon as i move to application to a live server i get the following err但是,一旦我将应用程序转移到实时服务器,我就会收到以下错误

Refused to execute script from 'https://test.com/testModules/test.js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.拒绝执行来自 'https://test.com/testModules/test.js' 的脚本,因为它的 MIME 类型 ('text/html') 不可执行,并且启用了严格的 MIME 类型检查。

For all the files i added.对于我添加的所有文件。

Now i open up my dev console and i can see the scripts are appended/included in the body at the bottom.现在我打开我的开发控制台,我可以看到脚本被附加/包含在底部的正文中。

What is going on and how can i fix it.发生了什么事,我该如何解决。

Thanks You感谢您

If you wouldn't like to make any improvements to your deployment architecture, then you should pretty much talk to "your backend devs" so that they configure the web server so that this particular route would emit a different MIME type while serving this particular static file.如果您不想对部署架构进行任何改进,那么您应该与“您的后端开发人员”交谈,以便他们配置 web 服务器,以便此特定路由在服务此特定 static 时会发出不同的 MIME 类型文件。

Alternatively, if possible, you could use Webpack to statically link the third-party code base to your React project so that they are distributed together.或者,如果可能,您可以使用 Webpack 将第三方代码库静态链接到您的 React 项目,以便将它们分布在一起。 This way, if you use any implementation of Javascript modules, you will eliminate this crutch of your library being visible at the scope of the HTML host and this might bring improvement to your codebase.这样,如果您使用 Javascript 模块的任何实现,您将消除在 HTML 主机的 scope 上可见的库的这个拐杖,这可能会改进您的代码库。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM