Description#
Implement a session history stack that tracks visited pages and allows back/forward traversal. This is the internal data structure that powers both the History API and the browser chrome back/forward buttons.
Requirements#
Session History#
- Maintain an ordered list of session history entries
- Each entry stores: URL, document state (or ability to reconstruct), scroll position (x, y), serialized state data (for pushState), document title
- Track the current entry index (position in the list)
- Navigating to a new page pushes a new entry after the current position (discarding any forward entries)
- Maximum history size limit (e.g., 50 entries) to prevent unbounded memory growth
Traversal#
traverse_back()— move to previous entry, reload document if neededtraverse_forward()— move to next entry, reload document if neededtraverse_to(delta)— move by delta steps (negative = back, positive = forward)- Same-document navigations (pushState, fragment changes) don't require full page reload on traversal
Integration Points#
- Link click navigation should push a new history entry
location.replace()should replace (not push) the current entry- Form submission should push a new history entry
- Fragment navigation should push a new history entry
Acceptance Criteria#
- Navigating to a new page creates a new history entry
- Back traversal returns to the previous page
- Forward traversal returns to the page you came back from
- Navigating from the middle of the stack discards forward entries
- Scroll position is saved and restored on traversal
- History size is bounded
- Unit tests for stack operations (push, traverse back/forward, truncation)