thepassle / app-tools

128 stars 8 forks source link

Router bug: hitting browser back button 2 or more times fails to navigate #12

Closed JudahGabriel closed 1 year ago

JudahGabriel commented 1 year ago

I've found a bug in the Router where if you navigate backwards two or more times, it'll never navigate to the previous page.

Minimal repro here (and source here).

Repro steps:

  1. Navigate to page A
  2. Navigate to page B
  3. Navigate to page C
  4. Click browser back button (result: successfully navigates to page B)
  5. Click browser back button (result: bug, it navigates to page B, which we're already on)

I don't yet know the source of the bug, but I'd be glad to submit a PR if you're willing to accept it.

thepassle commented 1 year ago

Im getting this on the repro:

image
thepassle commented 1 year ago

Ah I think its because onPopState we call navigate which then calls pushState 🤦‍♂️i can investigate some more tomorrow, or im happy to take a pr in the meantime

JudahGabriel commented 1 year ago

Thanks, I wondered if that is indeed the issue. Let me try preventing a pushState call on popState callback and see if that fixes it.

JudahGabriel commented 1 year ago

Yep, you're right. I updated the repro so that the code skips calling pushState when popState has been called. This fixes the issue.

I'll submit a PR momentarily.

JudahGabriel commented 1 year ago

OK! Opened this PR which fixes the issue.