Skip to content

Commit cdb7bfa

Browse files
martrappflorian-lefebvrelilnasy
authoredJan 8, 2024
Fixes an issue where view transitions to the 404-page did not work (#9642)
* Add new e2e test * Ensure cloned Response keeps its headers * Add change set * Update changeset * Update .changeset/big-knives-own.md Co-authored-by: Florian Lefebvre <contact@florian-lefebvre.dev> * Update packages/astro/src/vite-plugin-astro-server/route.ts Co-authored-by: Arsh <69170106+lilnasy@users.noreply.github.com> --------- Co-authored-by: Florian Lefebvre <contact@florian-lefebvre.dev> Co-authored-by: Arsh <69170106+lilnasy@users.noreply.github.com>
1 parent 3011f15 commit cdb7bfa

File tree

5 files changed

+36
-1
lines changed

5 files changed

+36
-1
lines changed
 

‎.changeset/big-knives-own.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"astro": patch
3+
---
4+
5+
Fixes an issue where View Transitions did not work when navigating to the 404 page
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
import Layout from '../components/Layout.astro';
3+
---
4+
<Layout>
5+
<p id="FourOhFour">Page not found</p>
6+
</Layout>
7+
</script>

‎packages/astro/e2e/fixtures/view-transitions/src/pages/one.astro

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import Layout from '../components/Layout.astro';
1111
<a id="click-self" href="">go to top</a>
1212
<a id="click-redirect-two" href="/redirect-two">go to redirect 2</a>
1313
<a id="click-redirect-external" href="/redirect-external">go to a redirect external</a>
14+
<a id="click-404" href="/undefined-page">go to undefined page</a>
1415

1516
<div id="test">test content</div>
1617
</Layout>

‎packages/astro/e2e/view-transitions.test.js

+19
Original file line numberDiff line numberDiff line change
@@ -1187,4 +1187,23 @@ test.describe('View Transitions', () => {
11871187

11881188
expect(requests).toHaveLength(0);
11891189
});
1190+
1191+
test('view transition should also work with 404 page', async ({ page, astro }) => {
1192+
const loads = [];
1193+
page.addListener('load', (p) => {
1194+
loads.push(p.title());
1195+
});
1196+
1197+
// Go to page 1
1198+
await page.goto(astro.resolveUrl('/one'));
1199+
let p = page.locator('#one');
1200+
await expect(p, 'should have content').toHaveText('Page 1');
1201+
1202+
// go to 404
1203+
await page.click('#click-404');
1204+
p = page.locator('#FourOhFour');
1205+
await expect(p, 'should have content').toHaveText('Page not found');
1206+
1207+
expect(loads.length, 'There should only be 1 page load').toEqual(1);
1208+
});
11901209
});

‎packages/astro/src/vite-plugin-astro-server/route.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,10 @@ export async function handleRoute({
370370
// Apply the `status` override to the response object before responding.
371371
// Response.status is read-only, so a clone is required to override.
372372
if (status && response.status !== status && (status === 404 || status === 500)) {
373-
response = new Response(response.body, { ...response, status });
373+
response = new Response(response.body, {
374+
status: status,
375+
headers: response.headers
376+
});
374377
}
375378
await writeSSRResult(request, response, incomingResponse);
376379
}

0 commit comments

Comments
 (0)
Please sign in to comment.