Skip to content

fix(history): add ignoreBlocker support to go method (#4867) #4872

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
7 changes: 4 additions & 3 deletions packages/history/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export function createHistory(opts: {
getLength: () => number
pushState: (path: string, state: any) => void
replaceState: (path: string, state: any) => void
go: (n: number) => void
go: (n: number, ignoreBlocker: boolean) => void
back: (ignoreBlocker: boolean) => void
forward: (ignoreBlocker: boolean) => void
createHref: (path: string) => string
Expand Down Expand Up @@ -204,7 +204,7 @@ export function createHistory(opts: {
go: (index, navigateOpts) => {
tryNavigation({
task: () => {
opts.go(index)
opts.go(index, navigateOpts?.ignoreBlocker ?? false)
handleIndexChange({ type: 'GO', index })
},
navigateOpts,
Expand Down Expand Up @@ -500,7 +500,8 @@ export function createBrowserHistory(opts?: {
ignoreNextBeforeUnload = true
win.history.forward()
},
go: (n) => {
go: (n, ignoreBlocker) => {
if (ignoreBlocker) skipBlockerNextPop = true
nextPopIsGo = true
win.history.go(n)
},
Expand Down
97 changes: 97 additions & 0 deletions packages/history/tests/ignoreBlocker.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import { describe, expect, test, vi } from 'vitest'
import { createBrowserHistory } from '../src'

describe('ignoreBlocker functionality', () => {
test('go method should respect ignoreBlocker option', () => {
const mockWindow = {
history: {
pushState: vi.fn(),
replaceState: vi.fn(),
go: vi.fn(),
back: vi.fn(),
forward: vi.fn(),
length: 3,
state: { __TSR_index: 2 },
},
location: {
pathname: '/a',
search: '',
hash: '',
},
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
}

const history = createBrowserHistory({ window: mockWindow })
const blocker = vi.fn(() => true)

const unblock = history.block({ blockerFn: blocker })

history.go(-2, { ignoreBlocker: true })
expect(mockWindow.history.go).toHaveBeenCalledWith(-2)

unblock()
})

test('back method should respect ignoreBlocker option', () => {
const mockWindow = {
history: {
pushState: vi.fn(),
replaceState: vi.fn(),
go: vi.fn(),
back: vi.fn(),
forward: vi.fn(),
length: 3,
state: { __TSR_index: 2 },
},
location: {
pathname: '/a',
search: '',
hash: '',
},
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
}

const history = createBrowserHistory({ window: mockWindow })
const blocker = vi.fn(() => true)

const unblock = history.block({ blockerFn: blocker })

history.back({ ignoreBlocker: true })
expect(mockWindow.history.back).toHaveBeenCalled()

unblock()
})

test('forward method should respect ignoreBlocker option', () => {
const mockWindow = {
history: {
pushState: vi.fn(),
replaceState: vi.fn(),
go: vi.fn(),
back: vi.fn(),
forward: vi.fn(),
length: 3,
state: { __TSR_index: 2 },
},
location: {
pathname: '/a',
search: '',
hash: '',
},
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
}

const history = createBrowserHistory({ window: mockWindow })
const blocker = vi.fn(() => true)

const unblock = history.block({ blockerFn: blocker })

history.forward({ ignoreBlocker: true })
expect(mockWindow.history.forward).toHaveBeenCalled()

unblock()
})
})