Skip to content

Do not run window.history.replaceState unconditionally#13918

Merged
AA-Turner merged 3 commits intosphinx-doc:masterfrom
haampie:patch-2
Nov 24, 2025
Merged

Do not run window.history.replaceState unconditionally#13918
AA-Turner merged 3 commits intosphinx-doc:masterfrom
haampie:patch-2

Conversation

@haampie
Copy link
Copy Markdown
Contributor

@haampie haampie commented Sep 27, 2025

Closes #13916

Currently every Sphinx documentation page always drops #:~:text=... Text Fragments due to an unconditional window.history.replaceState() call. That replaceState() is almost always redundant, since it happens when handling ?highlight=... search term highlighting, which is rarely present in the URL.

It's undesirable, because it prevents users from sharing the URL verbatim including the text fragment from the browser's URL bar. Text fragments are increasingly popular these days as they're used by Google Search and various LLMs to scroll to and highlight specific content on a longer page.

The solution is to only call window.history.replaceState when a Sphinx's custom ?highlight=... search param is part of the URL.

@jayaddison
Copy link
Copy Markdown
Contributor

For this pull request and/or #13921, please add an entry in the CHANGES.rst file -- perhaps at the end of either the bugfix list or features list, as appropriate, with a description of the modifications and a credit entry.

Currently every Sphinx documentation page *always* drops `#:~:text=...` Text Fragments unconditionally due to `window.history.replaceState`.

The solution is to only call `window.history.replaceState` when a `?highlight=...` param is part of the URL.
@haampie
Copy link
Copy Markdown
Contributor Author

haampie commented Nov 17, 2025

@jayaddison, I've added it to CHANGES.rst.

Since #13921 seems to be blocked, I'd suggest to go with this minimal bug fix.

Is the CI failure expected? Looks unrelated.

Copy link
Copy Markdown
Contributor

@jayaddison jayaddison left a comment

Choose a reason for hiding this comment

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

@haampie thanks for being patient - this looks good to me, and I have tested it locally to confirm the before-and-after behaviours.

I agree that the 1-out-of-30 GitHub Actions check failure (unit tests) seems to be an unrelated issue - we'd welcome a bug report (similar to #12159 for example) if you have time -- I'll try to get around to reporting that within the next week or so otherwise.

@haampie haampie mentioned this pull request Nov 17, 2025
@haampie
Copy link
Copy Markdown
Contributor Author

haampie commented Nov 17, 2025

Looks like an issue was already opened a couple weeks ago: #13996

@haampie haampie closed this Nov 18, 2025
@haampie haampie reopened this Nov 18, 2025
@haampie
Copy link
Copy Markdown
Contributor Author

haampie commented Nov 18, 2025

@jayaddison closed and opened the PR to restart github actions, which are now green. So the PR should be good to go.

@jayaddison
Copy link
Copy Markdown
Contributor

Thanks @haampie - I'm a contributor here, but I am not a core developer/maintainer, so I don't have permissions to merge. We'll need to wait/find one of the core team to help with that (they do check recently opened PRs from time to time; I can't guarantee how long it may take, though).

@haampie
Copy link
Copy Markdown
Contributor Author

haampie commented Nov 20, 2025

Maybe a friendly ping to @mgeier helps? For context, this PR should not be controversial, in contrast to #13921.

It fixes a bug where the browser clears parts #... bit of the URL on page load, even if there is no ?highlight=... param. Now it only does that when there is a highlight param.

@mgeier
Copy link
Copy Markdown
Contributor

mgeier commented Nov 21, 2025

I'm not a core dev either. I approve, but someone else will have to merge it.

@haampie
Copy link
Copy Markdown
Contributor Author

haampie commented Nov 22, 2025

In that case a friendly ping for @AA-Turner for a potential merge, since this PR has fallen of the first PRs page.

Copy link
Copy Markdown
Member

@AA-Turner AA-Turner left a comment

Choose a reason for hiding this comment

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

Please can you add a comment explaining why we don't unconditionally run the code. If possible, adding a test would also be great.

A

@haampie
Copy link
Copy Markdown
Contributor Author

haampie commented Nov 22, 2025

I've added a comment, which I think is enough. There are no existing tests covering that part.

Comment thread sphinx/themes/basic/static/sphinx_highlight.js Outdated
@AA-Turner
Copy link
Copy Markdown
Member

AA-Turner commented Nov 24, 2025

Thanks!

A

@AA-Turner AA-Turner merged commit 401d81f into sphinx-doc:master Nov 24, 2025
30 of 31 checks passed
@AA-Turner AA-Turner added this to the 9.0.0 milestone Nov 25, 2025
@haampie haampie deleted the patch-2 branch November 25, 2025 13:19
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Dec 24, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sphinx_highlight.js removes text fragment from URL

4 participants