[英]Using webpack dev server for Single Page App historyApiFallback without using the default index.html file
Recently I had to rename my index.html
file to app.html
because of some Firebase Hosting related issues.最近,由于一些与 Firebase 托管相关的问题,我不得不将我的
index.html
文件重命名为app.html
。
Before that, my webpack dev server was working just fine, with the following configuration using the index.html
file:在此之前,我的webpack 开发服务器运行良好,使用
index.html
文件进行以下配置:
webpack.config.js webpack.config.js
// SOME OTHER CONFIGS
entry: {
app: './src/index.js'
},
plugins: [
new CleanWebpackPlugin(),
new WebPackManifestPlugin(),
new HtmlWebpackPlugin({
//title: 'Todo App',
favicon: 'src/assets/Favicon.ico',
template: './src/index.html',
}),
new Dotenv()
],
devServer: {
contentBase: './public',
compress: true,
hot: true,
historyApiFallback: true,
},
But after changing my index.html
to app.html
I had to update some configs to point to the new file.但是在将我的
index.html
更改为app.html
我必须更新一些配置以指向新文件。
webpack.config.js webpack.config.js
// SOME OTHER CONFIGS
entry: {
app: './src/index.js'
},
plugins: [
new CleanWebpackPlugin(),
new WebPackManifestPlugin(),
new HtmlWebpackPlugin({
//title: 'Todo App',
favicon: 'src/assets/Favicon.ico',
template: './src/app.html', // <-------------- ADDED THIS
filename: 'app.html' // <-------------- ADDED THIS
}),
new Dotenv()
],
devServer: {
contentBase: './public',
compress: true,
hot: true,
historyApiFallback: true,
index: 'app.html' // <-------------- ADDED THIS
},
PROBLEM问题
And now what happens is that my dev server is only accepting the home route '/'
.现在发生的是我的开发服务器只接受主路由
'/'
。 For every other specific routes it's responding with a 404.对于其他所有特定路由,它都以 404 响应。
https://webpack.js.org/configuration/dev-server/#devserverhistoryapifallback I guess this is the reason why: https://webpack.js.org/configuration/dev-server/#devserverhistoryapifallback我想这就是原因:
QUESTION题
How can I solve this?我该如何解决这个问题? Do I need rewrites to point all the routes to my
app.html
file or is there an easier solution?我是否需要重写以将所有路由指向我的
app.html
文件,还是有更简单的解决方案?
If I need the rewrite, how can I write one of those?如果我需要重写,我该如何编写其中之一? I didn't get the
/^\\/$/
syntax of the from
property.我没有得到
from
属性的/^\\/$/
语法。 How can I write a /**
wildcard route?如何编写
/**
通配符路由?
From: https://github.com/bripkens/connect-history-api-fallback来自: https : //github.com/bripkens/connect-history-api-fallback
Just found out what was missing:刚刚发现缺少什么:
devServer: {
contentBase: './public',
compress: true,
hot: true,
// historyApiFallback: true,
historyApiFallback: {
index: '/app.html' // <----- THIS WORKS
},
index: 'app.html'
// headers: {
// "Access-Control-Allow-Origin": "*",
// "Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, PATCH, OPTIONS",
// "Access-Control-Allow-Headers": "X-Requested-With, content-type, Authorization"
// }
},
Now everything works.现在一切正常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.