From 3d0fafba545f4b5028cf43d86027dd44dab14421 Mon Sep 17 00:00:00 2001 From: Divyansh Singh <40380293+brc-dd@users.noreply.github.com> Date: Mon, 11 Aug 2025 10:48:56 +0530 Subject: [PATCH] fix: hmr of style blocks in dynamic routes (#4903) --- src/node/plugins/dynamicRoutesPlugin.ts | 27 ++++++++++++++++--------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/src/node/plugins/dynamicRoutesPlugin.ts b/src/node/plugins/dynamicRoutesPlugin.ts index f10aa27f..7b3b326d 100644 --- a/src/node/plugins/dynamicRoutesPlugin.ts +++ b/src/node/plugins/dynamicRoutesPlugin.ts @@ -5,6 +5,7 @@ import pm from 'picomatch' import { loadConfigFromFile, normalizePath, + type EnvironmentModuleGraph, type EnvironmentModuleNode, type Logger, type Plugin @@ -130,6 +131,7 @@ export const dynamicRoutesPlugin = async ( ): Promise => { return { name: 'vitepress:dynamic-routes', + enforce: 'pre', resolveId(id) { if (!id.endsWith('.md')) return @@ -177,11 +179,7 @@ export const dynamicRoutesPlugin = async ( const normalizedFile = normalizePath(file) // Trigger update if a module or its dependencies changed. - for (const id of moduleGraph.delete(normalizedFile)) { - routeModuleCache.delete(id) - const mod = this.environment.moduleGraph.getModuleById(id) - if (mod) modules.push(mod) - } + modules.push(...getModules(normalizedFile, this.environment.moduleGraph)) // Also check if the file matches any custom watch patterns. let watchedFileChanged = false @@ -192,11 +190,7 @@ export const dynamicRoutesPlugin = async ( ) { route.routes = undefined watchedFileChanged = true - - for (const id of moduleGraph.delete(file)) { - const mod = this.environment.moduleGraph.getModuleById(id) - if (mod) modules.push(mod) - } + modules.push(...getModules(file, this.environment.moduleGraph, false)) } } @@ -355,3 +349,16 @@ async function resolveDynamicRoutes( return resolvedRoutes } + +function getModules( + id: string, + envModuleGraph: EnvironmentModuleGraph, + deleteFromRouteModuleCache = true +) { + const modules: EnvironmentModuleNode[] = [] + for (const file of moduleGraph.delete(id)) { + deleteFromRouteModuleCache && routeModuleCache.delete(file) + modules.push(...(envModuleGraph.getModulesByFile(file)?.values() ?? [])) + } + return modules +}