Skip to content

Commit

Permalink
fix: avoid early error when server is closed in ssr (#13787)
Browse files Browse the repository at this point in the history
Co-authored-by: Bjorn Lu <[email protected]>
  • Loading branch information
patak-dev and bluwy authored Jul 11, 2023
1 parent 71ceb04 commit 89d01eb
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 9 deletions.
8 changes: 5 additions & 3 deletions packages/vite/src/node/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -464,9 +464,11 @@ export async function _createServer(
closeHttpServer(),
])
// Await pending requests. We throw early in transformRequest
// and in hooks if the server is closing, so the import analysis
// plugin stops pre-transforming static imports and this block
// is resolved sooner.
// and in hooks if the server is closing for non-ssr requests,
// so the import analysis plugin stops pre-transforming static
// imports and this block is resolved sooner.
// During SSR, we let pending requests finish to avoid exposing
// the server closed error to the users.
while (server._pendingRequests.size > 0) {
await Promise.allSettled(
[...server._pendingRequests.values()].map(
Expand Down
6 changes: 3 additions & 3 deletions packages/vite/src/node/server/pluginContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,7 @@ export async function createPluginContainer(
let id: string | null = null
const partial: Partial<PartialResolvedId> = {}
for (const plugin of getSortedPlugins('resolveId')) {
if (closed) throwClosedServerError()
if (closed && !ssr) throwClosedServerError()
if (!plugin.resolveId) continue
if (skip?.has(plugin)) continue

Expand Down Expand Up @@ -714,7 +714,7 @@ export async function createPluginContainer(
const ctx = new Context()
ctx.ssr = !!ssr
for (const plugin of getSortedPlugins('load')) {
if (closed) throwClosedServerError()
if (closed && !ssr) throwClosedServerError()
if (!plugin.load) continue
ctx._activePlugin = plugin
const handler =
Expand All @@ -738,7 +738,7 @@ export async function createPluginContainer(
const ctx = new TransformContext(id, code, inMap as SourceMap)
ctx.ssr = !!ssr
for (const plugin of getSortedPlugins('transform')) {
if (closed) throwClosedServerError()
if (closed && !ssr) throwClosedServerError()
if (!plugin.transform) continue
ctx._activePlugin = plugin
ctx._activeId = id
Expand Down
6 changes: 3 additions & 3 deletions packages/vite/src/node/server/transformRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export function transformRequest(
server: ViteDevServer,
options: TransformOptions = {},
): Promise<TransformResult | null> {
if (server._restartPromise) throwClosedServerError()
if (server._restartPromise && !options.ssr) throwClosedServerError()

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

Expand Down Expand Up @@ -256,7 +256,7 @@ async function loadAndTransform(
throw err
}

if (server._restartPromise) throwClosedServerError()
if (server._restartPromise && !ssr) throwClosedServerError()

// ensure module in graph after successful load
mod ??= await moduleGraph._ensureEntryFromUrl(url, ssr, undefined, resolved)
Expand Down Expand Up @@ -319,7 +319,7 @@ async function loadAndTransform(
}
}

if (server._restartPromise) throwClosedServerError()
if (server._restartPromise && !ssr) throwClosedServerError()

const result =
ssr && !server.config.experimental.skipSsrTransform
Expand Down
1 change: 1 addition & 0 deletions playground/ssr/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export async function createServer(
res.status(200).set({ 'Content-Type': 'text/html' }).end(html)
} catch (e) {
vite && vite.ssrFixStacktrace(e)
if (isTest) throw e
console.log(e.stack)
res.status(500).end(e.stack)
}
Expand Down

0 comments on commit 89d01eb

Please sign in to comment.