History API: Modify history entries

While pushState() lets you add a new state to the history, replaceState() allows you to edit the current history state.

history.pushState({}, '', '/posts')
const state = { post: 'first' }
history.pushState(state, '', '/post/first')
const state = { post: 'second' }
history.replaceState(state, '', '/post/second')

If you now call

history.back()

the browser goes straight to /posts, since /post/first was replaced by /post/second

Lessons in this unit:

0: Introduction
1: Navigating the history
2: Add an entry to the history
3: ▶︎ Modify history entries
4: Access the current history entry state
5: The `popstate` event
6: The `hashchange` event