mirror of
https://github.com/vitejs/vite.git
synced 2024-11-21 22:59:10 +00:00
fix(server): intercept ping requests (#13117)
Co-authored-by: 翠 / green <green@sapphi.red>
This commit is contained in:
parent
87e1f58382
commit
d06cc42103
@ -321,6 +321,11 @@ async function waitForSuccessfulPing(
|
||||
try {
|
||||
await fetch(`${pingHostProtocol}://${hostAndPath}`, {
|
||||
mode: 'no-cors',
|
||||
headers: {
|
||||
// Custom headers won't be included in a request with no-cors so (ab)use one of the
|
||||
// safelisted headers to identify the ping request
|
||||
Accept: 'text/x-vite-ping',
|
||||
},
|
||||
})
|
||||
return true
|
||||
} catch {}
|
||||
|
@ -608,6 +608,16 @@ export async function _createServer(
|
||||
// open in editor support
|
||||
middlewares.use('/__open-in-editor', launchEditorMiddleware())
|
||||
|
||||
// ping request handler
|
||||
// Keep the named function. The name is visible in debug logs via `DEBUG=connect:dispatcher ...`
|
||||
middlewares.use(function viteHMRPingMiddleware(req, res, next) {
|
||||
if (req.headers['accept'] === 'text/x-vite-ping') {
|
||||
res.writeHead(204).end()
|
||||
} else {
|
||||
next()
|
||||
}
|
||||
})
|
||||
|
||||
// serve static files under /public
|
||||
// this applies before the transform middleware so that these files are served
|
||||
// as-is without transforms.
|
||||
|
Loading…
Reference in New Issue
Block a user