How to configure Hot Reloading in Vue/Webpack to work on any test domain
Asked Answered
E

1

6

I create a backend project in a scripting language and a frontend using Vue-CLI. The project is tested in the domain example.localhost. In the test environment I run the Vue-CLI development version (localhost:8080) and use it on the project page example.localhost:80. This part works. "Hot Reloading" doesn't work.

On the Vue-CLI, Webpack, and SockJS project repositories, I saw issue threads about the failure of this module due to changes in SockJS, but a workaround was added that until the SockJS case is resolved.

I found a working configuration in my case, but this configuration is very dependent on the test domain name, which is what I would like to avoid. I would like it to work on other test domains and it doesn't work in its current form.

In previous attempts I even had a situation with CORS, but in preparing this example I was not able to get this state again. I do not accept the solution with disableHostCheck: true. The thing is, it must work without compromising security.


./docker-compose.yml

version: '3'

services:
  dev:
    image: gander/dev:7.3
    volumes:
      - ./public/index.html:/www/example.localhost/public/index.html
    ports:
      - 80:80

./public/index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Example</title>
</head>
<body>
<div id="app">Vue App</div>
<script src="http://localhost:8080/js/app.js"></script>
</body>
</html>

./package.json

{
  "name": "sandbox-vue-dev",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "serve": "vue-cli-service serve",
    "build": "vue-cli-service build"
  },
  "dependencies": {
    "core-js": "^3.4.3",
    "vue": "^2.6.10"
  },
  "devDependencies": {
    "@vue/cli-plugin-babel": "^4.1.0",
    "@vue/cli-service": "^4.1.0",
    "vue-template-compiler": "^2.6.10"
  }
}

./vue/vue.config.js, Hot Reloading not working:

module.exports = {
    outputDir: '../public/js/',
    publicPath: '/js/',
    filenameHashing: false,
    runtimeCompiler: true,
    chainWebpack(config) {
        config.optimization.splitChunks(false);
        config.output.filename('[name].js');
        ['html', 'preload', 'prefetch'].forEach(plugin => {
            config.plugins.delete(plugin);
        });
    }
};
module.exports = {
    devServer: {
        port: 8080
    },
    outputDir: '../public/js/',
    publicPath: '/js/',
    filenameHashing: false,
    runtimeCompiler: true,
    chainWebpack(config) {
        config.optimization.splitChunks(false);
        config.output.filename('[name].js');
        ['html', 'preload', 'prefetch'].forEach(plugin => {
            config.plugins.delete(plugin);
        });
    }
};
module.exports = {
    devServer: {
        port: 8080,
        headers: {
            'Access-Control-Allow-Origin': '.localhost'
        }
    },
    outputDir: '../public/js/',
    publicPath: '/js/',
    filenameHashing: false,
    runtimeCompiler: true,
    chainWebpack(config) {
        config.optimization.splitChunks(false);
        config.output.filename('[name].js');
        ['html', 'preload', 'prefetch'].forEach(plugin => {
            config.plugins.delete(plugin);
        });
    }
};
module.exports = {
    devServer: {
        port: 8080,
        host: 'localhost'
    },
    outputDir: '../public/js/',
    publicPath: '/js/',
    filenameHashing: false,
    runtimeCompiler: true,
    chainWebpack(config) {
        config.optimization.splitChunks(false);
        config.output.filename('[name].js');
        ['html', 'preload', 'prefetch'].forEach(plugin => {
            config.plugins.delete(plugin);
        });
    }
};

./vue/vue.config.js, Hot Reloading working, but hardcode domain:

module.exports = {
    devServer: {
        port: 8080,
        host: 'example.localhost'
    },
    outputDir: '../public/js/',
    publicPath: '/js/',
    filenameHashing: false,
    runtimeCompiler: true,
    chainWebpack(config) {
        config.optimization.splitChunks(false);
        config.output.filename('[name].js');
        ['html', 'preload', 'prefetch'].forEach(plugin => {
            config.plugins.delete(plugin);
        });
    }
};


Encephaloma answered 10/12, 2019 at 11:38 Comment(0)
L
1

I've been having the same problem today (January 11, 2023) when trying to integrate Django + VueJS through this tutorial: https://www.webucator.com/article/connecting-django-and-vue/.

In order to solve it, I just had to add the devServer.headers section to vue.config.js, as explained in https://www.fabiofranchino.com/log/how-to-enable-cors-on-vue-cli-serve/:

module.exports = {
  configureWebpack: {
    devServer: {
      headers: { 'Access-Control-Allow-Origin': '*' }
    }
  }
}

It worked perfectly for me.

Laud answered 11/1, 2023 at 3:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.