0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-16 21:46:22 -05:00

[@astrojs/prefetch]: Prevent prefetching current page (#5009)

* Check that removal of url.hash breaks no tests

* test if status-quo is as expected

* Adapt tests to fail

* Adapt the shouldPreload function to skip same path

* Add changeset
This commit is contained in:
Oskar Baumann 2022-10-07 16:13:51 +02:00 committed by GitHub
parent 358ffb541d
commit 92b27e9c92
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 49 additions and 31 deletions

View file

@ -0,0 +1,5 @@
---
'@astrojs/prefetch': patch
---
Prevents prefetching the current page

View file

@ -11,7 +11,7 @@ function shouldPreload({ href }: { href: string }) {
const url = new URL(href); const url = new URL(href);
return ( return (
window.location.origin === url.origin && window.location.origin === url.origin &&
window.location.pathname !== url.hash && window.location.pathname !== url.pathname &&
!preloaded.has(href) !preloaded.has(href)
); );
} catch {} } catch {}

View file

@ -17,23 +17,24 @@ test.describe('Basic prefetch', () => {
test.describe('prefetches rel="prefetch" links', () => { test.describe('prefetches rel="prefetch" links', () => {
test('skips /admin', async ({ page, astro }) => { test('skips /admin', async ({ page, astro }) => {
const requests = new Set(); const requests = [];
page.on('request', async (request) => requests.add(request.url())); page.on('request', async (request) => requests.push(request.url()));
await page.goto(astro.resolveUrl('/')); await page.goto(astro.resolveUrl('/'));
await page.waitForLoadState('networkidle'); await page.waitForLoadState('networkidle');
await expect( expect(requests.includes(astro.resolveUrl('/about')), '/about was prefetched').toBeTruthy();
requests.has(astro.resolveUrl('/about')), expect(
'/about was prefetched' requests.includes(astro.resolveUrl('/contact')),
).toBeTruthy();
await expect(
requests.has(astro.resolveUrl('/contact')),
'/contact was prefetched' '/contact was prefetched'
).toBeTruthy(); ).toBeTruthy();
await expect(requests.has(astro.resolveUrl('/admin')), '/admin was skipped').toBeFalsy(); expect(requests.includes(astro.resolveUrl('/admin')), '/admin was skipped').toBeFalsy();
expect(
requests.filter((r) => r === astro.resolveUrl('/')).length === 1,
'/ was skipped by prefetch and only queried once'
).toBeTruthy();
}); });
}); });
}); });
@ -53,23 +54,24 @@ test.describe('Basic prefetch', () => {
test.describe('prefetches rel="prefetch" links', () => { test.describe('prefetches rel="prefetch" links', () => {
test('skips /admin', async ({ page, astro }) => { test('skips /admin', async ({ page, astro }) => {
const requests = new Set(); const requests = [];
page.on('request', async (request) => requests.add(request.url())); page.on('request', async (request) => requests.push(request.url()));
await page.goto(astro.resolveUrl('/')); await page.goto(astro.resolveUrl('/'));
await page.waitForLoadState('networkidle'); await page.waitForLoadState('networkidle');
await expect( expect(requests.includes(astro.resolveUrl('/about')), '/about was prefetched').toBeTruthy();
requests.has(astro.resolveUrl('/about')), expect(
'/about was prefetched' requests.includes(astro.resolveUrl('/contact')),
).toBeTruthy();
await expect(
requests.has(astro.resolveUrl('/contact')),
'/contact was prefetched' '/contact was prefetched'
).toBeTruthy(); ).toBeTruthy();
await expect(requests.has(astro.resolveUrl('/admin')), '/admin was skipped').toBeFalsy(); expect(requests.includes(astro.resolveUrl('/admin')), '/admin was skipped').toBeFalsy();
expect(
requests.filter((r) => r === astro.resolveUrl('/')).length === 1,
'/ was skipped by prefetch and only queried once'
).toBeTruthy();
}); });
}); });
}); });

View file

@ -25,20 +25,24 @@ test.describe('Custom prefetch selectors', () => {
test.describe('prefetches links by custom selector', () => { test.describe('prefetches links by custom selector', () => {
test('only prefetches /contact', async ({ page, astro }) => { test('only prefetches /contact', async ({ page, astro }) => {
const requests = new Set(); const requests = [];
page.on('request', async (request) => requests.add(request.url())); page.on('request', async (request) => requests.push(request.url()));
await page.goto(astro.resolveUrl('/')); await page.goto(astro.resolveUrl('/'));
await page.waitForLoadState('networkidle'); await page.waitForLoadState('networkidle');
await expect(requests.has(astro.resolveUrl('/about')), '/about was skipped').toBeFalsy(); expect(requests.includes(astro.resolveUrl('/about')), '/about was skipped').toBeFalsy();
await expect( expect(
requests.has(astro.resolveUrl('/contact')), requests.includes(astro.resolveUrl('/contact')),
'/contact was prefetched' '/contact was prefetched'
).toBeTruthy(); ).toBeTruthy();
await expect(requests.has(astro.resolveUrl('/admin')), '/admin was skipped').toBeFalsy(); expect(requests.includes(astro.resolveUrl('/admin')), '/admin was skipped').toBeFalsy();
expect(
requests.filter((r) => r === astro.resolveUrl('/')).length === 1,
'/ was skipped by prefetch and only queried once'
).toBeTruthy();
}); });
}); });
}); });
@ -58,20 +62,24 @@ test.describe('Custom prefetch selectors', () => {
test.describe('prefetches links by custom selector', () => { test.describe('prefetches links by custom selector', () => {
test('only prefetches /contact', async ({ page, astro }) => { test('only prefetches /contact', async ({ page, astro }) => {
const requests = new Set(); const requests = [];
page.on('request', async (request) => requests.add(request.url())); page.on('request', async (request) => requests.push(request.url()));
await page.goto(astro.resolveUrl('/')); await page.goto(astro.resolveUrl('/'));
await page.waitForLoadState('networkidle'); await page.waitForLoadState('networkidle');
await expect(requests.has(astro.resolveUrl('/about')), '/about was skipped').toBeFalsy(); expect(requests.includes(astro.resolveUrl('/about')), '/about was skipped').toBeFalsy();
await expect( expect(
requests.has(astro.resolveUrl('/contact')), requests.includes(astro.resolveUrl('/contact')),
'/contact was prefetched' '/contact was prefetched'
).toBeTruthy(); ).toBeTruthy();
await expect(requests.has(astro.resolveUrl('/admin')), '/admin was skipped').toBeFalsy(); expect(requests.includes(astro.resolveUrl('/admin')), '/admin was skipped').toBeFalsy();
expect(
requests.filter((r) => r === astro.resolveUrl('/')).length === 1,
'/ was skipped by prefetch and only queried once'
).toBeTruthy();
}); });
}); });
}); });

View file

@ -10,6 +10,9 @@
<nav> <nav>
<ul> <ul>
<li>
<a href="/" rel="prefetch">Home</a>
</li>
<li> <li>
<a href="/about" rel="prefetch">About</a> <a href="/about" rel="prefetch">About</a>
</li> </li>