Skip to content

Commit

Permalink
Merge pull request #2097 from inertiajs/same-page-prefetch
Browse files Browse the repository at this point in the history
Don't prefetch the current page
  • Loading branch information
joetannenbaum authored Dec 3, 2024
2 parents 3678c7d + df094aa commit ed0a6a2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
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

0 comments on commit ed0a6a2

Please sign in to comment.