View Transitions: adding data-astro-reload to anchor element forces browser default behavior for navigation (#8217)
This commit is contained in:
parent
4bbcbac0b7
commit
c37632a20d
4 changed files with 32 additions and 0 deletions
8
.changeset/light-badgers-mate.md
Normal file
8
.changeset/light-badgers-mate.md
Normal 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>`
|
|
@ -294,6 +294,7 @@ const { fallback = 'animate' } = Astro.props as Props;
|
||||||
if (
|
if (
|
||||||
!link ||
|
!link ||
|
||||||
!(link instanceof HTMLAnchorElement) ||
|
!(link instanceof HTMLAnchorElement) ||
|
||||||
|
link.dataset.astroReload !== undefined ||
|
||||||
!link.href ||
|
!link.href ||
|
||||||
(link.target && link.target !== '_self') ||
|
(link.target && link.target !== '_self') ||
|
||||||
link.origin !== location.origin ||
|
link.origin !== location.origin ||
|
||||||
|
|
|
@ -9,4 +9,5 @@ import Layout from '../components/Layout.astro';
|
||||||
<span>go to 1</span>
|
<span>go to 1</span>
|
||||||
</div>
|
</div>
|
||||||
</a>
|
</a>
|
||||||
|
<a id="click-two" href="/two" data-astro-reload>load page / no navigation</a>
|
||||||
</Layout>
|
</Layout>
|
||||||
|
|
|
@ -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 have content').toHaveAttribute('data-astro-transition', 'forward');
|
||||||
await expect(h, 'should be absent').not.toHaveAttribute('class', /.*/);
|
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);
|
||||||
|
});
|
||||||
|
|
Loading…
Reference in a new issue