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:
parent
358ffb541d
commit
92b27e9c92
5 changed files with 49 additions and 31 deletions
5
.changeset/cold-colts-laugh.md
Normal file
5
.changeset/cold-colts-laugh.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'@astrojs/prefetch': patch
|
||||
---
|
||||
|
||||
Prevents prefetching the current page
|
|
@ -11,7 +11,7 @@ function shouldPreload({ href }: { href: string }) {
|
|||
const url = new URL(href);
|
||||
return (
|
||||
window.location.origin === url.origin &&
|
||||
window.location.pathname !== url.hash &&
|
||||
window.location.pathname !== url.pathname &&
|
||||
!preloaded.has(href)
|
||||
);
|
||||
} catch {}
|
||||
|
|
|
@ -17,23 +17,24 @@ test.describe('Basic prefetch', () => {
|
|||
|
||||
test.describe('prefetches rel="prefetch" links', () => {
|
||||
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.waitForLoadState('networkidle');
|
||||
|
||||
await expect(
|
||||
requests.has(astro.resolveUrl('/about')),
|
||||
'/about was prefetched'
|
||||
).toBeTruthy();
|
||||
await expect(
|
||||
requests.has(astro.resolveUrl('/contact')),
|
||||
expect(requests.includes(astro.resolveUrl('/about')), '/about was prefetched').toBeTruthy();
|
||||
expect(
|
||||
requests.includes(astro.resolveUrl('/contact')),
|
||||
'/contact was prefetched'
|
||||
).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('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.waitForLoadState('networkidle');
|
||||
|
||||
await expect(
|
||||
requests.has(astro.resolveUrl('/about')),
|
||||
'/about was prefetched'
|
||||
).toBeTruthy();
|
||||
await expect(
|
||||
requests.has(astro.resolveUrl('/contact')),
|
||||
expect(requests.includes(astro.resolveUrl('/about')), '/about was prefetched').toBeTruthy();
|
||||
expect(
|
||||
requests.includes(astro.resolveUrl('/contact')),
|
||||
'/contact was prefetched'
|
||||
).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();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
@ -25,20 +25,24 @@ test.describe('Custom prefetch selectors', () => {
|
|||
|
||||
test.describe('prefetches links by custom selector', () => {
|
||||
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.waitForLoadState('networkidle');
|
||||
|
||||
await expect(requests.has(astro.resolveUrl('/about')), '/about was skipped').toBeFalsy();
|
||||
await expect(
|
||||
requests.has(astro.resolveUrl('/contact')),
|
||||
expect(requests.includes(astro.resolveUrl('/about')), '/about was skipped').toBeFalsy();
|
||||
expect(
|
||||
requests.includes(astro.resolveUrl('/contact')),
|
||||
'/contact was prefetched'
|
||||
).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('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.waitForLoadState('networkidle');
|
||||
|
||||
await expect(requests.has(astro.resolveUrl('/about')), '/about was skipped').toBeFalsy();
|
||||
await expect(
|
||||
requests.has(astro.resolveUrl('/contact')),
|
||||
expect(requests.includes(astro.resolveUrl('/about')), '/about was skipped').toBeFalsy();
|
||||
expect(
|
||||
requests.includes(astro.resolveUrl('/contact')),
|
||||
'/contact was prefetched'
|
||||
).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();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
@ -10,6 +10,9 @@
|
|||
|
||||
<nav>
|
||||
<ul>
|
||||
<li>
|
||||
<a href="/" rel="prefetch">Home</a>
|
||||
</li>
|
||||
<li>
|
||||
<a href="/about" rel="prefetch">About</a>
|
||||
</li>
|
||||
|
|
Loading…
Reference in a new issue