Skip to content

fix: state updates detection on history back/forward - #405

Merged
ryansolid merged 5 commits into
solidjs:mainfrom
oedotme:main
May 1, 2024
Merged

ryansolid merged 5 commits into
solidjs:mainfrom
oedotme:main

Conversation

@oedotme

@oedotme oedotme commented Apr 15, 2024

Copy link
Copy Markdown
Contributor

This PR adds an event listener to popstate change event to update the router state with the history back/forward events between two routes with the same paths but with different state objects.

Currently the changes are only detected by refreshing the page.

Closes #275 and #396

@changeset-bot

changeset-bot Bot commented Apr 15, 2024 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 82f8035

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

Copy link
Copy Markdown
Member

I see what you are trying to do but could this already be handled by the current popstate event? I'm gathering this is related to that we don't have access to the state representation where we wire the events.

I'm just looking at:

init: notify => bindEvent(window, "popstate",
notifyIfNotBlocked(notify, delta => {
if (delta && delta < 0) {
return !beforeLeave.confirm(delta);
} else {
const s = getSource();
return !beforeLeave.confirm(s.value, { state: s.state });
}
})
),

And more specifically:

return () => {
const prevDepth = depth;
saveCurrentDepth();
const delta = prevDepth == null ? null : depth - prevDepth;
if (ignore) {
ignore = false;
return;
}
if (delta && block(delta)) {
ignore = true;
window.history.go(-delta);
} else {
notify();
}

And thinking there should be some way to set the source state properly and ensure that the notifications are working properly. Like the bug seems most likely with our way of telling things have changed rather than sideloading in a second event.

@oedotme

oedotme commented Apr 24, 2024

Copy link
Copy Markdown
Contributor Author

@ryansolid Thanks for your response, and yeah that makes sense.

From what I found so far, the source signal doesn't trigger updates when the state changes while the path value is the same, because of the following equals condition:

createSignal(wrap(config.get()), { equals: (a, b) => a.value === b.value }),

To allow the state change detection, we can set equals to:

  • false
  • or (a, b) => a.value === b.value && JSON.stringify(a.value) === JSON.stringify(b.value)
  • or even JSON.stringify(a) === JSON.stringify(b)

I went with false to avoid JSON.stringify potential performance hits, but please let me know what you think:

-       createSignal(wrap(config.get()), { equals: (a, b) => a.value === b.value }),
+       createSignal(wrap(config.get()), { equals: false }),


The other related logic that skips updates if the path hasn't changed:

solid-router/src/routing.ts

Lines 333 to 343 in 3cd2fef

if (value !== reference()) {
start(() => {
intent = "native";
setReference(value);
setState(state);
resetErrorBoundaries();
submissions[1]([]);
}).then(() => {
intent = undefined;
});
}

I was unsure about resetting error-boundaries and submission here as well, so I just added an else statement in order to handle the state-only changes:

+      } else {
+        start(() => {
+          intent = "native";
+          setState(state);
+        }).then(() => {
+          intent = undefined;
+        });
      }

Those two changes solved the state-only detection issue. Please let me know what you think, thanks!

@ryansolid

Copy link
Copy Markdown
Member

I see.. hmm.. yeah that equality check I guess needs to fail here but I do wonder if this can cause other issues. Shortcuts when trying to navigate to the same route. Probably nothing to sever.

We should reset errorboundaries and submissions in this case, because it is a navigation.

@oedotme

oedotme commented Apr 30, 2024

Copy link
Copy Markdown
Contributor Author

@ryansolid I re-added the equals check again just in case, to avoid any potential unexpected behaviors.

I also added an object equality comparison utility based on fast-deep-equal to compare the state, which addresses the performance concern I initially had with JSON.stringify.

Please let me know what you think. Thanks!

@ryansolid

ryansolid commented Apr 30, 2024 •

Copy link
Copy Markdown
Member

I'm going to play around with this and see if the equal check is important. I'm as concerned with adding more code as I am with JSON.stringify performance. So if we can avoid doing much here. My gut here is if the state reference is different that will be sufficient. It might be aggressive but it is probably fine. Like what would be the point of state if someone was just mutating the same reference. You wouldn't be able to go back and forth, as you already changed it.

Could it not just be?

equals: (a, b) => a.value === b.value && a.state === b.state }

@oedotme

oedotme commented May 1, 2024

Copy link
Copy Markdown
Contributor Author

@ryansolid Checking the state reference makes total sense in this case.

Could it not just be?

equals: (a, b) => a.value === b.value && a.state === b.state }

Yes, I've update the PR accordingly. Thanks again.

@ryansolid

Copy link
Copy Markdown
Member

Looks great thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Undetected location state changes while using browser back/forward buttons

2 participants