From 3ddd5d1c2f3c42676524184501aed0e9969785cd Mon Sep 17 00:00:00 2001 From: patak Date: Wed, 20 Mar 2024 22:22:50 +0100 Subject: [PATCH] fix: early return on closed server only for warmup --- packages/vite/src/node/server/index.ts | 34 +++++++++++-------- .../src/node/server/middlewares/transform.ts | 16 --------- .../vite/src/node/server/pluginContainer.ts | 16 --------- .../vite/src/node/server/transformRequest.ts | 12 +++---- 4 files changed, 25 insertions(+), 53 deletions(-) diff --git a/packages/vite/src/node/server/index.ts b/packages/vite/src/node/server/index.ts index 8a508dbbd46c5c..c34374076a95ff 100644 --- a/packages/vite/src/node/server/index.ts +++ b/packages/vite/src/node/server/index.ts @@ -53,7 +53,7 @@ import { getEnvFilesForMode } from '../env' import type { FetchResult } from '../../runtime/types' import { ssrFetchModule } from '../ssr/ssrFetchModule' import type { PluginContainer } from './pluginContainer' -import { ERR_CLOSED_SERVER, createPluginContainer } from './pluginContainer' +import { createPluginContainer } from './pluginContainer' import type { WebSocketServer } from './ws' import { createWebSocketServer } from './ws' import { baseMiddleware } from './middlewares/base' @@ -512,20 +512,24 @@ export async function _createServer( return transformRequest(url, server, options) }, async warmupRequest(url, options) { - await transformRequest(url, server, options).catch((e) => { - if ( - e?.code === ERR_OUTDATED_OPTIMIZED_DEP || - e?.code === ERR_CLOSED_SERVER - ) { - // these are expected errors - return - } - // Unexpected error, log the issue but avoid an unhandled exception - server.config.logger.error(`Pre-transform error: ${e.message}`, { - error: e, - timestamp: true, - }) - }) + if (server._restartPromise) { + // The server is restarting, avoid warming up this request as it won't be used + // and it will trigger the processing of other requests + return + } + await transformRequest(url, server, { ...options, warmup: true }).catch( + (e) => { + if (e?.code === ERR_OUTDATED_OPTIMIZED_DEP) { + // expected error + return + } + // Unexpected error, log the issue but avoid an unhandled exception + server.config.logger.error(`Pre-transform error: ${e.message}`, { + error: e, + timestamp: true, + }) + }, + ) }, transformIndexHtml(url, html, originalUrl) { return devHtmlTransformFn(server, url, html, originalUrl) diff --git a/packages/vite/src/node/server/middlewares/transform.ts b/packages/vite/src/node/server/middlewares/transform.ts index 12a440d4c10774..ce4d2bd6384323 100644 --- a/packages/vite/src/node/server/middlewares/transform.ts +++ b/packages/vite/src/node/server/middlewares/transform.ts @@ -31,7 +31,6 @@ import { ERR_OPTIMIZE_DEPS_PROCESSING_ERROR, ERR_OUTDATED_OPTIMIZED_DEP, } from '../../plugins/optimizedDeps' -import { ERR_CLOSED_SERVER } from '../pluginContainer' import { getDepsOptimizer } from '../../optimizer' import { cleanUrl, unwrapId, withTrailingSlash } from '../../../shared/utils' import { NULL_BYTE_PLACEHOLDER } from '../../../shared/constants' @@ -239,21 +238,6 @@ export function transformMiddleware( // error but a normal part of the missing deps discovery flow return } - if (e?.code === ERR_CLOSED_SERVER) { - // Skip if response has already been sent - if (!res.writableEnded) { - res.statusCode = 504 // status code request timeout - res.statusMessage = 'Outdated Request' - res.end() - } - // We don't need to log an error in this case, the request - // is outdated because new dependencies were discovered and - // the new pre-bundle dependencies have changed. - // A full-page reload has been issued, and these old requests - // can't be properly fulfilled. This isn't an unexpected - // error but a normal part of the missing deps discovery flow - return - } if (e?.code === ERR_FILE_NOT_FOUND_IN_OPTIMIZED_DEP_DIR) { // Skip if response has already been sent if (!res.writableEnded) { diff --git a/packages/vite/src/node/server/pluginContainer.ts b/packages/vite/src/node/server/pluginContainer.ts index 4ea3d0e6b51ea0..4ff2712d6b143f 100644 --- a/packages/vite/src/node/server/pluginContainer.ts +++ b/packages/vite/src/node/server/pluginContainer.ts @@ -84,18 +84,6 @@ import type { ModuleGraph, ModuleNode } from './moduleGraph' const noop = () => {} -export const ERR_CLOSED_SERVER = 'ERR_CLOSED_SERVER' - -export function throwClosedServerError(): never { - const err: any = new Error( - 'The server is being restarted or closed. Request is outdated', - ) - err.code = ERR_CLOSED_SERVER - // This error will be caught by the transform middleware that will - // send a 504 status code request timeout - throw err -} - export interface PluginContainerOptions { cwd?: string output?: OutputOptions @@ -642,7 +630,6 @@ export async function createPluginContainer( options: await (async () => { let options = rollupOptions for (const optionsHook of getSortedPluginHooks('options')) { - if (closed) throwClosedServerError() options = (await handleHookPromise( optionsHook.call(minimalContext, options), @@ -675,7 +662,6 @@ export async function createPluginContainer( let id: string | null = null const partial: Partial = {} for (const plugin of getSortedPlugins('resolveId')) { - if (closed && !ssr) throwClosedServerError() if (!plugin.resolveId) continue if (skip?.has(plugin)) continue @@ -737,7 +723,6 @@ export async function createPluginContainer( const ctx = new Context() ctx.ssr = !!ssr for (const plugin of getSortedPlugins('load')) { - if (closed && !ssr) throwClosedServerError() if (!plugin.load) continue ctx._activePlugin = plugin const handler = getHookHandler(plugin.load) @@ -762,7 +747,6 @@ export async function createPluginContainer( const ctx = new TransformContext(id, code, inMap as SourceMap) ctx.ssr = !!ssr for (const plugin of getSortedPlugins('transform')) { - if (closed && !ssr) throwClosedServerError() if (!plugin.transform) continue ctx._activePlugin = plugin ctx._activeId = id diff --git a/packages/vite/src/node/server/transformRequest.ts b/packages/vite/src/node/server/transformRequest.ts index 5dc6e3921215af..d4399ed8c5cea6 100644 --- a/packages/vite/src/node/server/transformRequest.ts +++ b/packages/vite/src/node/server/transformRequest.ts @@ -28,7 +28,6 @@ import { injectSourcesContent, } from './sourcemap' import { isFileServingAllowed } from './middlewares/static' -import { throwClosedServerError } from './pluginContainer' export const ERR_LOAD_URL = 'ERR_LOAD_URL' export const ERR_LOAD_PUBLIC_URL = 'ERR_LOAD_PUBLIC_URL' @@ -48,6 +47,10 @@ export interface TransformResult { export interface TransformOptions { ssr?: boolean html?: boolean + /** + * @internal + */ + warmup?: boolean } export function transformRequest( @@ -55,8 +58,6 @@ export function transformRequest( server: ViteDevServer, options: TransformOptions = {}, ): Promise { - if (server._restartPromise && !options.ssr) throwClosedServerError() - const cacheKey = (options.ssr ? 'ssr:' : options.html ? 'html:' : '') + url // This module may get invalidated while we are processing it. For example @@ -315,7 +316,7 @@ async function loadAndTransform( throw err } - if (server._restartPromise && !ssr) throwClosedServerError() + if (options.warmup && server._restartPromise) return null // ensure module in graph after successful load mod ??= await moduleGraph._ensureEntryFromUrl(url, ssr, undefined, resolved) @@ -386,8 +387,7 @@ async function loadAndTransform( } } } - - if (server._restartPromise && !ssr) throwClosedServerError() + if (options.warmup && server._restartPromise) return null const result = ssr && !server.config.experimental.skipSsrTransform