tinro
tinro copied to clipboard
Prevent endless redirect
I was just typing <Route path="/" redirect="/">
in REPL (for #81) and it froze my tab. I assume because of an endless redirect (my cursor was in the redirect
prop and I was about to type there).
To reproduce open REPL and paste <Route path="/" redirect="/" />
It should detect that edge case and not run into an endless loop. Mistakes happen and if path
or redirect
are dynamic this would lock up an app.
Maybe add a console.error
for this case in dev mode
Any suggestion to solve this? I don't want to rise a timer for each redirect action.
Can you check if the to-be-redirected URL exactly matches the current URL (including hash, query and everything)? I doubt anyone wants to have the exact same URL twice in the history stack.
How to detect this case?
<Route path="/bar" redirect="/foo"/>
<Route path="/foo" redirect="/bar"/>
I don't know if we need to go that far for a feature that's mainly meant to prevent users from shooting in their own foot.
An algorithm to solve this would be to build a graph of all redirects (/foo
-> /bar
and /bar
-> /foo
as two nodes with two edges) and check if it is free of cycles.
I think as a first step checking path === redirect
prop and then logging an error would be great.
- Graph is impossible, because routes are unknown. Tinro knows only opened childs hierarchy.
- Size of graph algorithm may increase tinro size more than twice.
- Half solution is not a solution.
- I know nothing about tinro's architecture. Couldn't you hold the graph in
<script context="module">
of<Route>
and each Route adds/updates/removes itself? - Like I said I don't think it's feasible either because it adds too much complexity (worst case it adds more bugs with false positives)
- Catching
path === redirect
would be one less bug