Skip to content

fix(deep-linking): preserve current pathname/search when setting hash#10903

Open
yogeshwaran-c wants to merge 1 commit into
swagger-api:masterfrom
yogeshwaran-c:fix/deep-linking-base-href
Open

fix(deep-linking): preserve current pathname/search when setting hash#10903
yogeshwaran-c wants to merge 1 commit into
swagger-api:masterfrom
yogeshwaran-c:fix/deep-linking-base-href

Conversation

@yogeshwaran-c
Copy link
Copy Markdown

Description

setHash in the deep-linking plugin calls history.pushState(null, null, "#value"). When the third argument to pushState is 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 pushState from window.location.pathname and window.location.search. The behavior is:

  • Unchanged in pages with no <base> tag (the new URL is identical to what the browser would have produced on its own).
  • Correct in pages that declare a <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: true loses its route the moment a user expands an operation, because pushState(\"#…\") is resolved against <base href=\"/\">.

How Has This Been Tested?

  • New Jest unit suite at test/unit/core/plugins/deep-linking/helpers.js covers the three branches of setHash. Verified the suite fails on master (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.js
  • New Cypress regression test/e2e-cypress/e2e/bugs/10591.cy.js intercepts the dev-e2e index.html response, injects <base href=\"/\">, then asserts that expanding an operation preserves both location.pathname and location.search. Verified the spec fails on master (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.js
  • Targeted ESLint pass on the three touched files (no warnings).

Checklist

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

Notes:

  • No documentation change is needed — setHash is an internal helper and the public deep-linking contract is unchanged for hosts without a <base> tag.

`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
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.

Deeplinking replaces entire route of the page.

1 participant