您可以使用以下两种方法之一设置根路径,以便在生产环境中为React应用程序提供服务:
一。通过在
包.json
文件
通知行5:
{
"name": "reactTest",
"version": "0.1.0",
"private": true,
"homepage": "mywebsite/web",
"dependencies": {
"jquery": "^3.3.1",
"moment": "^2.22.1",
"react": "^16.4.1",
"react-datepicker": "^1.5.0",
"react-dom": "^16.4.1",
"react-month-picker": "^1.3.7",
"react-scripts": "1.1.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
}
}
(
see documentation
)
2。使用公共URL环境变量
当运行
建造
job,在其前面添加env变量,如下所示:
PUBLIC_URL=mywebsite/web npm run build
(
see documentation
)
它是做什么的?
这些方法将
不
更改源映射文件中的路径,这些路径将始终是相对的,但是
将
使您能够使用您选择的绝对路径将React应用程序部署到您的web服务器。
它将在生成的
index.html索引
要绝对,根据您设置的值:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no">
<meta name="theme-color" content="#000000">
<link rel="manifest" href="mywebsite/web/manifest.json">
<link rel="shortcut icon" href="mywebsite/web/favicon.ico">
<title>React App</title>
<link href="mywebsite/web/static/css/main.c17080f1.css" rel="stylesheet">
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<script type="text/javascript" src="mywebsite/web/static/js/main.dbfd0e89.js"></script>
</body>
</html>