mefechoel / svelte-navigator

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

State is replaced when navigating to the same page but with no search path #86

Closed vmiliantsei closed 1 year ago

vmiliantsei commented 1 year ago

Describe the bug A clear and concise description of what the bug is.

When navigating from "/some/page?from=1" to "/some/page" the history state is replaced instead of being pushed.

To Reproduce Steps to reproduce the behavior:

  1. Go to "/some/page?from=1"
  2. Click on a link that leads to "/some/page"
  3. Hit browser's "back" button
  4. You won't be at expected "/some/page?from=1" page

Expected behavior A clear and concise description of what you expected to happen.

Browser's "back" button makes you go back to the previous page.

Additional context Add any other context about the problem here.

The following patch fixes the issue:

diff --git a/src/Link.svelte b/src/Link.svelte
index 3c00f09..6ba48c7 100644
--- a/src/Link.svelte
+++ b/src/Link.svelte
@@ -33,7 +33,7 @@
    // when for example an :id changes in the parent Routes path
    $: href = resolve(to, $location);
    $: isPartiallyCurrent = startsWith($location.pathname, href);
-   $: isCurrent = href === $location.pathname;
+   $: isCurrent = href === $location.pathname + $location.search;
    $: ariaCurrent = isCurrent ? { "aria-current": "page" } : {};
    $: props = (() => {
        if (isFunction(getProps)) {
mefechoel commented 1 year ago

Hey there! Yes, that's a bug. I think your suggested fix doesn't quite cut it though, as it would cause the aria-current attribute to not be applied in some cases. But by introducing a new variable that checks if there is an exact match, this can be fixed. I'll look into it :)

mefechoel commented 1 year ago

Fixed in 3.2.0