View Transitions: adding data-astro-reload to anchor element forces browser default behavior for navigation (#8217)

This commit is contained in:
Martin Trapp 2023-08-24 19:36:58 +02:00 committed by GitHub
parent 4bbcbac0b7
commit c37632a20d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 0 deletions

View file

@ -0,0 +1,8 @@
---
'astro': patch
---
Specify `data-astro-reload` (no value) on an anchor element to force the browser to ignore view transitions and fall back to default loading.
This is helpful when navigating to documents that have different content-types, e.g. application/pdf, where you want to use the build in viewer of the browser.
Example: `<a href='/my.pdf' data-astro-reload>...</a>`

View file

@ -294,6 +294,7 @@ const { fallback = 'animate' } = Astro.props as Props;
if (
!link ||
!(link instanceof HTMLAnchorElement) ||
link.dataset.astroReload !== undefined ||
!link.href ||
(link.target && link.target !== '_self') ||
link.origin !== location.origin ||

View file

@ -9,4 +9,5 @@ import Layout from '../components/Layout.astro';
<span>go to 1</span>
</div>
</a>
<a id="click-two" href="/two" data-astro-reload>load page / no navigation</a>
</Layout>

View file

@ -419,3 +419,25 @@ test('Navigation also swaps the attributes of the document root', async ({ page,
await expect(h, 'should have content').toHaveAttribute('data-astro-transition', 'forward');
await expect(h, 'should be absent').not.toHaveAttribute('class', /.*/);
});
test('Link with data-astro-reload attribute should trigger page load, no tranistion', async ({
page,
astro,
}) => {
const loads = [];
page.addListener('load', (p) => {
loads.push(p.title());
});
// Go to page 4
await page.goto(astro.resolveUrl('/four'));
let p = page.locator('#four');
await expect(p, 'should have content').toHaveText('Page 4');
// go to page 2
await page.click('#click-two');
p = page.locator('#two');
await expect(p, 'should have content').toHaveText('Page 2');
expect(loads.length, 'There should be 2 page load').toEqual(2);
});