Skip to content

Commit 914467e

Browse files
committed
fix: hmr not working for snippet imports in dynamic routes
1 parent 7df3052 commit 914467e

File tree

2 files changed

+19
-22
lines changed

2 files changed

+19
-22
lines changed

src/node/markdownToVue.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ export interface MarkdownCompileResult {
3232
includes: string[]
3333
}
3434

35-
export function clearCache(id?: string) {
36-
if (!id) {
35+
export function clearCache(relativePath?: string) {
36+
if (!relativePath) {
3737
cache.clear()
3838
return
3939
}
4040

41-
id = JSON.stringify({ id }).slice(1)
42-
cache.find((_, key) => key.endsWith(id!) && cache.delete(key))
41+
relativePath = JSON.stringify({ relativePath }).slice(1)
42+
cache.find((_, key) => key.endsWith(relativePath!) && cache.delete(key))
4343
}
4444

4545
let __pages: string[] = []
@@ -114,12 +114,7 @@ export async function createMarkdownToVueRenderFn(
114114
file = rewrites.get(file) || file
115115
const relativePath = slash(path.relative(srcDir, file))
116116

117-
const cacheKey = JSON.stringify({
118-
src,
119-
ts,
120-
file: relativePath,
121-
id: fileOrig
122-
})
117+
const cacheKey = JSON.stringify({ src, ts, relativePath })
123118
if (isBuild || options.cache !== false) {
124119
const cached = cache.get(cacheKey)
125120
if (cached) {

src/node/plugin.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ export async function createVitePressPlugin(
201201
return processClientJS(code, id)
202202
}
203203
if (id.endsWith('.md')) {
204+
const relativePath = path.posix.relative(srcDir, id)
204205
// transform .md files into vueSrc so plugin-vue can handle it
205206
const { vueSrc, deadLinks, includes, pageData } = await markdownToVue(
206207
code,
@@ -210,15 +211,14 @@ export async function createVitePressPlugin(
210211
allDeadLinks.push(...deadLinks)
211212
if (includes.length) {
212213
includes.forEach((i) => {
213-
;(importerMap[slash(i)] ??= new Set()).add(id)
214+
;(importerMap[slash(i)] ??= new Set()).add(relativePath)
214215
this.addWatchFile(i)
215216
})
216217
}
217218
if (
218219
this.environment.mode === 'dev' &&
219220
this.environment.name === 'client'
220221
) {
221-
const relativePath = path.posix.relative(srcDir, id)
222222
const payload: PageDataPayload = {
223223
path: `/${siteConfig.rewrites.map[relativePath] || relativePath}`,
224224
pageData
@@ -260,13 +260,13 @@ export async function createVitePressPlugin(
260260
configDeps.forEach((file) => server.watcher.add(file))
261261
}
262262

263-
const onFileAddDelete = async (added: boolean, _file: string) => {
264-
const file = slash(_file)
263+
const onFileAddDelete = async (added: boolean, file: string) => {
264+
const relativePath = path.posix.relative(srcDir, file)
265265
// restart server on theme file creation / deletion
266-
if (themeRE.test(file)) {
266+
if (themeRE.test(relativePath)) {
267267
siteConfig.logger.info(
268268
c.green(
269-
`${path.relative(process.cwd(), _file)} ${added ? 'created' : 'deleted'}, restarting server...\n`
269+
`${relativePath} ${added ? 'created' : 'deleted'}, restarting server...\n`
270270
),
271271
{ clear: true, timestamp: true }
272272
)
@@ -275,10 +275,10 @@ export async function createVitePressPlugin(
275275
}
276276

277277
// update pages, dynamicRoutes and rewrites on md file creation / deletion
278-
if (file.endsWith('.md')) await resolvePages(siteConfig)
278+
if (relativePath.endsWith('.md')) await resolvePages(siteConfig)
279279

280-
if (!added && importerMap[file]) {
281-
delete importerMap[file]
280+
if (!added && importerMap[relativePath]) {
281+
delete importerMap[relativePath]
282282
}
283283
}
284284
server.watcher
@@ -410,9 +410,11 @@ export async function createVitePressPlugin(
410410
mod && modules.push(mod)
411411
}
412412

413-
importerMap[slash(file)]?.forEach((importer) => {
414-
clearCache(importer)
415-
const mod = this.environment.moduleGraph.getModuleById(importer)
413+
importerMap[slash(file)]?.forEach((relativePath) => {
414+
clearCache(relativePath)
415+
const mod = this.environment.moduleGraph.getModuleById(
416+
path.posix.join(srcDir, relativePath)
417+
)
416418
mod && modules.push(mod)
417419
})
418420

0 commit comments

Comments
 (0)