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: avoid early error when server is closed in ssr #13787

Merged
merged 2 commits into from Jul 11, 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
8 changes: 5 additions & 3 deletions packages/vite/src/node/server/index.ts
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
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
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
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My test could be flaky as I haven't figured out some case where there's actually unexpected but harmless errors. I'm ok with leaving this out in case it makes our CI unstable 🤔

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CI looks good for now. If we see it starts to bring issues we can remove it later.

console.log(e.stack)
res.status(500).end(e.stack)
}
Expand Down