Configuration options for the history patching
The name of the custom event to dispatch on navigation changes
Optionalsignal?: AbortSignalOptional AbortSignal to control the lifecycle of the patching
A dispose function that restores the original History API methods and removes event listeners
// Example 1. Basic usage
const dispose = patchHistory({eventName: 'locationchange'})
// Listen for navigation changes
window.addEventListener('locationchange', (event) => {
console.log(`Navigation: ${event.detail.type} to ${event.detail.url}`);
})
// Clean up when done
dispose()
// Example 2. Usage with AbortSignal
const controller = new AbortController()
patchHistory({eventName: 'locationchange', signal: controller.signal})
window.addEventListener('locationchange', (event) => {
console.log(`Navigation: ${event.detail.type} to ${event.detail.url}`);
})
// Automatically dispose when signal is aborted
controller.abort()
Patches the browser's History API to dispatch custom events when navigation occurs. This function intercepts
pushState,replaceState, andpopstateevents to provide a unified interface for listening to navigation changes.