Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(vercel): allow external redirects #404

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/clean-cows-draw.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/vercel': minor
---

Allow external redirects to be configured through Astro config
23 changes: 18 additions & 5 deletions packages/vercel/src/lib/redirects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,30 @@ function getReplacePattern(segments: RoutePart[][]) {
}

function getRedirectLocation(route: RouteData, config: AstroConfig): string {
let redirectPath: string;
let forceTrailingSlash = false;

if (route.redirectRoute) {
const pattern = getReplacePattern(route.redirectRoute.segments);
const path = config.trailingSlash === 'always' ? appendForwardSlash(pattern) : pattern;
return pathJoin(config.base, path);
redirectPath = getReplacePattern(route.redirectRoute.segments);
if (config.trailingSlash === 'always') forceTrailingSlash = true;
// biome-ignore lint/style/noUselessElse: <explanation>
} else if (typeof route.redirect === 'object') {
return pathJoin(config.base, route.redirect.destination);
redirectPath = route.redirect.destination;
// biome-ignore lint/style/noUselessElse: <explanation>
} else {
return pathJoin(config.base, route.redirect || '');
redirectPath = route.redirect || '';
}

// Is a full URL - do not transform
if (URL.canParse(redirectPath)) {
return redirectPath;
}

if (forceTrailingSlash) {
redirectPath = appendForwardSlash(redirectPath);
}

return pathJoin(config.base, redirectPath);
}

function getRedirectStatus(route: RouteData): number {
Expand Down
9 changes: 9 additions & 0 deletions packages/vercel/test/redirects.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ describe('Redirects', () => {
},
'/blog/[...slug]': '/team/articles/[...slug]',
'/Basic/http-2-0.html': '/posts/http2',
'/google': 'https://google.com',
},
trailingSlash: 'always',
});
Expand Down Expand Up @@ -56,6 +57,14 @@ describe('Redirects', () => {
assert.equal(staticRoute.status, 301);
});

it('define external redirects', async () => {
const config = await getConfig();

const route = config.routes.find((r) => r.src === '/google');
assert.equal(route.headers.Location, 'https://google.com');
assert.equal(route.status, 301);
});

it('defines dynamic routes', async () => {
const config = await getConfig();

Expand Down