Skip to content

Commit

Permalink
no need to prefetch the current page, we're already on it
Browse files Browse the repository at this point in the history
  • Loading branch information
joetannenbaum committed Nov 20, 2024
1 parent 9106d74 commit 6752ba4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/core/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions tests/prefetch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit 6752ba4

Please sign in to comment.