-
Notifications
You must be signed in to change notification settings - Fork 310
fix replace history state when using deep path URL #852
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix replace history state when using deep path URL #852
Conversation
d034fe3
to
25ca776
Compare
@@ -791,6 +791,10 @@ export default class RapiDoc extends LitElement { | |||
} | |||
} | |||
|
|||
replaceHistoryState(hashId) { |
There was a problem hiding this comment.
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 theindex.html
in case of plain html or it will belocation.origin
in case of vue/react/angular etcroutePrefix
will be#
in case ofhtml
or will be a non-blank value in case of vue/react/angular etcelementId
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
in the deep-link example you provided above for the reason of this PR |
453f932
to
9767376
Compare
I made some changes to my PR. Following your explanations I tested it more thoroughly in a combination of many cases:
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 :) |
9767376
to
0955bf7
Compare
thank you for the contribution, another nice feature and code cleanup !!! |
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.
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 likehttp://www.mysite.com#overview
instead ofhttp://www.mysite.com/deep/link/rapidoc#overview
.