Skip to content
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 @@ -501,7 +501,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)
},
Comment on lines +504 to 508

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

Use curly braces for if statements.

The if statement has a one-line body without curly braces. As per coding guidelines, always use curly braces for if, else, loops, and similar control statements.

♻️ Proposed fix
-    go: (n, ignoreBlocker) => {
-      if (ignoreBlocker) skipBlockerNextPop = true
-      nextPopIsGo = true
-      win.history.go(n)
-    },
+    go: (n, ignoreBlocker) => {
+      if (ignoreBlocker) {
+        skipBlockerNextPop = true
+      }
+      nextPopIsGo = true
+      win.history.go(n)
+    },
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
go: (n, ignoreBlocker) => {
if (ignoreBlocker) skipBlockerNextPop = true
nextPopIsGo = true
win.history.go(n)
},
go: (n, ignoreBlocker) => {
if (ignoreBlocker) {
skipBlockerNextPop = true
}
nextPopIsGo = true
win.history.go(n)
},
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/history/src/index.ts` around lines 504 - 508, Update the if
statement in the go callback to wrap the skipBlockerNextPop assignment in curly
braces, preserving the existing behavior and surrounding history navigation
logic.

Source: Coding guidelines

Expand Down
48 changes: 48 additions & 0 deletions packages/history/tests/createBrowserHistory.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { afterEach, describe, expect, test, vi } from 'vitest'

import { createBrowserHistory } from '../src'
import type { RouterHistory } from '../src'

describe('createBrowserHistory', () => {
let history: RouterHistory | undefined

afterEach(() => {
history?.destroy()
history = undefined
})

// Establishes two entries in the browser session history (`/one` then `/two`)
// and returns a browser history positioned on the second entry, so that a
// subsequent `go(-1)` produces a real `popstate` that reaches the blocker
// handling in `onPushPopEvent`.
function setupTwoEntries(): RouterHistory {
window.history.replaceState({ __TSR_index: 0, __TSR_key: 'one' }, '', '/one')
window.history.pushState({ __TSR_index: 1, __TSR_key: 'two' }, '', '/two')
history = createBrowserHistory()
return history
}

describe('go respects the ignoreBlocker option', () => {
test('go(-1) runs registered blockers by default', async () => {
const history = setupTwoEntries()
const blockerFn = vi.fn(async () => true)
history.block({ blockerFn })

history.go(-1)
await new Promise((resolve) => setTimeout(resolve, 100))

expect(blockerFn).toHaveBeenCalledTimes(1)
})

test('go(-1, { ignoreBlocker: true }) skips registered blockers', async () => {
const history = setupTwoEntries()
const blockerFn = vi.fn(async () => true)
history.block({ blockerFn })

history.go(-1, { ignoreBlocker: true })
await new Promise((resolve) => setTimeout(resolve, 100))

expect(blockerFn).not.toHaveBeenCalled()
})
})
})