Skip to main content

history

Wrappers around the History API for pushing and replacing entries, navigating back/forward, and listening for popstate.

Import: @rtorcato/browser-common/history

๐Ÿ“– MDN: History API ยท ๐Ÿ“Š caniuse: history

Browser supportโ€‹

APIChromeEdgeFirefoxSafariiOS SafariBaseline
History.pushStateโœ… 5+โœ… 12+โœ… 4+ [1]โœ… 5+โœ… 4+Widely available
History.replaceStateโœ… 5+โœ… 12+โœ… 4+ [1]โœ… 5+โœ… 4+Widely available
  1. Firefox: Until Firefox 5, the passed object is serialized using JSON. Starting in Firefox 6, the object is serialized using the structured clone algorithm. This allows a wider variety of objects to be safely passed.

โœ… supported ยท โ— partial ยท โŒ not supported ยท [n] see note. Generated from MDN browser-compat-data and web-features. caniuse: History.pushState ยท History.replaceState

Exampleโ€‹

import { pushState, onPopState, goBack } from '@rtorcato/browser-common/history'

pushState('/page/2', { page: 2 })

const off = onPopState((event) => {
console.log('navigated to', event.state)
})

goBack()
off()

Exportsโ€‹

  • pushState(url, state?, title?) โ€” pushes a new history entry
  • replaceState(url, state?, title?) โ€” replaces the current history entry
  • goBack() โ€” navigates back one entry
  • goForward() โ€” navigates forward one entry
  • go(delta) โ€” navigates to a relative position in the history stack
  • onPopState(callback) โ€” listens for popstate; returns an unsubscribe function

See the API reference for full signatures.