Skip to content
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/fix-prefetch-tap-nested-elements.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes `data-astro-prefetch="tap"` not triggering when clicking nested elements (e.g. `<span>`, `<img>`, `<svg>`) inside an anchor tag.
4 changes: 4 additions & 0 deletions packages/astro/e2e/fixtures/prefetch/src/pages/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
<br>
<a id="prefetch-tap" href="/prefetch-tap" data-astro-prefetch="tap">tap</a>
<br>
<a id="prefetch-tap-nested" href="/prefetch-tap-nested" data-astro-prefetch="tap"><span>tap nested</span></a>
<br>
<a id="prefetch-default-nested" href="/prefetch-default-nested"><span>default nested</span></a>
<br>
<a id="prefetch-hover" href="/prefetch-hover" data-astro-prefetch="hover">hover</a>
<br>
<button id="prefetch-manual">manual</button>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>Prefetch default nested</h1>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>Prefetch tap nested</h1>
62 changes: 62 additions & 0 deletions packages/astro/e2e/prefetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,19 @@ test.describe('Prefetch (default)', () => {
expect(reqUrls).toContainEqual('/prefetch-tap');
});

test('data-astro-prefetch="tap" should prefetch on tap when clicking a nested child element', async ({
page,
astro,
}) => {
await page.goto(astro.resolveUrl('/'));
expect(reqUrls).not.toContainEqual('/prefetch-tap-nested');
await Promise.all([
page.waitForEvent('request'), // wait prefetch request
page.locator('#prefetch-tap-nested span').click(),
]);
expect(reqUrls).toContainEqual('/prefetch-tap-nested');
});

test('data-astro-prefetch="hover" should prefetch on hover', async ({ page, astro }) => {
await page.goto(astro.resolveUrl('/'));
await expectUrlNotPrefetched('/prefetch-hover', page);
Expand Down Expand Up @@ -195,6 +208,32 @@ test.describe("Prefetch (prefetchAll: true, defaultStrategy: 'tap')", () => {
await expectUrlPrefetched('/prefetch-tap', page);
});

test('data-astro-prefetch="tap" should prefetch on tap when clicking a nested child element', async ({
page,
astro,
}) => {
await page.goto(astro.resolveUrl('/'));
await expectUrlNotPrefetched('/prefetch-tap-nested', page);
await Promise.all([
page.waitForEvent('request'), // wait prefetch request
page.locator('#prefetch-tap-nested span').click(),
]);
await expectUrlPrefetched('/prefetch-tap-nested', page);
});

test('link without data-astro-prefetch should prefetch on tap when clicking a nested child element', async ({
page,
astro,
}) => {
await page.goto(astro.resolveUrl('/'));
await expectUrlNotPrefetched('/prefetch-default-nested', page);
await Promise.all([
page.waitForEvent('request'), // wait prefetch request
page.locator('#prefetch-default-nested span').click(),
]);
await expectUrlPrefetched('/prefetch-default-nested', page);
});

test('data-astro-prefetch="hover" should prefetch on hover', async ({ page, astro }) => {
await page.goto(astro.resolveUrl('/'));
await expectUrlNotPrefetched('/prefetch-hover', page);
Expand Down Expand Up @@ -268,6 +307,19 @@ test.describe("Prefetch (prefetchAll: true, defaultStrategy: 'load')", () => {
await expectUrlPrefetched('/prefetch-tap', page);
});

test('data-astro-prefetch="tap" should prefetch on tap when clicking a nested child element', async ({
page,
astro,
}) => {
await page.goto(astro.resolveUrl('/'));
await expectUrlNotPrefetched('/prefetch-tap-nested', page);
await Promise.all([
page.waitForEvent('request'), // wait prefetch request
page.locator('#prefetch-tap-nested span').click(),
]);
await expectUrlPrefetched('/prefetch-tap-nested', page);
});

test('data-astro-prefetch="hover" should prefetch on hover', async ({ page, astro }) => {
await page.goto(astro.resolveUrl('/'));
await expectUrlNotPrefetched('/prefetch-hover', page);
Expand Down Expand Up @@ -361,6 +413,16 @@ test.describe('Prefetch (default), Experimental ({ clientPrerender: true })', ()
expect(await scriptIsInHead(page, '/prefetch-tap')).toBeTruthy();
});

test('data-astro-prefetch="tap" should prefetch on tap when clicking a nested child element', async ({
page,
astro,
}) => {
await page.goto(astro.resolveUrl('/'));
expect(await scriptIsInHead(page, '/prefetch-tap-nested')).toBeFalsy();
await page.locator('#prefetch-tap-nested span').dragTo(page.locator('#prefetch-hover'));
expect(await scriptIsInHead(page, '/prefetch-tap-nested')).toBeTruthy();
});

test('data-astro-prefetch="hover" should prefetch on hover', async ({ page, astro }) => {
await page.goto(astro.resolveUrl('/'));
expect(await scriptIsInHead(page, '/prefetch-hover')).toBeFalsy();
Expand Down
20 changes: 12 additions & 8 deletions packages/astro/src/prefetch/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ function initTapStrategy() {
document.addEventListener(
event,
(e) => {
if (elMatchesStrategy(e.target, 'tap')) {
prefetch(e.target.href, { ignoreSlowConnection: true });
const anchor = (e.target as Element).closest('a');
if (elMatchesStrategy(anchor, 'tap')) {
prefetch(anchor.href, { ignoreSlowConnection: true });
}
},
{ passive: true },
Expand All @@ -78,8 +79,9 @@ function initHoverStrategy() {
document.body.addEventListener(
'focusin',
(e) => {
if (elMatchesStrategy(e.target, 'hover')) {
handleHoverIn(e);
const anchor = (e.target as Element).closest('a');
if (elMatchesStrategy(anchor, 'hover')) {
handleHoverIn(anchor.href);
}
},
{ passive: true },
Expand All @@ -94,15 +96,17 @@ function initHoverStrategy() {
// Add listeners for anchors matching the strategy
if (elMatchesStrategy(anchor, 'hover')) {
listenedAnchors.add(anchor);
anchor.addEventListener('mouseenter', handleHoverIn, { passive: true });
anchor.addEventListener(
'mouseenter',
(e) => handleHoverIn((e.currentTarget as HTMLAnchorElement).href),
{ passive: true },
);
anchor.addEventListener('mouseleave', handleHoverOut, { passive: true });
}
}
});

function handleHoverIn(e: Event) {
const href = (e.target as HTMLAnchorElement).href;

function handleHoverIn(href: string) {
// Debounce hover prefetches by 80ms
if (timeout) {
clearTimeout(timeout);
Expand Down
Loading