mirror of
https://github.com/withastro/astro.git
synced 2025-01-06 22:10:10 -05:00
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>
This commit is contained in:
parent
f6b53a2e69
commit
6bc2153d0f
2 changed files with 17 additions and 6 deletions
5
.changeset/odd-maps-glow.md
Normal file
5
.changeset/odd-maps-glow.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'@astrojs/underscore-redirects': patch
|
||||
---
|
||||
|
||||
Adds the base path as prefix for input paths
|
|
@ -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
|
||||
*/
|
||||
|
@ -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,
|
||||
|
|
Loading…
Reference in a new issue