webpack-dev-server historyApiFallback 不起作用

时间:2021-07-01 01:17:54

标签: javascript webpack router webpack-dev-server

我正在尝试实现我的客户端渲染版本。因此,我想处理从 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 '<'

指向第一个 <html> 标签。

  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)

有什么办法可以解决我做错的地方吗?

1 个答案:

答案 0 :(得分:0)

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

之前:

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

之后:

<script src="/index.js"></script>
相关问题