Skip to content

Commit c46e4b7

Browse files
committed
fix: hmr not updating page data in rewritten paths and file path is wrong in mdit for dynamic routes
closes #4172
1 parent e6f9dd3 commit c46e4b7

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

src/node/markdownToVue.ts

+18-6
Original file line numberDiff line numberDiff line change
@@ -57,21 +57,33 @@ export async function createMarkdownToVueRenderFn(
5757
base,
5858
siteConfig?.logger
5959
)
60+
6061
pages = pages.map((p) => slash(p.replace(/\.md$/, '')))
6162

63+
const dynamicRoutes = new Map(
64+
siteConfig?.dynamicRoutes?.routes.map((r) => [
65+
r.fullPath,
66+
path.join(srcDir, r.route)
67+
]) || []
68+
)
69+
70+
const rewrites = new Map(
71+
Object.entries(siteConfig?.rewrites.map || {}).map(([key, value]) => [
72+
path.join(srcDir, key),
73+
path.join(srcDir, value!)
74+
]) || []
75+
)
76+
6277
return async (
6378
src: string,
6479
file: string,
6580
publicDir: string
6681
): Promise<MarkdownCompileResult> => {
67-
const fileOrig = file
68-
const alias =
69-
siteConfig?.rewrites.map[file] || // virtual dynamic path file
70-
siteConfig?.rewrites.map[file.slice(srcDir.length + 1)]
71-
file = alias ? path.join(srcDir, alias) : file
82+
const fileOrig = dynamicRoutes.get(file) || file
83+
file = rewrites.get(file) || file
7284
const relativePath = slash(path.relative(srcDir, file))
73-
const cacheKey = JSON.stringify({ src, file: fileOrig })
7485

86+
const cacheKey = JSON.stringify({ src, file: relativePath })
7587
if (isBuild || options.cache !== false) {
7688
const cached = cache.get(cacheKey)
7789
if (cached) {

src/node/plugin.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -402,8 +402,9 @@ export async function createVitePressPlugin(
402402
config.publicDir
403403
)
404404

405+
const relativePath = slash(path.relative(srcDir, file))
405406
const payload: PageDataPayload = {
406-
path: `/${slash(path.relative(srcDir, file))}`,
407+
path: `/${siteConfig.rewrites.map[relativePath] || relativePath}`,
407408
pageData
408409
}
409410

0 commit comments

Comments
 (0)