Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(server): intercept ping requests #13117

Merged
merged 2 commits into from May 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/vite/src/client/client.ts
Expand Up @@ -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 {}
Expand Down
10 changes: 10 additions & 0 deletions packages/vite/src/node/server/index.ts
Expand Up @@ -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.
Expand Down