...">

Nested React 4 router not working on Webpack 3

Generally, I cannot get routes like

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

      

to work, and as I read it looks like the package in index.html should be absolute, however I am using HtmlWebpackPlugin so the package is injected as a relative path.

I tried to set up the output config for webpack like this:

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

      

But that doesn't work either.

If I try this route: http: // localhost: 8080 / events / 7 I get a 404 error when I try 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

+3


source to share


1 answer


Add <base href="/" />

to the main tag of your index.html file



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

      

+7


source







All Articles