Skip to content

Comments

fix(docs-infra): preserve text fragment highlights during initial navigation#67235

Closed
yogeshwaran-c wants to merge 1 commit intoangular:mainfrom
yogeshwaran-c:fix/text-fragment-highlights-v2
Closed

fix(docs-infra): preserve text fragment highlights during initial navigation#67235
yogeshwaran-c wants to merge 1 commit intoangular:mainfrom
yogeshwaran-c:fix/text-fragment-highlights-v2

Conversation

@yogeshwaran-c
Copy link

What kind of change does this PR introduce?

Bug fix (reworked from closed PR #67216)

What is the current behavior?

When using the browser's "Copy link to highlight" feature on angular.dev, the text highlight briefly appears but disappears as the page finishes loading. The :~:text=... fragment is also removed from the URL. This happens because Angular's Router and the navigation adapter both call history.replaceState during the initial navigation, which causes Chromium to clear text fragment highlights.

Closes #65119

What is the new behavior?

Text fragment highlights are preserved after the initial page load. The :~:text=... fragment remains in the URL and the browser's text highlighting feature works correctly on angular.dev.

How this differs from the previous approach (PR #67216)

The previous PR added a preserveTextFragmentHighlight initializer that suppressed history.replaceState/pushState with no-ops, but it was registered AFTER initializeNavigationAdapter. This caused a conflict because the navigation adapter calls replaceState during NavigationStart to trigger a Navigation API intercept (for browser loading indicators), and the suppression broke this adapter entirely.

This reworked fix resolves the conflict by:

  1. URL-based detection instead of feature detection: Checks window.location.href.includes(':~:') to detect an active text fragment, rather than checking fragmentDirective in document. This is more precise -- it only activates when there is actually a text fragment in the current URL.

  2. Correct initialization order: The preserveTextFragmentHighlight initializer is registered BEFORE initializeNavigationAdapter, so replaceState/pushState are already suppressed when the adapter subscribes to router events and tries to call replaceState on NavigationStart.

  3. Acceptable trade-off: The navigation adapter's replaceState call being suppressed during the initial navigation means the Navigation API intercept won't trigger for the first navigation. This is acceptable because the browser already shows a loading indicator during the initial page load.

Additional context

The fix includes unit tests that verify:

  • History methods are suppressed when the URL contains a text fragment directive
  • History methods are restored after the initial navigation completes
  • History methods are not touched when there is no text fragment in the URL

…igation

Chromium-based browsers clear text fragment highlights whenever
history.pushState or history.replaceState is called. During the initial
navigation, Angular's Router and the navigation adapter both call
replaceState, which strips the text highlight and removes the :~:text=
fragment from the URL.

The previous approach (PR angular#67216) suppressed replaceState/pushState
entirely via a separate environment initializer, but this conflicted
with the navigation adapter which also calls replaceState during
NavigationStart. The adapter's replaceState call triggers a Navigation
API intercept for browser loading indicators.

This fix resolves the conflict by:
1. Checking whether the current URL contains a text fragment directive
   (:~:) instead of checking the fragmentDirective browser feature
2. Registering the suppression initializer BEFORE the navigation
   adapter, so replaceState/pushState are already no-ops when the
   adapter fires on NavigationStart
3. Restoring the originals after the first navigation completes

The navigation adapter's replaceState call being suppressed during the
initial navigation is acceptable because the browser already shows a
loading indicator for the initial page load.

Fixes angular#65119
@pullapprove pullapprove bot requested a review from atscott February 24, 2026 02:10
@angular-robot angular-robot bot added the area: docs-infra Angular.dev application and infrastructure label Feb 24, 2026
@ngbot ngbot bot added this to the Backlog milestone Feb 24, 2026
@JeanMeche
Copy link
Member

This obviously has not been tested in the adev app because it still doesn't work.

Please continue if you'd like to get banned.

@JeanMeche JeanMeche closed this Feb 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: docs-infra Angular.dev application and infrastructure

Projects

None yet

Development

Successfully merging this pull request may close these issues.

“Copy link to highlight” not working — text highlight and URL fragment removed after page load on Angular Docs

2 participants