1

I'm using react with webpack. My app worked well before using webpack-dev-server. Only the home page works now, the others return "404 not found". Maybe there's a problem in webpack.config.js ?

var webpack = require("webpack");
var path = require("path");

var BUILD_DIR = path.resolve(__dirname, "public");
var APP_DIR = path.resolve(__dirname, "app");

var config = {
    entry: [
        "webpack/hot/only-dev-server",
        APP_DIR + "/main.js",
        "webpack-dev-server/client?localhost:8080"
    ],

    output: {
        path: BUILD_DIR + "/dist",
        filename: "bundle.js",
    },

    plugins: [
        new webpack.ProvidePlugin({
            $: "jquery",
            jQuery: "jquery"
        }),
        new webpack.HotModuleReplacementPlugin()
    ],

    module: {
        loaders: [...],
    },

    devServer: {
        noInfo: true,
        hot: true,
        inline: true
    },
}

module.exports = config;

And my script in package.json:

"start": "webpack --watch & webpack-dev-server --content-base public"

Webpack generates well bundle file. So i think the webpack-dev-server is the one who causes this problem.

Thank you !

2
  • Are they regular html files? I believe webpack dev server flattens everything into one directory and doesn't preserve source directory structure by default.
    – Andy Ray
    Commented Sep 24, 2016 at 1:01
  • I used react, sorry for not mention that. So i have index.html and bundle.js in folder "puclic" Commented Sep 24, 2016 at 1:23

2 Answers 2

4

I've found the answer. For someone who has the same problem, we need to add "historyApiFallback: true" to devServerConfig:

devServer: {
    noInfo: true,
    hot: true,
    inline: true,
    historyApiFallback: true
},

Comments

Enter at least 15 characters
Enter at least 15 characters
0

can you change your npm script

"start": "webpack --watch & webpack-dev-server --content-base public"

to

"start": "webpack --watch & webpack-dev-server --content-base public/"

and see what happens? Otherwise I will comeback and assist you.

6 Comments

Enter at least 15 characters
Sorry but nothing happens. I've changed "public" to "public/". Thanks
Enter at least 15 characters
Enter at least 15 characters
Can you give me more context? Are you using react router? When does that 404 page happen? For example, if you have example.com/test URL. Does 404 happen when you enter example.com/test directly into the URL bar?
Enter at least 15 characters
Enter at least 15 characters
Yes, i used react-router. "localhost:8080" gives me the home page, but "localhost:8080/login" returns 404 with the message "Cannot GET /login". I tried to click a link or type directly into the URL bar but it's always the same.
Enter at least 15 characters
Enter at least 15 characters
And i'm pretty sure that there is not problem with my code because before using webpack, everything worked well.
Enter at least 15 characters
Enter at least 15 characters
I think webpack-dev-server not support browserHistory maybe
Enter at least 15 characters
Enter at least 15 characters
|
Enter at least 15 characters

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.