Skip to content

Commit

Permalink
fix(css): fix stale css when reloading with hmr disabled (#10270) (#1…
Browse files Browse the repository at this point in the history
  • Loading branch information
russelldavis committed Jan 4, 2023
1 parent 32dee3c commit e5807c4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 33 deletions.
21 changes: 2 additions & 19 deletions packages/vite/src/node/server/hmr.ts
Expand Up @@ -128,15 +128,15 @@ export function updateModules(
file: string,
modules: ModuleNode[],
timestamp: number,
{ config, ws }: ViteDevServer,
{ config, ws, moduleGraph }: ViteDevServer,
afterInvalidation?: boolean,
): void {
const updates: Update[] = []
const invalidatedModules = new Set<ModuleNode>()
let needFullReload = false

for (const mod of modules) {
invalidate(mod, timestamp, invalidatedModules)
moduleGraph.invalidateModule(mod, invalidatedModules, timestamp, true)
if (needFullReload) {
continue
}
Expand Down Expand Up @@ -317,23 +317,6 @@ function propagateUpdate(
return false
}

function invalidate(mod: ModuleNode, timestamp: number, seen: Set<ModuleNode>) {
if (seen.has(mod)) {
return
}
seen.add(mod)
mod.lastHMRTimestamp = timestamp
mod.transformResult = null
mod.ssrModule = null
mod.ssrError = null
mod.ssrTransformResult = null
mod.importers.forEach((importer) => {
if (!importer.acceptedHmrDeps.has(mod)) {
invalidate(importer, timestamp, seen)
}
})
}

export function handlePrunedModules(
mods: Set<ModuleNode>,
{ ws }: ViteDevServer,
Expand Down
33 changes: 19 additions & 14 deletions packages/vite/src/node/server/moduleGraph.ts
Expand Up @@ -48,16 +48,6 @@ export class ModuleNode {
}
}

function invalidateSSRModule(mod: ModuleNode, seen: Set<ModuleNode>) {
if (seen.has(mod)) {
return
}
seen.add(mod)
mod.ssrModule = null
mod.ssrError = null
mod.importers.forEach((importer) => invalidateSSRModule(importer, seen))
}

export type ResolvedUrl = [
url: string,
resolvedId: string,
Expand Down Expand Up @@ -108,15 +98,30 @@ export class ModuleGraph {
mod: ModuleNode,
seen: Set<ModuleNode> = new Set(),
timestamp: number = Date.now(),
isHmr: boolean = false,
): void {
// Save the timestamp for this invalidation, so we can avoid caching the result of possible already started
// processing being done for this module
mod.lastInvalidationTimestamp = timestamp
if (seen.has(mod)) {
return
}
seen.add(mod)
if (isHmr) {
mod.lastHMRTimestamp = timestamp
} else {
// Save the timestamp for this invalidation, so we can avoid caching the result of possible already started
// processing being done for this module
mod.lastInvalidationTimestamp = timestamp
}
// Don't invalidate mod.info and mod.meta, as they are part of the processing pipeline
// Invalidating the transform result is enough to ensure this module is re-processed next time it is requested
mod.transformResult = null
mod.ssrTransformResult = null
invalidateSSRModule(mod, seen)
mod.ssrModule = null
mod.ssrError = null
mod.importers.forEach((importer) => {
if (!importer.acceptedHmrDeps.has(mod)) {
this.invalidateModule(importer, seen, timestamp, isHmr)
}
})
}

invalidateAll(): void {
Expand Down

0 comments on commit e5807c4

Please sign in to comment.