tinro icon indicating copy to clipboard operation
tinro copied to clipboard

Prevent endless redirect

Open Prinzhorn opened this issue 3 years ago • 7 comments

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

Prinzhorn avatar Aug 18 '21 07:08 Prinzhorn

Any suggestion to solve this? I don't want to rise a timer for each redirect action.

AlexxNB avatar Aug 18 '21 18:08 AlexxNB

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.

Prinzhorn avatar Aug 19 '21 15:08 Prinzhorn

How to detect this case?

<Route path="/bar" redirect="/foo"/>
<Route path="/foo" redirect="/bar"/>

AlexxNB avatar Aug 19 '21 16:08 AlexxNB

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.

graph

Prinzhorn avatar Aug 19 '21 16:08 Prinzhorn

I think as a first step checking path === redirect prop and then logging an error would be great.

Prinzhorn avatar Aug 19 '21 16:08 Prinzhorn

  1. Graph is impossible, because routes are unknown. Tinro knows only opened childs hierarchy.
  2. Size of graph algorithm may increase tinro size more than twice.
  3. Half solution is not a solution.

AlexxNB avatar Aug 19 '21 16:08 AlexxNB

  1. 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?
  2. 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)
  3. Catching path === redirect would be one less bug

Prinzhorn avatar Aug 20 '21 06:08 Prinzhorn