【问题标题】:webpack-dev-server historyApiFallback doesn't workwebpack-dev-server historyApiFallback 不起作用
【发布时间】:2021-07-01 01:17:54
【问题描述】:

我正在尝试实现我的客户端渲染版本。因此,我想处理来自router.js 的路由,该路由是从index.html 导入的。

所以我添加了一个historyAPIFallback,但它似乎不起作用。

我尝试了两种方法都指向localhost/post/1/test

  1. 来自official guide
// webpack.config.js
devServer: {
    port: 3000,
    historyApiFallback: {
      rewrites: [{ from: /.*/, to: './src/index.html' }],
    },
  },

这个输出:

Uncaught SyntaxError: Unexpected token '<'

指向第一个&lt;html&gt;标签。

  1. 来自 stackoverflow(这似乎是一个旧解决方案)
// webpack.config.js
devServer: {
    port: 3000,
    historyApiFallback: {
      index: './src/index.html',
    },
  },

这个输出:

GET http://localhost:3000/post/1/index.js net::ERR_ABORTED 404 (Not Found)

有什么办法可以解决我做错的事情吗?

【问题讨论】:

    标签: javascript webpack router webpack-dev-server


    【解决方案1】:

    对我有用的解决方案是在我导入 index.jsindex.html 中添加 /

    之前:

    <script src="index.js"></script>
    

    之后:

    <script src="/index.js"></script>
    

    【讨论】:

      猜你喜欢
      • 2019-04-22
      • 2020-09-28
      • 1970-01-01
      • 2018-04-26
      • 2018-08-20
      • 2018-05-21
      • 2018-06-12
      • 1970-01-01
      • 2016-12-28
      相关资源
      最近更新 更多