Skip to content

Commit

Permalink
feat(underscore-redirects): add base to input paths
Browse files Browse the repository at this point in the history
Co-authored-by: Sarah Rainsberger <5098874+sarah11918@users.noreply.github.com>
  • Loading branch information
alexanderniebuhr and sarah11918 committed Nov 1, 2023
1 parent 2da33b7 commit 24d67b8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/odd-maps-glow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/underscore-redirects': minor
---

Adds the base path as prefix for input paths
18 changes: 12 additions & 6 deletions packages/underscore-redirects/src/astro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function getRedirectStatus(route: RouteData): ValidRedirectStatus {
}

interface CreateRedirectsFromAstroRoutesParams {
config: Pick<AstroConfig, 'build' | 'output'>;
config: Pick<AstroConfig, 'build' | 'output' | 'base'>;
/**
* Maps a `RouteData` to a dynamic target
*/
Expand All @@ -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();

Expand All @@ -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,
Expand All @@ -53,15 +59,15 @@ 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,
});
} else {
_redirects.add({
dynamic: false,
input: route.pathname,
input: `${base}${route.pathname}`,
target: dynamicTarget,
status: 200,
weight: 2,
Expand Down Expand Up @@ -94,15 +100,15 @@ export function createRedirectsFromAstroRoutes({
}
_redirects.add({
dynamic: true,
input: pattern,
input: `${base}${route.pattern}`,
target,
status: route.type === 'redirect' ? 301 : 200,
weight: 1,
});
} else {
_redirects.add({
dynamic: true,
input: pattern,
input: `${base}${route.pattern}`,
target: dynamicTarget,
status: 200,
weight: 1,
Expand Down

0 comments on commit 24d67b8

Please sign in to comment.