Description
What is the new or updated feature that you are suggesting?
For the HashRouter
, it should be possible to start the location.hash
with #
instead of #/
. To this end, I request the ability to serialize relative location instead of absolute location.
PR #8460 supports writing the relative path in the URL, configured to take effect when the Router
basename
property is given as ""
, instead of the default of "/"
.
UPDATE:
This is now possible with [email protected]
and use-hash-history
. I hope it will be soon available with an upcoming release of history
through PR #911.
Examples
When using HashRouter
, the default basename is "/"
. This results in urls like this:
example.com/#/
example.com/#/abc
example.com/#/1/2/3
After PR #8460, using <HashRouter basename="">
results in urls like this:
example.com/#
example.com/#abc
example.com/#1/2/3
I've created a codepen to compare the current behavior of <HashRouter basename="">
for the current V6 of react-router-dom and my PR #8460
Why should this feature be included?
In react-router-dom@v5, it was once possible to append directly after #
with hashType="noslash"
. The hashType
property of createHashHistory
was available in history@4, but lost in history@5, so the additional configuration it afforded the HashRouter
was removed in react-router-dom@6
with no replacement.
In react-router-dom@6
, there is no possibility of hashType="noslash"
, so I propose we implement it whenever the user provides basename=""
. The other missing hashType of hashType="hashbang"
could also supported with basename="!#"
, but that might be a separate issue.
The ability to begin hashes with #
instead of #/
is not only cosmetic. I am migrating an existing website from vanilla javascript to react, and backwards compatibility of existing URL hash values requires #
instead of #/
.