AI Workshop: learn to build apps with AI →
History API: Modify history entries

Join the AI Workshop and learn to build real-world apps with AI. A hands-on, practical program to level up your skills.


While pushState() adds a new entry to the history, replaceState() replaces the current history entry without adding one.

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

If you then call:

history.back()

the browser navigates to /posts, because /post/first was replaced by /post/second and no longer exists as a separate entry..

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
7: Bfcache and page restoration