diff options
author | 2023-11-28 04:13:18 -0500 | |
---|---|---|
committer | 2023-11-28 17:13:18 +0800 | |
commit | d90714fc3dd7c3eab0a6b29319b0b666bb04b678 (patch) | |
tree | d7ba5ea444502a3e5678ff2b95725aead1e8ca3b | |
parent | e7ce779ff89880b9dee7b917fb0f6ebe88268917 (diff) | |
download | astro-d90714fc3dd7c3eab0a6b29319b0b666bb04b678.tar.gz astro-d90714fc3dd7c3eab0a6b29319b0b666bb04b678.tar.zst astro-d90714fc3dd7c3eab0a6b29319b0b666bb04b678.zip |
fix: links with same path but different search params not prefetched (#9189)
Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
Diffstat (limited to '')
-rw-r--r-- | .changeset/brown-jars-lick.md | 5 | ||||
-rw-r--r-- | packages/astro/e2e/fixtures/prefetch/src/pages/index.astro | 2 | ||||
-rw-r--r-- | packages/astro/e2e/prefetch.test.js | 26 | ||||
-rw-r--r-- | packages/astro/src/prefetch/index.ts | 2 |
4 files changed, 32 insertions, 3 deletions
diff --git a/.changeset/brown-jars-lick.md b/.changeset/brown-jars-lick.md new file mode 100644 index 000000000..0d824e445 --- /dev/null +++ b/.changeset/brown-jars-lick.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fixes an issue where links with the same pathname as the current page, but different search params, were not prefetched. diff --git a/packages/astro/e2e/fixtures/prefetch/src/pages/index.astro b/packages/astro/e2e/fixtures/prefetch/src/pages/index.astro index e61bc1c6c..88ce196ae 100644 --- a/packages/astro/e2e/fixtures/prefetch/src/pages/index.astro +++ b/packages/astro/e2e/fixtures/prefetch/src/pages/index.astro @@ -9,6 +9,8 @@ <br> <a id="prefetch-false" href="/prefetch-false" data-astro-prefetch="false">false</a> <br> + <a id="prefetch-search-param" href="?search-param=true" data-astro-prefetch="hover">search param</a> + <br> <a id="prefetch-tap" href="/prefetch-tap" data-astro-prefetch="tap">tap</a> <br> <a id="prefetch-hover" href="/prefetch-hover" data-astro-prefetch="hover">hover</a> diff --git a/packages/astro/e2e/prefetch.test.js b/packages/astro/e2e/prefetch.test.js index dc29bde33..a19c87680 100644 --- a/packages/astro/e2e/prefetch.test.js +++ b/packages/astro/e2e/prefetch.test.js @@ -16,7 +16,8 @@ test.describe('Prefetch (default)', () => { test.beforeEach(async ({ page }) => { page.on('request', (req) => { - reqUrls.push(new URL(req.url()).pathname); + const urlObj = new URL(req.url()); + reqUrls.push(urlObj.pathname + urlObj.search); }); }); @@ -38,6 +39,16 @@ test.describe('Prefetch (default)', () => { expect(reqUrls).not.toContainEqual('/prefetch-false'); }); + test('Link with search param should prefetch', async ({ page, astro }) => { + await page.goto(astro.resolveUrl('/')); + expect(reqUrls).not.toContainEqual('/?search-param=true'); + await Promise.all([ + page.waitForEvent('request'), // wait prefetch request + page.locator('#prefetch-search-param').hover(), + ]); + expect(reqUrls).toContainEqual('/?search-param=true'); + }); + test('data-astro-prefetch="tap" should prefetch on tap', async ({ page, astro }) => { await page.goto(astro.resolveUrl('/')); expect(reqUrls).not.toContainEqual('/prefetch-tap'); @@ -102,7 +113,8 @@ test.describe("Prefetch (prefetchAll: true, defaultStrategy: 'tap')", () => { test.beforeEach(async ({ page }) => { page.on('request', (req) => { - reqUrls.push(new URL(req.url()).pathname); + const urlObj = new URL(req.url()); + reqUrls.push(urlObj.pathname + urlObj.search); }); }); @@ -129,6 +141,16 @@ test.describe("Prefetch (prefetchAll: true, defaultStrategy: 'tap')", () => { expect(reqUrls).not.toContainEqual('/prefetch-false'); }); + test('Link with search param should prefetch', async ({ page, astro }) => { + await page.goto(astro.resolveUrl('/')); + expect(reqUrls).not.toContainEqual('/?search-param=true'); + await Promise.all([ + page.waitForEvent('request'), // wait prefetch request + page.locator('#prefetch-search-param').hover(), + ]); + expect(reqUrls).toContainEqual('/?search-param=true'); + }); + test('data-astro-prefetch="tap" should prefetch on tap', async ({ page, astro }) => { await page.goto(astro.resolveUrl('/')); expect(reqUrls).not.toContainEqual('/prefetch-tap'); diff --git a/packages/astro/src/prefetch/index.ts b/packages/astro/src/prefetch/index.ts index 573efe573..15f4ef0cc 100644 --- a/packages/astro/src/prefetch/index.ts +++ b/packages/astro/src/prefetch/index.ts @@ -226,7 +226,7 @@ function canPrefetchUrl(url: string, ignoreSlowConnection: boolean) { const urlObj = new URL(url, location.href); return ( location.origin === urlObj.origin && - location.pathname !== urlObj.pathname && + (location.pathname !== urlObj.pathname || location.search !== urlObj.search) && !prefetchedUrls.has(url) ); } catch {} |