我最近将HashRouter转换为BrowserRouter,因为它在开发环境中运行良好,因为我添加了
devServer: {
historyApiFallback: true,
allowedHosts: 'all',
},
至WebPack.dev.config
当我在Prod上运行这个应用程序时,myapp.com可以工作,但myapp.com/home不能工作(它显示nginx 404错误).
如果我转到myapp.com/#/home,那么它会将我重定向到myapp.com/home,页面将呈现(我已经捕获了将遗留散列转换为普通URL的所有逻辑).
这是我完整的webpack配置.
Webpack.common.js
module.exports = {
entry: path.join(__dirname, 'src', 'index.js'),
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js',
chunkFilename: '[id].js',
publicPath: '/',
hashFunction: 'xxhash64',
},
resolve: {
extensions: ['.js', '.jsx'],
},
module: {
rules: [
{
test: /\.(scss)$/,
use: [
{
// inject CSS to page
loader: 'style-loader',
},
{
// translates CSS into CommonJS modules
loader: 'css-loader',
},
{
// Run postcss actions
loader: 'postcss-loader',
options: {
// `postcssOptions` is needed for postcss 8.x;
// if you use postcss 7.x skip the key
postcssOptions: {
// postcss plugins, can be exported to postcss.config.js
plugins: function () {
return [require('autoprefixer')]
},
},
},
},
{
// compiles Sass to CSS
loader: 'sass-loader',
},
],
},
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env', '@babel/preset-react'],
},
},
},
{
test: /\.css$/,
exclude: /node_modules/,
use: [
{ loader: 'style-loader' },
{
loader: 'css-loader',
options: {
modules: {
localIdentName: '[name]__[local]___[hash:base64:5]',
},
sourceMap: true,
},
},
{
loader: 'postcss-loader',
options: {
postcssOptions: {
plugins: [['autoprefixer', {}]],
},
},
},
],
},
{
test: /\.(png|jpe?g|gif|svg)$/,
use: [
{
loader: 'url-loader',
options: { limit: 10000, name: 'static/media/[name].[hash:8].[ext]' },
},
],
},
],
},
plugins: [
new HtmlWebpackPlugin({
template: __dirname + '/public/index.html',
filename: 'index.html',
inject: 'body',
favicon: './public/favicon.ico',
manifest: './public/manifest.json',
}),
],
}
Webpack.prod.js
module.exports = merge(common, {
mode: 'production',
performance: {
hints: false,
maxEntrypointSize: 512000,
maxAssetSize: 512000,
},
plugins: [
new Dotenv({
path: './.env',
}),
],
})
我遗漏了什么?
编辑
myapp.com可以工作,但如果我想导航到myapp.com/intro,它会给我ngnix 404.