Skip to content

Commit c0d6c60

Browse files
authored
fix: backport outdated optimized dep removed from module graph (#8534)
1 parent 078a7dc commit c0d6c60

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

packages/vite/src/node/plugins/importAnalysis.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,10 @@ import {
5050
optimizedDepNeedsInterop
5151
} from '../optimizer'
5252
import { checkPublicFile } from './asset'
53-
import { ERR_OUTDATED_OPTIMIZED_DEP } from './optimizedDeps'
53+
import {
54+
ERR_OUTDATED_OPTIMIZED_DEP,
55+
throwOutdatedRequest
56+
} from './optimizedDeps'
5457
import { isCSSRequest, isDirectCSSRequest } from './css'
5558

5659
const isDebug = !!process.env.DEBUG
@@ -170,6 +173,13 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
170173
// since we are already in the transform phase of the importer, it must
171174
// have been loaded so its entry is guaranteed in the module graph.
172175
const importerModule = moduleGraph.getModuleById(importer)!
176+
if (!importerModule && isOptimizedDepFile(importer, config)) {
177+
// Ids of optimized deps could be invalidated and removed from the graph
178+
// Return without transforming, this request is no longer valid, a full reload
179+
// is going to request this id again. Throwing an outdated error so we
180+
// properly finish the request with a 504 sent to the browser.
181+
throwOutdatedRequest(importer)
182+
}
173183

174184
if (!imports.length) {
175185
importerModule.isSelfAccepting = false

packages/vite/src/node/plugins/optimizedDeps.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export function optimizedDepsPlugin(): Plugin {
7373
}
7474
}
7575

76-
function throwProcessingError(id: string) {
76+
function throwProcessingError(id: string): never {
7777
const err: any = new Error(
7878
`Something unexpected happened while optimizing "${id}". ` +
7979
`The current page should have reloaded by now`
@@ -84,7 +84,7 @@ function throwProcessingError(id: string) {
8484
throw err
8585
}
8686

87-
function throwOutdatedRequest(id: string) {
87+
export function throwOutdatedRequest(id: string): never {
8888
const err: any = new Error(
8989
`There is a new version of the pre-bundle for "${id}", ` +
9090
`a page reload is going to ask for it.`

0 commit comments

Comments
 (0)