Nested React router 4 routes not working on Webpack 3
Asked Answered
U

2

9

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

University answered 16/7, 2017 at 21:9 Comment(2)
can you describe what you mean by doesn't work? What happens when you load the / and use a <Link to="events/2" />Frequent
@Frequent i've edited the question to add more details about the errorUniversity
G
33

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

<head>
    <meta charset="utf-8">
    <title>React App Setup</title>
    <base href="/" />
</head>
Geraint answered 1/2, 2018 at 17:30 Comment(4)
live saving advise. Thank youHrutkay
Best, You save my time. Thanks a lot.Selfgovernment
Can you please briefly explain the answer? Why it was failing, and what this line does to resolve the issue?Orland
Works, but how. Can anyone elaborate?Eury
X
0

I tried the <base href="/" /> solution. It worked for my bundle file but not my css. Then, I noticed the real problem. Both of my paths were relative, so the reload caused them to load at the wrong path.

Solution

  <head>
    <link rel="stylesheet" href="/index.css" />
  </head>
  <body>
    <div id="app"></div>
    <script src="/bundle.js"></script>
  </body>

Notice, the non-relative paths /index.css and /bundle.js. Before, they were ./index.css and ./bundle.js.

Xylo answered 8/4, 2023 at 3:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.