From 6752ba4bbf85214ca51540edbba6f74b6180dc65 Mon Sep 17 00:00:00 2001 From: Joe Tannenbaum Date: Wed, 20 Nov 2024 14:15:18 -0500 Subject: [PATCH 1/2] no need to prefetch the current page, we're already on it --- packages/core/src/router.ts | 5 +++++ tests/prefetch.spec.ts | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/packages/core/src/router.ts b/packages/core/src/router.ts index 991a32820..0d50977f8 100644 --- a/packages/core/src/router.ts +++ b/packages/core/src/router.ts @@ -198,6 +198,11 @@ export class Router { prefetch: true, }) + if (visit.url.href === window.location.href) { + // Don't prefetch the current page, you're already on it + return + } + const events = this.getVisitEvents(options) // If either of these return false, we don't want to continue diff --git a/tests/prefetch.spec.ts b/tests/prefetch.spec.ts index 88e50b2d6..cff16d9bc 100644 --- a/tests/prefetch.spec.ts +++ b/tests/prefetch.spec.ts @@ -18,6 +18,24 @@ const hoverAndClick = async (page: Page, buttonText: string, id: number) => { await isPrefetchSwrPage(page, id) } +test('will not prefetch current page', async ({ page }) => { + // These two prefetch requests should be made on mount + const prefetch2 = page.waitForResponse('prefetch/2') + const prefetch4 = page.waitForResponse('prefetch/4') + + await page.goto('prefetch/1') + + // These two prefetch requests should be made on mount + await prefetch2 + await prefetch4 + + requests.listen(page) + await page.getByRole('link', { name: 'On Hover (Default)' }).hover() + await page.waitForTimeout(100) + // This is the page we're already on, so it shouldn't make a request + await expect(requests.requests.length).toBe(0) +}) + test('can prefetch using link props', async ({ page }) => { // These two prefetch requests should be made on mount const prefetch2 = page.waitForResponse('prefetch/2') From df094aada9eb9455bb000c3d62047f82978c2fd8 Mon Sep 17 00:00:00 2001 From: Joe Tannenbaum Date: Tue, 3 Dec 2024 14:23:50 -0500 Subject: [PATCH 2/2] better current url comparison --- packages/core/src/router.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/core/src/router.ts b/packages/core/src/router.ts index 0d50977f8..856273179 100644 --- a/packages/core/src/router.ts +++ b/packages/core/src/router.ts @@ -198,7 +198,10 @@ export class Router { prefetch: true, }) - if (visit.url.href === window.location.href) { + const visitUrl = visit.url.origin + visit.url.pathname + visit.url.search + const currentUrl = window.location.origin + window.location.pathname + window.location.search + + if (visitUrl === currentUrl) { // Don't prefetch the current page, you're already on it return }