Skip to content

Commit

Permalink
refactor: replace client socket url with server request (#918)
Browse files Browse the repository at this point in the history
  • Loading branch information
edersoares authored Oct 20, 2020
1 parent 24336f4 commit bec6b3c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
6 changes: 2 additions & 4 deletions src/client/client.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// This file runs in the browser.

// injected by serverPluginHmr when served
declare const __PORT__: number
declare const __HOST__: string
declare const __MODE__: string
declare const __DEFINES__: Record<string, any>
;(window as any).process = (window as any).process || {}
Expand Down Expand Up @@ -29,9 +29,7 @@ console.log('[vite] connecting...')

declare var __VUE_HMR_RUNTIME__: HMRRuntime

const socketProtocol = location.protocol === 'https:' ? 'wss' : 'ws'
const socketUrl = `${socketProtocol}://${location.hostname}:${__PORT__}`
const socket = new WebSocket(socketUrl, 'vite-hmr')
const socket = new WebSocket(__HOST__, 'vite-hmr')

function warnFailedFetch(err: Error, path: string | string[]) {
if (!err.message.match('fetch')) {
Expand Down
5 changes: 4 additions & 1 deletion src/node/server/serverPluginClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ export const clientPlugin: ServerPlugin = ({ app, config }) => {

app.use(async (ctx, next) => {
if (ctx.path === clientPublicPath) {
const socketProtocol = ctx.protocol.toString() === 'https' ? 'wss' : 'ws'
const socketUrl = `${socketProtocol}://${ctx.host.toString()}`

ctx.type = 'js'
ctx.status = 200
ctx.body = clientCode.replace(`__PORT__`, ctx.port.toString())
ctx.body = clientCode.replace(`__HOST__`, JSON.stringify(socketUrl))
} else {
if (ctx.path === legacyPublicPath) {
console.error(
Expand Down

0 comments on commit bec6b3c

Please sign in to comment.