0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-03-17 23:11:29 -05:00

refactor: simplifying it to remove the regex

This commit is contained in:
Tony Sullivan 2022-04-20 17:19:45 +02:00
parent 4755ce9dba
commit 63209ac803

View file

@ -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);