Skip to content

Conversation

sroucheray
Copy link
Contributor

Replacing history state did not work when RapiDoc component is delivered in a deep URL web page (i.e. http://www.mysite.com/deep/link/rapidoc).

Rapidoc calls on history.replaceState did not always use the full URL (was not DRY). So in some cases we ended up with URLs like http://www.mysite.com#overview instead of http://www.mysite.com/deep/link/rapidoc#overview.

@sroucheray sroucheray force-pushed the fix/replace-history-state branch from d034fe3 to 25ca776 Compare November 30, 2022 16:33
@@ -791,6 +791,10 @@ export default class RapiDoc extends LitElement {
}
}

replaceHistoryState(hashId) {
Copy link
Collaborator

@mrin9 mrin9 Dec 1, 2022

Choose a reason for hiding this comment

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

The idea is to support 2 styles of routes

  • Routes with hash #
  • Routes without hash

Frameworks like react, vue angular routing do not use hash in there routes
therefore routePrefix is used

at the end the route is constructed like
{current-location} + {routePrefix} + {elementId}
where

  • current-location is location of the index.html in case of plain html or it will be location.origin in case of vue/react/angular etc
  • routePrefix will be # in case of html or will be a non-blank value in case of vue/react/angular etc
  • elementId is the id of the element
  • Note that presence of routePrefix determines are we generating hash based routes or non-hash routes

So

  • in case of plain html the final route can be
    • http://example.com/index.html#element-id-1
      • current-location = http://example.com/index.html
      • routePrefix = '' (coz routePrefix =='' we use # instead)
      • elementId = element-id-1
      • the final route looks like http://example.com/index.html#element-id-1
  • in case of a framework, where rapidoc is loaded on a page in (vue/react etc) the route may look like
    • http://example.com/api-doc/element-id-1
      • current-location = http://example.com (location.origin)
      • routePrefix = 'api-doc'
      • elementId = element-id-1
      • the final route looks like http://example.com/api-doc/element-id-1

keeping this as the goal, this code needs some change as it assumes there will always be a hash in the route

in case of frameworks the replaceState should change

  • from http://example.com/api-doc/element-id-1
  • to http://example.com/api-doc/element-id-2

but in this case it will change to
http://example.com/api-doc/element-id-1 + api-doc + element-id-2

@mrin9
Copy link
Collaborator

mrin9 commented Dec 1, 2022

in the deep-link example you provided above for the reason of this PR
can you try setting routePrefix = '/deep/link/rapidoc#' and check if it takes care of your situation ?

@sroucheray sroucheray force-pushed the fix/replace-history-state branch from 453f932 to 9767376 Compare December 1, 2022 21:39
@sroucheray
Copy link
Contributor Author

sroucheray commented Dec 1, 2022

I made some changes to my PR. Following your explanations I tested it more thoroughly in a combination of many cases:

  • render style: view, read, focused
  • with or without route prefix
  • within a deep path or not

I think this PR fix also small bugs where RapiDoc does not take into account the hash, for example here https://rapidocweb.com/examples/read-mode.html#get-/store/order/-orderId- (the hash is changed when the page loads at least in Chrome on Linux).

With this PR, the ID of an element (to open or to scroll to) is extracted from the URL whether it is in a hash or in the URL path with a routePrefix.

I made a lot of tests manually. I think for the sake of the project it would be good to have unit tests to avoid regression and make sure I don't introduce one :)

@sroucheray sroucheray force-pushed the fix/replace-history-state branch from 9767376 to 0955bf7 Compare December 2, 2022 07:54
@mrin9 mrin9 merged commit 5501ef0 into rapi-doc:master Dec 2, 2022
@mrin9
Copy link
Collaborator

mrin9 commented Dec 2, 2022

thank you for the contribution, another nice feature and code cleanup !!!

@sroucheray sroucheray deleted the fix/replace-history-state branch December 2, 2022 10:44
colvinco added a commit to colvinco/RapiDoc that referenced this pull request Apr 15, 2025
I noticed a regression when upgrading from 9.3.2, in focused mode previously the page would default scroll to the `Overview` when no `#` route is provided.

The getElementId changes for rapi-doc#852 meant that when no route is provided, the entire url would be returned, where previously an empty/undefined value would be used and hit https://github.com/rapi-doc/RapiDoc/blob/7f53d25959e5a4e1beb4b610aaef445b896838f2/src/rapidoc.js#L808-L814 - which doesn't happen when the url is returned.
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.

2 participants