首页 > 解决方案 > webpack-dev-server historyApiFallback 不起作用

问题描述

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

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

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

  1. 来自官方指南
// 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)

任何想法来解决我做错了什么?

标签: javascriptwebpackrouterwebpack-dev-server

解决方案


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

前:

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

后:

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

推荐阅读