From 7f2592f41bd5b8a5fbe1a72bcab569ceaf38b8ab Mon Sep 17 00:00:00 2001 From: Alexander Niebuhr Date: Wed, 1 Nov 2023 16:01:31 +0100 Subject: [PATCH] feat(underscore-redirects): add `base` to input paths (#8953) Co-authored-by: Sarah Rainsberger <5098874+sarah11918@users.noreply.github.com> Co-authored-by: Bjorn Lu <34116392+bluwy@users.noreply.github.com> --- .changeset/odd-maps-glow.md | 5 +++++ packages/underscore-redirects/src/astro.ts | 18 ++++++++++++------ 2 files changed, 17 insertions(+), 6 deletions(-) create mode 100644 .changeset/odd-maps-glow.md diff --git a/.changeset/odd-maps-glow.md b/.changeset/odd-maps-glow.md new file mode 100644 index 0000000000000..c8df7fa511940 --- /dev/null +++ b/.changeset/odd-maps-glow.md @@ -0,0 +1,5 @@ +--- +'@astrojs/underscore-redirects': patch +--- + +Adds the base path as prefix for input paths diff --git a/packages/underscore-redirects/src/astro.ts b/packages/underscore-redirects/src/astro.ts index b378eb9559f32..8d91847e0dc7e 100644 --- a/packages/underscore-redirects/src/astro.ts +++ b/packages/underscore-redirects/src/astro.ts @@ -12,7 +12,7 @@ function getRedirectStatus(route: RouteData): ValidRedirectStatus { } interface CreateRedirectsFromAstroRoutesParams { - config: Pick; + config: Pick; /** * Maps a `RouteData` to a dynamic target */ @@ -28,6 +28,12 @@ export function createRedirectsFromAstroRoutes({ routeToDynamicTargetMap, dir, }: CreateRedirectsFromAstroRoutesParams) { + const base = + config.base && config.base !== '/' + ? config.base.endsWith('/') + ? config.base.slice(0, -1) + : config.base + : ''; const output = config.output; const _redirects = new Redirects(); @@ -39,7 +45,7 @@ export function createRedirectsFromAstroRoutes({ // from the user if provided. _redirects.add({ dynamic: false, - input: route.pathname, + input: `${base}${route.pathname}`, target: typeof route.redirect === 'object' ? route.redirect.destination : route.redirect, status: getRedirectStatus(route), weight: 2, @@ -53,7 +59,7 @@ export function createRedirectsFromAstroRoutes({ } else if (route.distURL) { _redirects.add({ dynamic: false, - input: route.pathname, + input: `${base}${route.pathname}`, target: prependForwardSlash(route.distURL.toString().replace(dir.toString(), '')), status: 200, weight: 2, @@ -61,7 +67,7 @@ export function createRedirectsFromAstroRoutes({ } else { _redirects.add({ dynamic: false, - input: route.pathname, + input: `${base}${route.pathname}`, target: dynamicTarget, status: 200, weight: 2, @@ -94,7 +100,7 @@ export function createRedirectsFromAstroRoutes({ } _redirects.add({ dynamic: true, - input: pattern, + input: `${base}${route.pattern}`, target, status: route.type === 'redirect' ? 301 : 200, weight: 1, @@ -102,7 +108,7 @@ export function createRedirectsFromAstroRoutes({ } else { _redirects.add({ dynamic: true, - input: pattern, + input: `${base}${route.pattern}`, target: dynamicTarget, status: 200, weight: 1,