Skip to content

Commit d06cc42

Browse files
fix(server): intercept ping requests (#13117)
Co-authored-by: 翠 / green <[email protected]>
1 parent 87e1f58 commit d06cc42

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

packages/vite/src/client/client.ts

+5
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,11 @@ async function waitForSuccessfulPing(
321321
try {
322322
await fetch(`${pingHostProtocol}://${hostAndPath}`, {
323323
mode: 'no-cors',
324+
headers: {
325+
// Custom headers won't be included in a request with no-cors so (ab)use one of the
326+
// safelisted headers to identify the ping request
327+
Accept: 'text/x-vite-ping',
328+
},
324329
})
325330
return true
326331
} catch {}

packages/vite/src/node/server/index.ts

+10
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,16 @@ export async function _createServer(
608608
// open in editor support
609609
middlewares.use('/__open-in-editor', launchEditorMiddleware())
610610

611+
// ping request handler
612+
// Keep the named function. The name is visible in debug logs via `DEBUG=connect:dispatcher ...`
613+
middlewares.use(function viteHMRPingMiddleware(req, res, next) {
614+
if (req.headers['accept'] === 'text/x-vite-ping') {
615+
res.writeHead(204).end()
616+
} else {
617+
next()
618+
}
619+
})
620+
611621
// serve static files under /public
612622
// this applies before the transform middleware so that these files are served
613623
// as-is without transforms.

0 commit comments

Comments
 (0)