Skip to content

Commit 89d01eb

Browse files
patak-devbluwy
andauthoredJul 11, 2023
fix: avoid early error when server is closed in ssr (#13787)
Co-authored-by: Bjorn Lu <34116392+bluwy@users.noreply.github.com>
1 parent 71ceb04 commit 89d01eb

File tree

4 files changed

+12
-9
lines changed

4 files changed

+12
-9
lines changed
 

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

+5-3
Original file line numberDiff line numberDiff line change
@@ -464,9 +464,11 @@ export async function _createServer(
464464
closeHttpServer(),
465465
])
466466
// Await pending requests. We throw early in transformRequest
467-
// and in hooks if the server is closing, so the import analysis
468-
// plugin stops pre-transforming static imports and this block
469-
// is resolved sooner.
467+
// and in hooks if the server is closing for non-ssr requests,
468+
// so the import analysis plugin stops pre-transforming static
469+
// imports and this block is resolved sooner.
470+
// During SSR, we let pending requests finish to avoid exposing
471+
// the server closed error to the users.
470472
while (server._pendingRequests.size > 0) {
471473
await Promise.allSettled(
472474
[...server._pendingRequests.values()].map(

‎packages/vite/src/node/server/pluginContainer.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,7 @@ export async function createPluginContainer(
649649
let id: string | null = null
650650
const partial: Partial<PartialResolvedId> = {}
651651
for (const plugin of getSortedPlugins('resolveId')) {
652-
if (closed) throwClosedServerError()
652+
if (closed && !ssr) throwClosedServerError()
653653
if (!plugin.resolveId) continue
654654
if (skip?.has(plugin)) continue
655655

@@ -714,7 +714,7 @@ export async function createPluginContainer(
714714
const ctx = new Context()
715715
ctx.ssr = !!ssr
716716
for (const plugin of getSortedPlugins('load')) {
717-
if (closed) throwClosedServerError()
717+
if (closed && !ssr) throwClosedServerError()
718718
if (!plugin.load) continue
719719
ctx._activePlugin = plugin
720720
const handler =
@@ -738,7 +738,7 @@ export async function createPluginContainer(
738738
const ctx = new TransformContext(id, code, inMap as SourceMap)
739739
ctx.ssr = !!ssr
740740
for (const plugin of getSortedPlugins('transform')) {
741-
if (closed) throwClosedServerError()
741+
if (closed && !ssr) throwClosedServerError()
742742
if (!plugin.transform) continue
743743
ctx._activePlugin = plugin
744744
ctx._activeId = id

‎packages/vite/src/node/server/transformRequest.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export function transformRequest(
4747
server: ViteDevServer,
4848
options: TransformOptions = {},
4949
): Promise<TransformResult | null> {
50-
if (server._restartPromise) throwClosedServerError()
50+
if (server._restartPromise && !options.ssr) throwClosedServerError()
5151

5252
const cacheKey = (options.ssr ? 'ssr:' : options.html ? 'html:' : '') + url
5353

@@ -256,7 +256,7 @@ async function loadAndTransform(
256256
throw err
257257
}
258258

259-
if (server._restartPromise) throwClosedServerError()
259+
if (server._restartPromise && !ssr) throwClosedServerError()
260260

261261
// ensure module in graph after successful load
262262
mod ??= await moduleGraph._ensureEntryFromUrl(url, ssr, undefined, resolved)
@@ -319,7 +319,7 @@ async function loadAndTransform(
319319
}
320320
}
321321

322-
if (server._restartPromise) throwClosedServerError()
322+
if (server._restartPromise && !ssr) throwClosedServerError()
323323

324324
const result =
325325
ssr && !server.config.experimental.skipSsrTransform

‎playground/ssr/server.js

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ export async function createServer(
5757
res.status(200).set({ 'Content-Type': 'text/html' }).end(html)
5858
} catch (e) {
5959
vite && vite.ssrFixStacktrace(e)
60+
if (isTest) throw e
6061
console.log(e.stack)
6162
res.status(500).end(e.stack)
6263
}

0 commit comments

Comments
 (0)
Please sign in to comment.