Skip to content

Commit d475afc

Browse files
lfilhoematipico
andauthored
Fix relative redirects regression. Closes #13607 (#13616)
Co-authored-by: Emanuele Stoppa <my.burning@gmail.com>
1 parent 516de7d commit d475afc

3 files changed

Lines changed: 15 additions & 6 deletions

File tree

.changeset/tasty-cities-warn.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro': patch
3+
---
4+
5+
Fixes a regression where relative static redirects didn't work as expected.

packages/astro/src/core/routing/manifest/create.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -357,12 +357,9 @@ function createRedirectRoutes(
357357
destination = to.destination;
358358
}
359359

360-
// URLs that don't start with leading slash should be considered external
361-
if (!destination.startsWith('/')) {
362-
// check if the link starts with http or https; if not, log a warning
363-
if (!/^https?:\/\//.test(destination) && !URL.canParse(destination)) {
364-
throw new AstroError(UnsupportedExternalRedirect);
365-
}
360+
// check if the link starts with http or https; if not, throw an error
361+
if (URL.canParse(destination) && !/^https?:\/\//.test(destination)) {
362+
throw new AstroError(UnsupportedExternalRedirect);
366363
}
367364

368365
routes.push({

packages/astro/test/redirects.test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ describe('Astro.redirect', () => {
131131
'/more/old/[dynamic]/[route]': '/more/[dynamic]/[route]',
132132
'/more/old/[...spread]': '/more/new/[...spread]',
133133
'/external/redirect': 'https://example.com/',
134+
'/relative/redirect': '../../test',
134135
},
135136
});
136137
await fixture.build();
@@ -214,6 +215,12 @@ describe('Astro.redirect', () => {
214215
assert.equal(html.includes('http-equiv="refresh'), true);
215216
assert.equal(html.includes('url=https://example.com/'), true);
216217
});
218+
219+
it('supports redirecting to a relative destination', async () => {
220+
const html = await fixture.readFile('/relative/redirect/index.html');
221+
assert.equal(html.includes('http-equiv="refresh'), true);
222+
assert.equal(html.includes('url=../../test'), true);
223+
});
217224
});
218225

219226
describe('dev', () => {

0 commit comments

Comments
 (0)