Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't prefetch the current page #2097

Merged
merged 2 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/core/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,14 @@ export class Router {
prefetch: true,
})

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
}

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
Loading