Skip to content

Commit

Permalink
fix: handle ping requests before they reach user middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
gtm-nayan committed May 8, 2023
1 parent c63ba3f commit 5d57541
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/vite/src/client/client.ts
Original file line number Diff line number Diff line change
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
9 changes: 9 additions & 0 deletions packages/vite/src/node/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,15 @@ export async function _createServer(
// open in editor support
middlewares.use('/__open-in-editor', launchEditorMiddleware())

// ping request handler
middlewares.use((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

0 comments on commit 5d57541

Please sign in to comment.