historyApiFallback在Webpack开发服务器中不起作用

马特:

在React Router中使用Webpack开发服务器和browserHistory通过HTML5 History API处理网址。historyapifallback-option在我的webpack配置文件中不起作用。刷新后http://localhost:8080/users还是http://localhost:8080/products得到了404。

webpack.config.js

var webpack = require('webpack');
var merge = require('webpack-merge');

const TARGET = process.env.npm_lifecycle_event;

var common = {
    cache: true,
    debug: true,
    entry: './src/script/index.jsx',
    resolve: {
        extensions: ['', '.js', '.jsx']
    },
    output: {
        sourceMapFilename: '[file].map'
    },
    module: {
        loaders: [
            {
                test: /\.js[x]?$/,
                loader: 'babel-loader',
                exclude: /(node_modules)/
            }
        ]
    },
    plugins: [
        new webpack.ProvidePlugin({
            $: "jquery",
            jQuery: "jquery"
        })
    ]
};

if(TARGET === 'dev' || !TARGET) {
    module.exports = merge(common,{
        devtool: 'eval-source-map',
        devServer: {
            historyApiFallback: true
        },
        output: {
            filename: 'index.js',
            publicPath: 'http://localhost:8090/assets/'
        },
        plugins: [
            new webpack.DefinePlugin({
                'process.env.NODE_ENV': JSON.stringify('dev')
            })
        ]
    });
}

index.html

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
        <title>Test</title>
    </head>
    <body>
        <div id="content">
            <!-- this is where the root react component will get rendered -->
        </div>
        <script src="http://localhost:8090/webpack-dev-server.js"></script>
        <script type="text/javascript" src="http://localhost:8090/assets/index.js"></script>
    </body>
</html>

index.jsx

import React, {Component} from 'react';
import ReactDOM from 'react-dom';
import {Router, Route, useRouterHistory, browserHistory, Link} from 'react-router';

class Home extends Component{
  constructor(props) {
    super(props);
  }

  render() {
      return <div>
          I am home component
          <Link to="/users" activeClassName="active">Users</Link>
          <Link to="/products" activeClassName="active">Products</Link>
        </div>;
  }
}

class Users extends Component{
  constructor(props) {
    super(props);
  }

  render() {
      return <div> I am Users component </div>;
  }
}

class Products extends Component{
  constructor(props) {
    super(props);
  }

  render() {
      return <div> I am Products component </div>;
  }
}

ReactDOM.render(
    <Router history={browserHistory} onUpdate={() => window.scrollTo(0, 0)}>
        <Route path="/" component={Home}/>
        <Route path="/users" component={Users} type="users"/>
        <Route path="/products" component={Products} type="products"/>
    </Router>
    , document.getElementById('content'));

package.json

{
  "name": "test",
  "version": "1.0.0",
  "description": "",
  "main": "index.jsx",
  "scripts": {
    "start": "npm run serve | npm run dev",
    "serve": "./node_modules/.bin/http-server -p 8080",
    "dev": "webpack-dev-server -d --progress --colors --port 8090 --history-api-fallback"
  },
  "author": "",
  "license": "MIT",
  "dependencies": {
    "events": "^1.1.0",
    "jquery": "^2.2.3",
    "path": "^0.12.7",
    "react": "^15.0.2",
    "react-dom": "^15.0.2",
    "react-mixin": "^3.0.5",
    "react-router": "^2.4.0"
  },
  "devDependencies": {
    "babel": "^6.5.2",
    "babel-core": "^6.8.0",
    "babel-loader": "^6.2.4",
    "babel-polyfill": "^6.8.0",
    "babel-preset-es2015": "^6.6.0",
    "babel-preset-react": "^6.5.0",
    "babel-register": "^6.8.0",
    "http-server": "^0.9.0",
    "webpack": "^1.13.0",
    "webpack-dev-server": "^1.14.1",
    "webpack-merge": "^0.12.0"
  }
}

我尝试在配置中更改devServer,但没有帮助:

devServer: {
    historyApiFallback: {
        index: 'index.html',
    }
},

devServer: {
    historyApiFallback: {
        index: 'index.js',
    }
},

devServer: {
    historyApiFallback: {
        index: 'http://localhost:8090/assets',
    }
},

devServer: {
    historyApiFallback: {
        index: 'http://localhost:8090/assets/',
    }
},

devServer: {
    historyApiFallback: {
        index: 'http://localhost:8090/assets/index.html',
    }
},

devServer: {
    historyApiFallback: {
        index: 'http://localhost:8090/assets/index.js',
    }
},

devServer: {
    historyApiFallback: {
        index: 'http://localhost:8090/assets/index.js',
    }
},
output: {
    filename: 'index.js',
            publicPath: 'http://localhost:8090/assets/'
},
越前:

我今天遇到同样的问题。让webpack.config.js中的config output.publicPath等于devServer.historyApiFallback.index并指出html文件路径。我的 webpack-dev-server版本是1.10.1,可以正常工作。http://webpack.github.io/docs/webpack-dev-server.html#the-historyapifallback-option不起作用,您必须指出html文件路由。

例如

module.exports = {
    entry: "./src/app/index.js",
    output: {
        path: path.resolve(__dirname, 'build'),
        publicPath: 'build',
        filename: 'bundle-main.js'
    },
    devServer: {
        historyApiFallback:{
            index:'build/index.html'
        },
    },
};

historyApiFallback.index表示当URL路径与真实文件不匹配时,webpack-dev-server使用historyApiFallback.index中的文件配置在浏览器而不是404页面中显示。然后关于路由更改的所有事情都让您的js使用react-router做到。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Javascript在Go Web服务器中不起作用

“ rails服务器”命令在终端中不起作用

Webpack开发服务器热模式不起作用

Webpack开发服务器的惰性模式有什么作用?

如何同时使用historyApiFallback和代理远程api请求设置Webpack开发服务器?

推送器在Godaddy服务器上不起作用

Google App Engine queue.yaml在开发服务器中不起作用

Tableau按钮在服务器中不起作用

开发服务器热更新不起作用

webpack --watch && http服务器./dist -p 8080 --cors -o不起作用

与Android App中的服务器通信不起作用

JSDoc在使用Webpack开发服务器的Lerna monorepo项目的本地包中不起作用,但在发布到包注册表中时起作用

Webpack开发服务器代理路由器选项不起作用

DNS解析在18.04服务器上不起作用

php file()方法在服务器中不起作用

Apache Web服务器在浏览器中不起作用

servlet在CouchBase服务器中不起作用

肥皂服务器在Laravel 5.2中不起作用

$ .getJSON在部署服务器中不起作用

注销在实时服务器Godaddy中不起作用

.htaccess 文件在新服务器中不起作用

Webpack 2 中是否有开发服务器隧道?

Java 邮件在云服务器中不起作用

来自本地服务器的 GET 请求不起作用

PHPMailer 脚本在共享服务器中运行良好,但相同的脚本在 AWS 服务器中不起作用

RPostgreSQL 查询在 Shiny 服务器中不起作用

Kafka 服务器启动命令不起作用

我的快速服务器的代理服务器在我的 React 应用程序中不起作用

CSS 在 Blazor 服务器中的 _Host.chstml 中不起作用