Skip to content

Commit cf6290f

Browse files
committed
fix(serve): use explicit sockjs url unless inside a container
close vuejs#1974
1 parent 6b77af6 commit cf6290f

File tree

1 file changed

+23
-2
lines changed
  • packages/@vue/cli-service/lib/commands

1 file changed

+23
-2
lines changed

packages/@vue/cli-service/lib/commands/serve.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ module.exports = (api, options) => {
3232
const isInContainer = checkInContainer()
3333
const isProduction = process.env.NODE_ENV === 'production'
3434

35+
const url = require('url')
3536
const path = require('path')
3637
const chalk = require('chalk')
3738
const webpack = require('webpack')
@@ -91,8 +92,19 @@ module.exports = (api, options) => {
9192
// inject dev & hot-reload middleware entries
9293
if (!isProduction) {
9394
const sockjsUrl = publicUrl
95+
// explicitly configured via devServer.public
9496
? `?${publicUrl}/sockjs-node`
95-
: ``
97+
: isInContainer
98+
// can't infer public netowrk url if inside a container...
99+
// use client-side inference (note this would break with non-root baseUrl)
100+
? ``
101+
// otherwise infer the url
102+
: `?` + url.format({
103+
protocol,
104+
port,
105+
hostname: urls.lanUrlForConfig || 'localhost',
106+
pathname: '/sockjs-node'
107+
})
96108
const devClients = [
97109
// dev server client
98110
require.resolve(`webpack-dev-server/client`) + sockjsUrl,
@@ -195,7 +207,16 @@ module.exports = (api, options) => {
195207
} else {
196208
console.log()
197209
console.log(chalk.yellow(` It seems you are running Vue CLI inside a container.`))
198-
console.log(chalk.yellow(` Access the dev server via ${protocol}://localhost:<your container's external mapped port>.`))
210+
if (!publicUrl && options.baseUrl && options.baseUrl !== '/') {
211+
console.log()
212+
console.log(chalk.yellow(` Since you are using a non-root baseUrl, the hot-reload socket`))
213+
console.log(chalk.yellow(` will not be able to infer the correct URL to connect. You should`))
214+
console.log(chalk.yellow(` explicitly specify the URL via ${chalk.blue(`devServer.public`)}.`))
215+
console.log()
216+
}
217+
console.log(chalk.yellow(` Access the dev server via ${chalk.cyan(
218+
`${protocol}://localhost:<your container's external mapped port>${options.baseUrl}`
219+
)}`))
199220
}
200221
console.log()
201222

0 commit comments

Comments
 (0)