19

I'm working with webpack-dev-server to do hot reloading. However in my console it keeps saying Invalid Host/Origin header

The setup I have in my webpack config is as follows:

'use strict'

const webpack = require('webpack')
const { VueLoaderPlugin } = require('vue-loader')
const HtmlWebpackPlugin = require('html-webpack-plugin')

module.exports = {
  mode: 'development',

  devServer: {
    headers: {
      'Access-Control-Allow-Origin': '*'
    },
    hot: true,
    watchOptions: {
      poll: true
    }
  },
  module: {
    rules: [
      ...
    ]
  },
  plugins: [
    new webpack.HotModuleReplacementPlugin(),
    ...
  ]
}

How do I solve this issue so that it works in my dev environment? Would love the hear this as currently I have to refresh the page constantly.

Maarten Raaijmakers
  • 615
  • 2
  • 8
  • 20

6 Answers6

34

This issue is probably caused by this webpack-dev-server issue that has been fixed recently.

To avoid getting the Invalid Host/Origin header error add this to your devServer entry:

disableHostCheck: true
pors
  • 3,856
  • 36
  • 33
7

Set allowedHosts, https://webpack.js.org/configuration/dev-server/#devserverallowedhosts. For example, if your web page is xyz.google.com, then just add a host .google.com to it.

Rfank2019
  • 297
  • 4
  • 10
4

Is your page hosted at a different domain than your webpack files are being served from? If so, you might just need to add the page's domain to the devServer.allowedHosts option.

broken-e
  • 790
  • 7
  • 10
2

If this is on Firefox, you can fix it by setting network.http.sendOriginHeader to 1 in about:config.

This "Invalid Host/Origin header" error occurs on Firefox because Firefox still does not sent Origin header with same-origin POST requests, and webpack-dev-server insists on having it.

(Really, webpack-dev-server should only check the Origin header when it's present.)

Avoid disableHostCheck! It's dangerous even in your local dev environment! It allows attackers to connect to your dev environment when you visit an unrelated site with malicious code. It seems really bad.

DS.
  • 22,632
  • 6
  • 47
  • 54
-1

Update your webpack-dev-server to >= version 3.1.14 (as of Jan-2019)

npm i -D webpack-dev-server@3.1.14

The respective issue occuring in ~3.1.11 has been patched.

No change to the webpack configuration is required.

chouxy-dev
  • 47
  • 3
-3

In the end it was an update on the package that didn't come through properly and was solved after installing the newest build.

Maarten Raaijmakers
  • 615
  • 2
  • 8
  • 20