Nested React router 4 routes not working on Webpack 3

白昼怎懂夜的黑 提交于 2019-11-28 08:23:01

问题


As the tittle suggest i can't get routes like

<Route path="/events/:id" component={EventDetailRoute} />

to work, and as i've read seems to be that the bundle in the index.html must be absolute, however i'm using the HtmlWebpackPlugin so the bundle gets injected as a relative path.

I've tried to set my output config for webpack as follows:

output: {
    path: path.resolve('dist'),
    filename: 'index_bundle.js',
    publicPath: '/'
},

But that doesn't work neither.

If i try this route: http://localhost:8080/events/7, I'm getting a 404 error when trying to find http://localhost:8080/events/index_bundle.js

This is my webpack.config:

const path = require('path'),
    HtmlWebpackPlugin = require('html-webpack-plugin'),
    webpack = require('webpack');

const HtmlWebpackPluginConfig = new HtmlWebpackPlugin({
    template: './src/index.html',
    filename: 'index.html',
    inject: 'body'
})

module.exports = {
    entry: './src/index.js',
    output: {
        path: "/" + path.resolve('dist', 'build'),
        filename: 'index_bundle.js',
        publicPath: '/'
    },
    devServer: {
        historyApiFallback: true,
        hot: true,
        publicPath: '/'
    },
    module: {
        rules: [
            {
                test: /\.jsx?$/,
                loader: 'babel-loader',
                exclude: /node_modules/
            },
            {
                test: /\.scss$/,
                use: [
                    {
                        loader: 'style-loader'
                    },
                    {
                        loader: 'css-loader',
                        options: {
                            modules: true,
                            camelCase: 'dashes',
                            localIdentName: '[name]__[local]'
                        }
                    },
                    {
                        loader: 'resolve-url-loader'
                    },
                    {
                        loader: 'sass-loader'
                    }
                ]
            },
            {
                test: /\.css$/,
                use: [
                    { loader: 'style-loader' },
                    { loader: 'css-loader' },
                ]
            },
            {
                test: /\.(eot|svg|ttf|woff|woff2)$/,
                use: {
                    loader: 'file-loader?name=[name].[ext]&outputPath=fonts/',
                }
            },
            {
                test: /\.(png|jpg)$/,
                use: {
                    loader: 'file-loader?name=[name].[ext]&outputPath=assets/',
                }
            }
        ]
    },
    plugins: [
        HtmlWebpackPluginConfig,
        new webpack.HotModuleReplacementPlugin(),
        new webpack.NamedModulesPlugin()
    ]
}

Im using webpack 3.1.0, webpack-dev-server 2.5.1 and react-router-dom 4.1.1


回答1:


Add <base href="/" /> to head tag of your index.html file

<head>
    <meta charset="utf-8">
    <title>React App Setup</title>
    <base href="/" />
</head>


来源:https://stackoverflow.com/questions/45133342/nested-react-router-4-routes-not-working-on-webpack-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!