From ce8d139a8e70e4d0a8d06711c50119990b041078 Mon Sep 17 00:00:00 2001 From: Divyansh Singh <40380293+brc-dd@users.noreply.github.com> Date: Thu, 22 Sep 2022 01:03:31 +0530 Subject: [PATCH] feat(build): allow using `transformIndexHtml` (#1380) --- src/node/plugin.ts | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/node/plugin.ts b/src/node/plugin.ts index dab227f4..d00dc06d 100644 --- a/src/node/plugin.ts +++ b/src/node/plugin.ts @@ -35,6 +35,9 @@ const isPageChunk = ( chunk.facadeModuleId.endsWith('.md') ) +const cleanUrl = (url: string): string => + url.replace(/#.*$/s, '').replace(/\?.*$/s, '') + export async function createVitePressPlugin( siteConfig: SiteConfig, ssr = false, @@ -176,12 +179,12 @@ export async function createVitePressPlugin( // serve our index.html after vite history fallback return () => { - server.middlewares.use((req, res, next) => { - if (req.url!.replace(/\?.*$/, '').endsWith('.html')) { + server.middlewares.use(async (req, res, next) => { + const url = req.url && cleanUrl(req.url) + if (url?.endsWith('.html')) { res.statusCode = 200 res.setHeader('Content-Type', 'text/html') - res.end(` - + let html = ` @@ -193,7 +196,9 @@ export async function createVitePressPlugin(
-`) +` + html = await server.transformIndexHtml(url, html, req.originalUrl) + res.end(html) return } next()