Skip to content

Commit

Permalink
fix: websocket proxies not the same as http (#4893)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeplus64 authored Sep 16, 2021
1 parent 3984569 commit 9260848
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions packages/vite/src/node/server/middlewares/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export function proxyMiddleware(
httpServer.on('upgrade', (req, socket, head) => {
const url = req.url!
for (const context in proxies) {
if (url.startsWith(context)) {
if (doesProxyContextMatchUrl(context, url)) {
const [proxy, opts] = proxies[context]
if (
(opts.ws || opts.target?.toString().startsWith('ws:')) &&
Expand All @@ -71,7 +71,9 @@ export function proxyMiddleware(
if (opts.rewrite) {
req.url = opts.rewrite(url)
}
debug(`${req.url} -> ws ${opts.target}`)
proxy.ws(req, socket, head)
return
}
}
}
Expand All @@ -82,10 +84,7 @@ export function proxyMiddleware(
return function viteProxyMiddleware(req, res, next) {
const url = req.url!
for (const context in proxies) {
if (
(context.startsWith('^') && new RegExp(context).test(url)) ||
url.startsWith(context)
) {
if (doesProxyContextMatchUrl(context, url)) {
const [proxy, opts] = proxies[context]
const options: HttpProxy.ServerOptions = {}

Expand Down Expand Up @@ -116,3 +115,10 @@ export function proxyMiddleware(
next()
}
}

function doesProxyContextMatchUrl(context: string, url: string): boolean {
return (
(context.startsWith('^') && new RegExp(context).test(url)) ||
url.startsWith(context)
)
}

0 comments on commit 9260848

Please sign in to comment.