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:
parent
9a0808cf18
commit
12cc4d88f2
3 changed files with 21 additions and 4 deletions
5
.changeset/little-doors-tickle.md
Normal file
5
.changeset/little-doors-tickle.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'@astrojs/vercel': patch
|
||||
---
|
||||
|
||||
Fixes a bug that caused external redirects to fail
|
|
@ -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 {
|
||||
|
|
|
@ -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 () => {
|
||||
|
|
Loading…
Add table
Reference in a new issue