mefechoel / svelte-navigator

Simple, accessible routing for Svelte
Other
506 stars 39 forks source link

useLocation() got triggered twice when we call navigate(-1) #114

Open billy-onggo opened 7 months ago

billy-onggo commented 7 months ago

I encountered inconsistency on useLocation hook when programmatically navigating routes via navigate(-1) compared to history.back(-1) or hitting the browser's back button.

Is this intented? Given the current documentation states:

navigate(-1) is equivalent to hitting the back button in your browser https://github.com/mefechoel/svelte-navigator/tree/main#parameters-1

Steps to reproduce:

Expected Behaviour The navigate(-1) method should trigger the location change only once

Note:

Sample Code

<script lang="ts">
  import { Route, useNavigate, useLocation } from "svelte-navigator";
  const navigate = useNavigate()

  const location = useLocation();
  location.subscribe((l) => console.log(l))
</script>

<div>
  <h2>My Nested Route, using navigate</h2>
  <div>
    <button on:click={()=> navigate('one')}>one</button>
    <button on:click={()=> navigate('two')}>two</button>
    <button on:click={()=> navigate(-1)}>BACK</button>
    <button on:click={()=> navigate(1)}>FORWARD</button>
  </div>
  <div>
    <Route path="/one" primary={false}>Page One</Route>
    <Route path="/two" primary={false}>Page Two</Route>
  </div>
</div>

Screenshots image

Desktop