Configure webpack to allow proxied GitHub Codespaces connections

By default, webpack's development websocket rejects all incoming connections having an unexpected `Host` or `Origin` header:
https://webpack.js.org/configuration/dev-server/#devserverallowedhosts

This configuration sets `devServer.allowedHosts` to `[".preview.app.github.dev"]` if a GitHub Codespaces environment is detected.

Additionally, to handle how GitHub Codespaces forwards ports, webpack's configuration is changed to obtain the full websocket URL from the client script, instead of only the hostname:
https://webpack.js.org/configuration/dev-server/#websocketurl
This commit is contained in:
Stefano Pigozzi 2022-11-19 00:45:42 +00:00 committed by GitHub
parent 48790db447
commit 1413052d82
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -2,6 +2,7 @@ const path = require('path');
const common = require('./webpack.common'); const common = require('./webpack.common');
const { merge } = require('webpack-merge'); const { merge } = require('webpack-merge');
module.exports = merge(common, { module.exports = merge(common, {
mode: 'development', mode: 'development',
output: { output: {
@ -11,6 +12,10 @@ module.exports = merge(common, {
}, },
devServer: { devServer: {
historyApiFallback: true, historyApiFallback: true,
client: {
webSocketURL: "auto://0.0.0.0:0/ws"
},
allowedHosts: process.env.CODESPACES ? [".preview.app.github.dev"] : "auto"
}, },
module: { module: {
rules: [ rules: [