Skip to content

fix: [#1847] Navigation with location.href now adds to history#2074

Open
TrevorBurnham wants to merge 1 commit intocapricorn86:masterfrom
TrevorBurnham:fix/1847-location-href-history
Open

fix: [#1847] Navigation with location.href now adds to history#2074
TrevorBurnham wants to merge 1 commit intocapricorn86:masterfrom
TrevorBurnham:fix/1847-location-href-history

Conversation

@TrevorBurnham
Copy link
Copy Markdown
Contributor

Closes #1847

Problem

Setting location.href doesn't increment history.length. The href setter calls browserFrame.goto()BrowserFrameNavigator.navigate(), which pushes to history for full navigations. However, when using a detached window (e.g. new Window(), the common test scenario), validateFrameNavigation() returns false and the code falls through to a path that only updates the URL via setURL() without pushing a history entry.

Changes

packages/happy-dom/src/browser/utilities/BrowserFrameNavigator.ts

  • In the validateFrameNavigation fallback path (used by detached windows), push a history entry before calling setURL(), matching the behavior of the normal navigation path.

packages/happy-dom/test/history/History.test.ts

  • Added test verifying history.length increases after setting location.href on a detached window.

Copy link
Copy Markdown
Owner

@capricorn86 capricorn86 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

History should not be changed when inside a window of a DetachedBrowser as there is no navigation happening. The user stays on the same page. For it to work with unit tests, it can fallback to setting the current URL without navigating.

@TrevorBurnham
Copy link
Copy Markdown
Contributor Author

History should not be changed when inside a window of a DetachedBrowser as there is no navigation happening.

But it's already the case that history.pushState() (for example) works in DetachedBrowser. Why would we simulate realistic behavior for that method but not for the href setter?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Navigation history not added to when navigating with location.href

2 participants