solidjs / solid-router

A universal router for Solid inspired by Ember and React Router
MIT License
1.1k stars 137 forks source link

fix: hash router state reset with `replace: false` #417

Closed oedotme closed 1 month ago

oedotme commented 2 months ago

Prior to v0.13.3, it seems the router source value was triggered twice when navigating between different paths.

In the first run, next has both path + state values, and in the second run, it has only the path value:

https://github.com/solidjs/solid-router/blob/57847624fb673003f6f0d8261ca7352ff00e7480/src/routers/createRouter.ts#L39-L42


It seems after introducing the state detection fix at #405, it surfaced this behavior — see #416 issue:

https://github.com/solidjs/solid-router/blob/57847624fb673003f6f0d8261ca7352ff00e7480/src/routing.ts#L344-L358

As the start function was only running once as the path hasn't changed in the second run. But now start runs twice which causes the state to reset to undefined unexpectedly.


I've noticed the behavior is different when setting replace: true while navigating. Changing the HashRouter set function to use window.history.pushState(...) seems to fix it for replace: false (default):

https://github.com/solidjs/solid-router/blob/57847624fb673003f6f0d8261ca7352ff00e7480/src/routers/HashRouter.ts#L27-L31

-         window.location.hash = value;
+         window.history.pushState(state, "", "#" + value);

Closes #416

changeset-bot[bot] commented 2 months ago

🦋 Changeset detected

Latest commit: 2e90de51745ee0b07043eaa2fbc938c754e61711

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package | Name | Type | | --------------- | ----- | | @solidjs/router | Patch |

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

ryansolid commented 1 month ago

Thanks.