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
2 changes: 1 addition & 1 deletion docs/router/framework/react/api/router/useBlockerHook.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ type ShouldBlockFnArgs = {

- Optional
- Type: `BlockerFn`
- The function that returns a `boolean` or `Promise<boolean>` indicating whether to allow navigation.
- The function that returns a `boolean` or `Promise<boolean>` indicating whether the navigation should be blocked (`true` blocks, `false` allows).

### `options.condition` option (⚠️ deprecated)

Expand Down
11 changes: 9 additions & 2 deletions packages/history/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,12 @@ export type ParsedHistoryState = HistoryState & {
__TSR_index: number
}

type ShouldAllowNavigation = any
export type ShouldBlockNavigation = boolean

/**
* @deprecated Use `ShouldBlockNavigation`. Returning `true` blocks navigation.
*/
export type ShouldAllowNavigation = ShouldBlockNavigation

export type HistoryAction = 'PUSH' | 'REPLACE' | 'FORWARD' | 'BACK' | 'GO'

Expand All @@ -70,7 +75,7 @@ export type BlockerFnArgs = {

export type BlockerFn = (
args: BlockerFnArgs,
) => Promise<ShouldAllowNavigation> | ShouldAllowNavigation
) => Promise<ShouldBlockNavigation> | ShouldBlockNavigation

export type NavigationBlocker = {
blockerFn: BlockerFn
Expand Down Expand Up @@ -149,6 +154,7 @@ export function createHistory(opts: {
action: actionInfo.type,
})
if (isBlocked) {
// Truthy means the blocker wants to keep the navigation from proceeding.
opts.onBlocked?.()
return
}
Expand Down Expand Up @@ -437,6 +443,7 @@ export function createBrowserHistory(opts?: {
action,
})
if (isBlocked) {
// Truthy means the blocker wants to keep the navigation from proceeding.
ignoreNextPop = true
win.history.go(1)
history.notify(notify)
Expand Down
Loading