[gloo_history::HashHistory
] Assertion failed when calling HashHistory
's location()
#470
Labels
bug
Something isn't working
Assertion failed when calling
HashHistory
'slocation()
HashHistory
'slocation()
asserts that what comes after the '#' in the URL starts with '/'. However the user could delete that '/', and if you then calllocation()
, the assertion will trigger, saying "You cannot use relative path with this history type.".In my specific case, I have a listener that changes the app's state accordingly when the user changes the URL. I do this using
gloo_history::HashHistory
. If the user inputs a URL with the wrong format, I cannot handle it usingHashHistory
.Currently the only way I found to handle this is to first check the hash manually using
gloo_history::BrowserHistory
:Steps to Reproduce
Option 1: manually
gloo_history::HashHistory
location()
ongloo_history::HashHistory
Option 2: unit test
crates/history/tests/hash_history.rs
history_works()
wasm-pack test --firefox --headless crates/history/ --test hash_history
(or similar)Expected Behavior
Returning a location with empty path, maybe? Or making
location()
returnOption
so that this returnsNone
? I'm not sure (suggestion are welcome). Definitely not an exceptionActual Behavior
Exception thrown
Additional Context
HashHistory
tries to prevent this case from happening, but it does so during thread-local initialization, which only happens the first time you create aHashHistory
:This code does work as expected, except since the user can change the URL without triggering a page reload, the assumption that fixing the URL hash once will be enough is not actually true.
Not sure it's an ideal fix (since it is kinda lying about the true state of the URL), but the following patch makes the modified unit test I suggest in the "Steps to Reproduce" section ("Option 2: unit test") pass:
If you like this I could try submitting a PR, but I would like to first hear your thoughts. In any case, I feel that if I'm using
HashHistory
and using its API correctly (providing paths starting with '/', etc.), it shouldn't be possible for assertions to fail.The text was updated successfully, but these errors were encountered: