From 65daaa7022c8ad27b8fc203b5396f840da88f713 Mon Sep 17 00:00:00 2001 From: Miguel Sandoval Date: Wed, 13 Dec 2023 16:53:00 -0800 Subject: [PATCH] Avoid duplicate base prefix applied on ssr when assets plugin applied before html transforms --- .../src/node/server/middlewares/indexHtml.ts | 28 ++++++++++--------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/packages/vite/src/node/server/middlewares/indexHtml.ts b/packages/vite/src/node/server/middlewares/indexHtml.ts index 4ebc2a41e86bfe..85375323e4c2d7 100644 --- a/packages/vite/src/node/server/middlewares/indexHtml.ts +++ b/packages/vite/src/node/server/middlewares/indexHtml.ts @@ -138,19 +138,21 @@ const processNodeUrl = ( } if ( - (url[0] === '/' && url[1] !== '/') || - // #3230 if some request url (localhost:3000/a/b) return to fallback html, the relative assets - // path will add `/a/` prefix, it will caused 404. - // - // skip if url contains `:` as it implies a url protocol or Windows path that we don't want to replace. - // - // rewrite `./index.js` -> `localhost:5173/a/index.js`. - // rewrite `../index.js` -> `localhost:5173/index.js`. - // rewrite `relative/index.js` -> `localhost:5173/a/relative/index.js`. - ((url[0] === '.' || isBareRelative(url)) && - originalUrl && - originalUrl !== '/' && - htmlPath === '/index.html') + // avoid duplicate base prefix when applying html transforms after assets plugin + !url.startsWith(config.base) && + ((url[0] === '/' && url[1] !== '/') || + // #3230 if some request url (localhost:3000/a/b) return to fallback html, the relative assets + // path will add `/a/` prefix, it will caused 404. + // + // skip if url contains `:` as it implies a url protocol or Windows path that we don't want to replace. + // + // rewrite `./index.js` -> `localhost:5173/a/index.js`. + // rewrite `../index.js` -> `localhost:5173/index.js`. + // rewrite `relative/index.js` -> `localhost:5173/a/relative/index.js`. + ((url[0] === '.' || isBareRelative(url)) && + originalUrl && + originalUrl !== '/' && + htmlPath === '/index.html')) ) { url = path.posix.join(config.base, url) }