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',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Astro doesn't support external redirects, so adding on here is misleading because it won't work with other adapters.

Since you're adding the feature for the Vercel adapter, what about adding a new option to the adapter, maybe called externalRedirects?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not officially, but I ran into this when switching out the Cloudflare adapter, where a redirect like the one in the test works. That is what prompted me to create this PR.
Otherwise, I would strongly welcome external redirects becoming officially supported, as this should only require small adjustments, and it feels like the most intuitive approach.

},
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