elm-book
elm-book copied to clipboard
fix: avoid infinite loop when hitting an inexistent route
closes #48
Testing the pr
When testing the pr, remember to
- Add
ElmBook.ThemeOptions.useHashBasedNavigation
in the UI/ElmBook.elm file - Modify a chapter so to add an inexistent link to test the bugfix
FYI: The links must be in this format #/guides/inexistent
Rationale
When hashBasedNavigation is true and a route that can't be found among the chapters is hit, this is what happens:
- OnUrlChange is triggered
-
extractPath url_
is called and returns/guides/inexistent
(url.fragment) which is then assigned tourl
- The catch all "_" case executes
- An urlChapter can't be found so it goes in the
Nothing
case - The
Nothing
case navigates to#/
- The OnUrlChange is triggered again
-
extractPath url_
is called and returns/
(url.fragment
isNothing
henceMaybe.withDefault
kicks in) which is then assigned tourl
- The "/" case executes
- It gets the fallback chapter's url (
/overview
in the case of this repo) and navigates there, which is a problem because we have hashBasedNavigation set to true - The OnUrlChange is triggered again
-
extractPath url_
is called and when hashBasedNavigation is true it returns the fragment or/
and in the case of/overview
the fragment is Nothing. - From now on it repeats from 8 to 12 infinitely
Solution
How do we stop it? Should be simple honestly, when the fallback chapter is retrieved, let's add the hashtag on front.
Screencast
https://github.com/user-attachments/assets/669b83f2-15fd-409f-9774-57412e7940c1