Skip to content

Commit ac6b9f4

Browse files
authored
fix(vite): Special handling for auth requests in proxy server during startup (#520)
1 parent f9a2cbd commit ac6b9f4

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

packages/vite/src/lib/getMergedConfig.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,22 @@ export function getMergedConfig(rwConfig: Config, rwPaths: Paths) {
8383
waitingForApiServer = false
8484
}, 2500)
8585

86-
proxy.on('error', (err, _req, res) => {
86+
proxy.on('error', (err, req, res) => {
8787
const isWaiting =
8888
waitingForApiServer && err.message.includes('ECONNREFUSED')
8989

9090
if (!isWaiting) {
9191
console.error(err)
9292
}
9393

94+
// This heuristic isn't perfect. It's written to handle dbAuth.
95+
// But it's very unlikely the user would have code that does
96+
// this exact request without it being a auth token request.
97+
// We need this special handling because we don't want the error
98+
// message below to be used as the auth token.
99+
const isAuthTokenRequest =
100+
isWaiting && req.url === '/auth?method=getToken'
101+
94102
const waitingMessage =
95103
'⌛ API Server launching, please refresh your page...'
96104
const genericMessage =
@@ -103,11 +111,16 @@ export function getMergedConfig(rwConfig: Config, rwPaths: Paths) {
103111
],
104112
}
105113

114+
// Use 203 to indicate that the response was modified by a proxy
106115
res.writeHead(203, {
107116
'Content-Type': 'application/json',
108117
'Cache-Control': 'no-cache',
109118
})
110-
res.write(JSON.stringify(responseBody))
119+
120+
if (!isAuthTokenRequest) {
121+
res.write(JSON.stringify(responseBody))
122+
}
123+
111124
res.end()
112125
})
113126
},

0 commit comments

Comments
 (0)