Skip to content

Commit

Permalink
fix(css): fix stale css when reloading with hmr disabled (vitejs#10270)
Browse files Browse the repository at this point in the history
In the non-hmr path, when invalidating a module, it wasn't invalidating
the importers. This meant that CSS dynamically generated based on a
module's contents (e.g., what tailwind does) would not regenerate
when refreshing a page.

The HMR code path already invalidated importers, which is why the bug
didn't exist with HMR enabled. This change makes the invalidation
consistent between HMR and non-HMR.
  • Loading branch information
russelldavis committed Dec 27, 2022
1 parent 13ac37d commit 8325293
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 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 @@ -488,9 +488,11 @@ export async function createServer(
if (file.endsWith('/package.json')) {
return invalidatePackageData(packageCache, file)
}
// invalidate module graph cache on file change
moduleGraph.onFileChange(file)
if (serverConfig.hmr !== false) {
if (serverConfig.hmr === false) {
// Invalidate module graph cache on file change.
// The HMR path below does its own invalidation.
moduleGraph.onFileChange(file)
} else {
try {
await handleHMRUpdate(file, server)
} catch (err) {
Expand Down
20 changes: 9 additions & 11 deletions packages/vite/src/node/server/moduleGraph.ts
Original file line number Diff line number Diff line change
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 @@ -109,14 +99,22 @@ export class ModuleGraph {
seen: Set<ModuleNode> = new Set(),
timestamp: number = Date.now(),
): void {
if (seen.has(mod)) {
return
}
seen.add(mod)
// 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) =>
this.invalidateModule(importer, seen, timestamp),
)
}

invalidateAll(): void {
Expand Down

0 comments on commit 8325293

Please sign in to comment.