diff --git a/src/node/config.ts b/src/node/config.ts index 912f64c4..25b93061 100644 --- a/src/node/config.ts +++ b/src/node/config.ts @@ -185,7 +185,7 @@ export interface SiteConfig pages: string[] dynamicRoutes: { routes: ResolvedRouteConfig[] - fileToModulesMap: Record + fileToModulesMap: Record> } rewrites: { map: Record diff --git a/src/node/plugins/dynamicRoutesPlugin.ts b/src/node/plugins/dynamicRoutesPlugin.ts index b8c2f0a0..6d0af4ac 100644 --- a/src/node/plugins/dynamicRoutesPlugin.ts +++ b/src/node/plugins/dynamicRoutesPlugin.ts @@ -67,7 +67,7 @@ export const dynamicRoutesPlugin = async ( if (matched) { const { route, params, content } = matched const routeFile = normalizePath(path.resolve(config.root, route)) - config.dynamicRoutes.fileToModulesMap[routeFile].push(id) + config.dynamicRoutes.fileToModulesMap[routeFile].add(id) let baseContent = fs.readFileSync(routeFile, 'utf-8') @@ -90,9 +90,12 @@ export const dynamicRoutesPlugin = async ( async handleHotUpdate(ctx) { const mods = config.dynamicRoutes.fileToModulesMap[ctx.file] if (mods) { - // path loader module updated, reset loaded routes - if (/\.paths\.[jt]s$/.test(ctx.file)) { - await resolvePages(config.srcDir, config.userConfig) + // path loader module or deps updated, reset loaded routes + if (!/\.md$/.test(ctx.file)) { + Object.assign( + config, + await resolvePages(config.srcDir, config.userConfig) + ) } for (const id of mods) { ctx.modules.push(server.moduleGraph.getModuleById(id)!) @@ -106,7 +109,7 @@ export async function resolveDynamicRoutes( routes: string[] ): Promise { const pendingResolveRoutes: Promise[] = [] - const routeFileToModulesMap: Record = {} + const routeFileToModulesMap: Record> = {} for (const route of routes) { // locate corresponding route paths file @@ -138,10 +141,17 @@ export async function resolveDynamicRoutes( } if (mod) { - // route md file and route paths loader file point to the same array - routeFileToModulesMap[mod.path] = routeFileToModulesMap[ - path.resolve(route) - ] = [] + // this array represents the virtual modules affected by this route + const matchedModuleIds = (routeFileToModulesMap[ + normalizePath(path.resolve(route)) + ] = new Set()) + + // each dependency (including the loader module itself) also point to the + // same array + for (const dep of mod.dependencies) { + routeFileToModulesMap[normalizePath(path.resolve(dep))] = + matchedModuleIds + } const resolveRoute = async (): Promise => { const loader = mod.config.paths diff --git a/src/node/plugins/staticDataPlugin.ts b/src/node/plugins/staticDataPlugin.ts index adf86722..e8fa50d7 100644 --- a/src/node/plugins/staticDataPlugin.ts +++ b/src/node/plugins/staticDataPlugin.ts @@ -71,7 +71,7 @@ export const staticDataPlugin: Plugin = { const res = await loadConfigFromFile({} as any, id) // record deps for hmr - if (res) { + if (server && res) { for (const dep of res.dependencies) { depToLoaderModuleIdMap[normalizePath(path.resolve(dep))] = id } @@ -121,7 +121,7 @@ export const staticDataPlugin: Plugin = { }, handleHotUpdate(ctx) { - const file = normalizePath(ctx.file) + const file = ctx.file // dependency of data loader changed // (note the dep array includes the loader file itself)