From 63209ac803d2fef607f3cef4672ebb3e159962a2 Mon Sep 17 00:00:00 2001 From: Tony Sullivan Date: Wed, 20 Apr 2022 17:19:45 +0200 Subject: [PATCH] refactor: simplifying it to remove the regex --- .../astro/src/vite-plugin-astro-server/index.ts | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/packages/astro/src/vite-plugin-astro-server/index.ts b/packages/astro/src/vite-plugin-astro-server/index.ts index f671c48e43..d3db71112e 100644 --- a/packages/astro/src/vite-plugin-astro-server/index.ts +++ b/packages/astro/src/vite-plugin-astro-server/index.ts @@ -112,20 +112,14 @@ async function handle404Response( html = subpathNotUsedTemplate(devRoot, pathname); } else { // HACK: redirect without the base path for assets in publicDir - // Only redirect if: - // (1) astroConfig.base was provided - // (2) pathname begins with the base path - // (3) pathname isn't `{basepath}` or `{basepath}/` - const baseRegex = new RegExp(`${config.base}\/\d*$`); - const shouldRedirect = config.base && config.base !== './' - && pathname.match(baseRegex); + const redirectTo = config.base && config.base !== './' + && pathname.replace(config.base, ''); - if (shouldRedirect) { + if (redirectTo && redirectTo !== '/') { const response = new Response(null, { status: 301, headers: { - // substring at 1 to maintain the leading / - Location: pathname.replace(config.base.substring(1), ''), + Location: redirectTo, }, }); await writeWebResponse(res, response);