From c46e4b784ddb9ce3bd1cfcc3de1d1d676535cb5b Mon Sep 17 00:00:00 2001 From: Divyansh Singh <40380293+brc-dd@users.noreply.github.com> Date: Sun, 1 Sep 2024 17:02:12 +0530 Subject: [PATCH] fix: hmr not updating page data in rewritten paths and file path is wrong in mdit for dynamic routes closes #4172 --- src/node/markdownToVue.ts | 24 ++++++++++++++++++------ src/node/plugin.ts | 3 ++- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/node/markdownToVue.ts b/src/node/markdownToVue.ts index 642dabd7..458837c0 100644 --- a/src/node/markdownToVue.ts +++ b/src/node/markdownToVue.ts @@ -57,21 +57,33 @@ export async function createMarkdownToVueRenderFn( base, siteConfig?.logger ) + pages = pages.map((p) => slash(p.replace(/\.md$/, ''))) + const dynamicRoutes = new Map( + siteConfig?.dynamicRoutes?.routes.map((r) => [ + r.fullPath, + path.join(srcDir, r.route) + ]) || [] + ) + + const rewrites = new Map( + Object.entries(siteConfig?.rewrites.map || {}).map(([key, value]) => [ + path.join(srcDir, key), + path.join(srcDir, value!) + ]) || [] + ) + return async ( src: string, file: string, publicDir: string ): Promise => { - const fileOrig = file - const alias = - siteConfig?.rewrites.map[file] || // virtual dynamic path file - siteConfig?.rewrites.map[file.slice(srcDir.length + 1)] - file = alias ? path.join(srcDir, alias) : file + const fileOrig = dynamicRoutes.get(file) || file + file = rewrites.get(file) || file const relativePath = slash(path.relative(srcDir, file)) - const cacheKey = JSON.stringify({ src, file: fileOrig }) + const cacheKey = JSON.stringify({ src, file: relativePath }) if (isBuild || options.cache !== false) { const cached = cache.get(cacheKey) if (cached) { diff --git a/src/node/plugin.ts b/src/node/plugin.ts index c91d9357..8132ad82 100644 --- a/src/node/plugin.ts +++ b/src/node/plugin.ts @@ -402,8 +402,9 @@ export async function createVitePressPlugin( config.publicDir ) + const relativePath = slash(path.relative(srcDir, file)) const payload: PageDataPayload = { - path: `/${slash(path.relative(srcDir, file))}`, + path: `/${siteConfig.rewrites.map[relativePath] || relativePath}`, pageData }