fix(deep-linking): preserve current pathname/search when setting hash#10903
Open
yogeshwaran-c wants to merge 1 commit into
Open
fix(deep-linking): preserve current pathname/search when setting hash#10903yogeshwaran-c wants to merge 1 commit into
yogeshwaran-c wants to merge 1 commit into
Conversation
`setHash` previously called `history.pushState(null, null, "#value")`. A hash-only URL passed to `pushState` is resolved against the document's base URL (the `<base href>` tag), so when a host page sets `<base href="/">` -- as Angular always does -- the resulting URL becomes `/#value`, wiping out the current route's pathname and search string. Build the third argument from the current `location.pathname` and `location.search` so the resolution is idempotent: the URL is unchanged in pages without a `<base>` tag and remains correct in pages that do declare one. Fixes swagger-api#10591
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
setHashin the deep-linking plugin callshistory.pushState(null, null, "#value"). When the third argument topushStateis a hash-only URL, the browser resolves it against the document's base URL — i.e. the<base href>tag — instead of the current location. Frameworks like Angular always inject<base href=\"/\">, so the resolved URL becomes/#value, which wipes out the page's pathname and search string. The originally requested route is gone.This PR builds the URL passed to
pushStatefromwindow.location.pathnameandwindow.location.search. The behavior is:<base>tag (the new URL is identical to what the browser would have produced on its own).<base href>(the existing path and query are preserved).Motivation and Context
Fixes #10591.
Reported and root-caused by the issue author: an Angular host that mounts Swagger UI with
deepLinking: trueloses its route the moment a user expands an operation, becausepushState(\"#…\")is resolved against<base href=\"/\">.How Has This Been Tested?
test/unit/core/plugins/deep-linking/helpers.jscovers the three branches ofsetHash. Verified the suite fails onmaster(asserts include the full path) and passes with the patch.cross-env NODE_ENV=test BABEL_ENV=commonjs BROWSERSLIST_ENV=node-development jest --config ./config/jest/jest.unit.config.js --no-cache test/unit/core/plugins/deep-linking/helpers.jstest/e2e-cypress/e2e/bugs/10591.cy.jsintercepts the dev-e2eindex.htmlresponse, injects<base href=\"/\">, then asserts that expanding an operation preserves bothlocation.pathnameandlocation.search. Verified the spec fails onmaster(the search string is wiped) and passes with the patch.cross-env BROWSERSLIST_ENV=browser-production cypress run --spec test/e2e-cypress/e2e/bugs/10591.cy.jsChecklist
Notes:
setHashis an internal helper and the public deep-linking contract is unchanged for hosts without a<base>tag.