简体   繁体   English

如何将 React App 嵌入到另一个网站

[英]How to embed React App into another website

I have an old website running on server x.我有一个旧网站在服务器 x 上运行。 Now a React-App has been developed, which is on server y.现在已经开发了一个 React-App,它在服务器 y 上。

The website should display the React-App.该网站应显示 React-App。

I have searched and read several posts on the topic but with no success so far.我已经搜索并阅读了有关该主题的几篇文章,但到目前为止都没有成功。 The only solution currently working is an iframe but we don't want this.目前唯一有效的解决方案是 iframe,但我们不想要这个。

If I do如果我做

npm run

and inspect the source on the server hosting the React-App, there is the following:并检查托管 React-App 的服务器上的源代码,有以下内容:

...

<div id="react-reporting"></div>

<script src="/static/js/bundle.js"></script><script src="/static/js/0.chunk.js"></script><script src="/static/js/main.chunk.js"></script>

Basically I would like to take this HTML-part and put it into the old site.基本上我想把这个 HTML 部分放到旧站点中。 Is this possible and if yes how?这是可能的,如果是,如何?

My approach actually works, but I missed these two scripts:我的方法确实有效,但我错过了这两个脚本:

 <script src="https://unpkg.com/react@16/umd/react.development.js" crossorigin></script>
 <script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js" crossorigin></script>

So for testing locally it is:因此,对于本地测试,它是:

 <div id="react-reporting"></div>
 <script src="https://unpkg.com/react@16/umd/react.development.js" crossorigin></script>
 <script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js" crossorigin></script>
 <script src="http://localhost:3000/static/js/bundle.js"></script><script src="http://localhost:3000/static/js/0.chunk.js"></script>
 <script src="http://localhost:3000/static/js/main.chunk.js"></script>

I also approached an error regarding sockjs which was caused by the test-file, which imports the React App, was being opened as file:// not via http://我还处理了一个关于 sockjs 的错误,这是由导入 React 应用程序的测试文件引起的,它被打开为 file:// 而不是通过 http://

Edit编辑

After some more tests, I found using webpack is the best solution - it makes the app accessible as a single file.经过更多测试,我发现使用 webpack 是最好的解决方案 - 它使应用程序可以作为单个文件访问。

package.json包.json

...
"scripts": {
    "start": "webpack-dev-server --inline --hot",
    "build": "webpack --config webpack.config.js"
},
...

webpack.config.js webpack.config.js

 const path = require("path")
 const UglifyJsPlugin = require("uglifyjs-webpack-plugin")
 const glob = require("glob")

 module.exports = {
     entry: ['@babel/polyfill','./src/index.js'],
     output: {
        filename: "bundle.js"
     },
     module: {
        rules: [
             {
                test: /\.css$/,
                loader: 'style-loader!css-loader'
             },
             {
                test: /\.js$/,
                exclude: /node_modules/,
                loader: 'babel-loader'
            }
        ],
    },
    optimization: {
        minimizer: [new UglifyJsPlugin()]
    }
}
  • polyfill is needed for compatibility with IE11与 IE11 兼容需要 polyfill
  • every file processed (starting with the entries) counts as module.处理的每个文件(从条目开始)都算作模块。 If it matches the "test"-part of a rule, the rule is applied (except for the excludes).如果它与规则的“测试”部分匹配,则应用该规则(排除除外)。

.babelrc .babelrc

{
   "presets": ['@babel/react',
              ['@babel/preset-env', {
                 "targets": {
                   "browsers": ["last 2 versions", "ie >= 11"]
                }
              }]],
  "plugins": ['@babel/plugin-proposal-class-properties']
}
  • plugin-proposal-class-properties is only needed because I have some static members in classes.只需要 plugin-proposal-class-properties 是因为我在类中有一些静态成员。

When running跑步时

npm start

the Webpack-Dev-Server will start and make the app accessible under http://localhost:8080/dist/bundle.js . Webpack-Dev-Server 将启动并使应用程序可在http://localhost:8080/dist/bundle.js 下访问

Code to embed the app in another site:将应用程序嵌入另一个站点的代码:

<div id="react-reporting"></div>
<script src="https://unpkg.com/react@16/umd/react.development.js" crossorigin></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js" crossorigin></script>
<script src="http://localhost:8080/dist/bundle.js"></script>                                    

If you are using create-react-app, go to the folder of your React App, and insert this command:如果您使用的是 create-react-app,请转到您的 React App 文件夹,然后插入以下命令:

npm run build npm 运行构建

This will generate a ./build folder with all that you need for your app.这将生成一个 ./build 文件夹,其中包含您的应用程序所需的所有内容。
Then it's simply a matter of dragging the ./build folder to server x.然后只需将 ./build 文件夹拖到服务器 x 即可。
You can also change the HTML that calls the react app so it will fit the old webpage.您还可以更改调用 react 应用程序的 HTML,使其适合旧网页。

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

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