webpack-contrib / webpack-hot-middleware

Webpack hot reloading you can attach to your own server
MIT License
2.34k stars 296 forks source link

Error on subpath/subroute #203

Open sarincasm opened 7 years ago

sarincasm commented 7 years ago

This works great when I am on the home page or a single level down on the URL - for eg: works for http://localhost:8080 works for http://localhost:8080/app

However, when I am on a page like http://localhost:8080/app/1

I get an error. When inspecting using the network tab on dev console - I see that it is trying to fetch a route which looks like /app/{}.hot-update.json rather than /{}.hot-update.json which is why it doesn't match the express route.

Not sure if this is an error with my config or some known issue. Thanks

glenjamin commented 7 years ago

This is likely to be a problem with your publicPath webpack setting

sarincasm commented 7 years ago

Hi.. thanks for your reply

here is what my setup looks like

Express route - server.js


const webpackConfig = require('./webpack.config').devConfig
            , webpack = require('webpack')
            , compiler = webpack(webpackConfig)

app.use(webpackDevMiddleware(compiler, {
            noInfo: false,
            publicPath: '/',
            filename: 'bundle.js',
            historyApiFallback: true,
            hot: true,
            stats: {colors: true}
        }))

Inside webpack.config.js


const srcDir = path.join(__dirname, 'public/jsx/')

const devConfig = {
    context: srcDir,
    entry: [
        'webpack-hot-middleware/client?path=/__webpack_hmr&timeout=20000',
        './entry.js'
    ],
    output: {
        path: path.join(__dirname, 'public'),
        filename: 'bundle.js'
    },
    module: {
        loaders: [
            {
                test: /\.js?/,
                include: srcDir,
                loaders: ['react-hot-loader', 'babel-loader']
            },
            {
                test: /\.less$/,
                use: [
                    'style-loader',
                    { loader: 'css-loader', options: { importLoaders: 1 } },
                    'less-loader'
                ]
            }
        ]
    },
    plugins: [
        new webpack.optimize.OccurrenceOrderPlugin(),
        new webpack.HotModuleReplacementPlugin(),
        new webpack.NoEmitOnErrorsPlugin()
    ]
}

Should public path be something else ?

beck commented 6 years ago

Had same issue, adding devConfig.output.publicPath = '/' fixed it for me. ¯\_(ツ)_/¯

beck commented 6 years ago

Reading the docs on public path config, it seems like setting publicPath to / should not be a requirement for hot reloading to work and this is indeed a bug in the middelware.

sarincasm commented 6 years ago

thanks @beck worked for me too

danielstorey commented 6 years ago

I had the same problem and can confirm that adding publicPath: "/" to my webpack config output fixed it.