0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-04-07 23:41:43 -05:00

fix: generate correct external redirects (#13480)

This commit is contained in:
Matt Kane 2025-03-21 10:48:15 +00:00 committed by GitHub
parent 9a0808cf18
commit 12cc4d88f2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 21 additions and 4 deletions

View file

@ -0,0 +1,5 @@
---
'@astrojs/vercel': patch
---
Fixes a bug that caused external redirects to fail

View file

@ -1,5 +1,5 @@
import nodePath from 'node:path';
import { removeLeadingForwardSlash } from '@astrojs/internal-helpers/path';
import { isRemotePath, removeLeadingForwardSlash } from '@astrojs/internal-helpers/path';
import type { AstroConfig, IntegrationResolvedRoute, RoutePart } from 'astro';
import type { Redirect } from '@vercel/routing-utils';
@ -92,10 +92,14 @@ function getRedirectLocation(route: IntegrationResolvedRoute, config: AstroConfi
return pathJoin(config.base, pattern);
}
if (typeof route.redirect === 'object') {
return pathJoin(config.base, route.redirect.destination);
const destination =
typeof route.redirect === 'object' ? route.redirect.destination : (route.redirect ?? '');
if (isRemotePath(destination)) {
return destination;
}
return pathJoin(config.base, route.redirect || '');
return pathJoin(config.base, destination);
}
function getRedirectStatus(route: IntegrationResolvedRoute): number {

View file

@ -16,6 +16,10 @@ describe('Redirects', () => {
status: 302,
destination: '/',
},
'/four': {
status: 302,
destination: 'http://example.com',
},
'/blog/[...slug]': '/team/articles/[...slug]',
'/Basic/http-2-0.html': '/posts/http2',
},
@ -43,6 +47,10 @@ describe('Redirects', () => {
const threeRoute = config.routes.find((r) => r.src === '^/three$');
assert.equal(threeRoute.headers.Location, '/');
assert.equal(threeRoute.status, 302);
const fourRoute = config.routes.find((r) => r.src === '^/four$');
assert.equal(fourRoute.headers.Location, 'http://example.com');
assert.equal(fourRoute.status, 302);
});
it('define redirects for static files', async () => {