mirror of
https://github.com/withastro/astro.git
synced 2025-03-31 23:31:30 -05:00
Fall back to page reload when redirected to a cross origin URL (#11302)
* fall back to page reload when redirected to a cross origin URL * Make redirect work with dynamically assigned ports * undo
This commit is contained in:
parent
8ce66f2ef7
commit
0622567326
5 changed files with 43 additions and 1 deletions
5
.changeset/small-badgers-juggle.md
Normal file
5
.changeset/small-badgers-juggle.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'astro': patch
|
||||
---
|
||||
|
||||
Fixes an issue with the view transition router when redirecting to an URL with different origin.
|
|
@ -11,6 +11,7 @@ import Layout from '../components/Layout.astro';
|
|||
<a id="click-self" href="">go to top</a>
|
||||
<a id="click-redirect-two" href="/redirect-two">go to redirect 2</a>
|
||||
<a id="click-redirect-external" href="/redirect-external">go to a redirect external</a>
|
||||
<a id="click-redirect" href="/redirect">redirect cross-origin</a>
|
||||
<a id="click-404" href="/undefined-page">go to undefined page</a>
|
||||
<custom-a id="custom-click-two">
|
||||
<template shadowrootmode="open">
|
||||
|
|
|
@ -0,0 +1,10 @@
|
|||
---
|
||||
const myURL = Astro.request.url;
|
||||
const redirectTo = (myURL.startsWith("http://localhost")
|
||||
? myURL.replace("http://localhost","http://127.0.0.1")
|
||||
: myURL.replace("http://127.0.0.1", "http://localhost"))
|
||||
.replace("redirect","two");
|
||||
return Astro.redirect(redirectTo);
|
||||
---
|
||||
|
||||
<h1>Should not see this</h1>
|
|
@ -788,6 +788,26 @@ test.describe('View Transitions', () => {
|
|||
expect(loads.length, 'There should be 2 page loads').toEqual(2);
|
||||
});
|
||||
|
||||
test('Cross origin redirects do not raise errors', async ({ page, astro }) => {
|
||||
|
||||
let consoleErrors = [];
|
||||
page.on('console', (msg) => {
|
||||
if (msg.type() === 'error') {
|
||||
consoleErrors.push(msg.text());
|
||||
}
|
||||
});
|
||||
// Go to page 1
|
||||
await page.goto(astro.resolveUrl('/one'));
|
||||
let p = page.locator('#one');
|
||||
await expect(p, 'should have content').toHaveText('Page 1');
|
||||
|
||||
await page.click('#click-redirect');
|
||||
p = page.locator('#two');
|
||||
await expect(p, 'should have content').toHaveText('Page 2');
|
||||
|
||||
expect(consoleErrors.length, 'There should be no errors').toEqual(0);
|
||||
});
|
||||
|
||||
test('client:only styles are retained on transition (1/2)', async ({ page, astro }) => {
|
||||
const totalExpectedStyles = 9;
|
||||
|
||||
|
|
|
@ -418,7 +418,13 @@ async function transition(
|
|||
}
|
||||
// if there was a redirection, show the final URL in the browser's address bar
|
||||
if (response.redirected) {
|
||||
preparationEvent.to = new URL(response.redirected);
|
||||
const redirectedTo = new URL(response.redirected);
|
||||
// but do not redirect cross origin
|
||||
if (redirectedTo.origin !== preparationEvent.to.origin) {
|
||||
preparationEvent.preventDefault();
|
||||
return;
|
||||
}
|
||||
preparationEvent.to = redirectedTo;
|
||||
}
|
||||
|
||||
parser ??= new DOMParser();
|
||||
|
|
Loading…
Add table
Reference in a new issue