简体   繁体   English

使用 webpack 开发服务器进行单页应用 historyApiFallback 而不使用默认的 index.html 文件

[英]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.

相关问题 使用webpack-dev-server时,html-webpack-plugin不会将js文件注入index.html - html-webpack-plugin not inject js file into index.html when using webpack-dev-server index.html 使用 webpack-dev-server 不重新加载 - index.html using webpack-dev-server not reload Webpack 开发服务器未找到索引。html - Webpack dev server not finding index.html 使用 webpack 构建 index.html 以与开发服务器一起使用 - Build index.html with webpack to use with dev server webpack-dev-server 不提供 index.html 由 HtmlWebPackPlugin 生成 - webpack-dev-server not serving index.html generated by HtmlWebPackPlugin historyApiFallback 在 Webpack 开发服务器中不起作用 - historyApiFallback doesn't work in Webpack dev server 如何使用HTML Webpack插件将文件加载器所需的CSS文件包含到index.html中 - how to include css file required with file loader into index.html using html webpack plugin 使用 Webpack 和 React 创建一个 2 页的网站( index.html 和 another.html ) - Creating a 2 page website ( index.html & another.html ) using Webpack and React 尽管将webpack-dev-server的historyApiFallback设置为true,但仍在我的React应用中接收404 - Receiving 404s in my React app despite setting historyApiFallback to true for webpack-dev-server 使用html-webpack-plugin在Webpack中生成index.html文件(使用vue-simple样板的项目) - Using html-webpack-plugin to generate an index.html file in Webpack (project using vue-simple boilerplate)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM