From e002f4f4a578ae63156e756abac0487f42b4cdcd Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Thu, 16 Dec 2021 20:25:55 +0800 Subject: [PATCH] fix: pending reload never timeout (#6120) --- .../src/node/server/middlewares/transform.ts | 28 +++++++++++-------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/packages/vite/src/node/server/middlewares/transform.ts b/packages/vite/src/node/server/middlewares/transform.ts index a68308f391e363..df7f21223a19b9 100644 --- a/packages/vite/src/node/server/middlewares/transform.ts +++ b/packages/vite/src/node/server/middlewares/transform.ts @@ -74,23 +74,29 @@ export function transformMiddleware( !req.url?.startsWith(CLIENT_PUBLIC_PATH) && !req.url?.includes('vite/dist/client') ) { - // missing dep pending reload, hold request until reload happens - server._pendingReload.then(() => - // If the refresh has not happened after timeout, Vite considers - // something unexpected has happened. In this case, Vite - // returns an empty response that will error. - setTimeout(() => { - // Don't do anything if response has already been sent - if (res.writableEnded) return + try { + // missing dep pending reload, hold request until reload happens + await Promise.race([ + server._pendingReload, + // If the refresh has not happened after timeout, Vite considers + // something unexpected has happened. In this case, Vite + // returns an empty response that will error. + new Promise((_, reject) => + setTimeout(reject, NEW_DEPENDENCY_BUILD_TIMEOUT) + ) + ]) + } catch { + // Don't do anything if response has already been sent + if (!res.writableEnded) { // status code request timeout res.statusCode = 408 res.end( `

[vite] Something unexpected happened while optimizing "${req.url}"

` + `

The current page should have reloaded by now

` ) - }, NEW_DEPENDENCY_BUILD_TIMEOUT) - ) - return + } + return + } } let url = decodeURI(removeTimestampQuery(req.url!)).replace(