Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix transitions with non-recommended headers #9464

Merged
merged 5 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/khaki-ducks-give.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes an edge case with view transitions where some spec-compliant `Content-Type` headers would cause a valid HTML response to be ignored.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Layout from '../components/Layout.astro';
<a id="click-one" href="#test">test</a>
<a id="click-two" href="/two">go to 2</a>
<a id="click-three" href="/three">go to 3</a>
<a id="click-seven" href="/seven">go to 7</a>
<a id="click-longpage" href="/long-page">go to long page</a>
<a id="click-self" href="">go to top</a>
<a id="click-redirect-two" href="/redirect-two">go to redirect 2</a>
Expand Down
10 changes: 10 additions & 0 deletions packages/astro/e2e/fixtures/view-transitions/src/pages/seven.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
import Layout from '../components/Layout.astro';

Astro.response.headers.set('Content-Type', 'text/html ; charset=utf-8');
---
<Layout link="/one.css">
<p id="seven">Page 7</p>

<div id="test">test content</div>
</Layout>
19 changes: 19 additions & 0 deletions packages/astro/e2e/view-transitions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,25 @@ test.describe('View Transitions', () => {
expect(loads.length, 'There should only be 1 page load').toEqual(1);
});

test('Clicking on a link to a page with non-recommended headers', async ({page, astro}) => {
const loads = [];
page.addListener('load', (p) => {
loads.push(p.title());
});

// Go to page 4
await page.goto(astro.resolveUrl('/one'));
let p = page.locator('#one');
await expect(p, 'should have content').toHaveText('Page 1');

// Go to page 1
await page.click('#click-seven');
p = page.locator('#seven');
await expect(p, 'should have content').toHaveText('Page 7');

expect(loads.length, 'There should only be 1 page load').toEqual(1);
});

test('Moving to a page without ViewTransitions triggers a full page navigation', async ({
page,
astro,
Expand Down
3 changes: 2 additions & 1 deletion packages/astro/src/transitions/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,9 @@ async function fetchHTML(
): Promise<null | { html: string; redirected?: string; mediaType: DOMParserSupportedType }> {
try {
const res = await fetch(href, init);
const contentType = res.headers.get('content-type') ?? '';
// drop potential charset (+ other name/value pairs) as parser needs the mediaType
const mediaType = res.headers.get('content-type')?.replace(/;.*$/, '');
const mediaType = contentType.split(';', 1)[0].trim();
Fryuni marked this conversation as resolved.
Show resolved Hide resolved
// the DOMParser can handle two types of HTML
if (mediaType !== 'text/html' && mediaType !== 'application/xhtml+xml') {
// everything else (e.g. audio/mp3) will be handled by the browser but not by us
Expand Down
Loading