Draft: Add event.persisted check in restore handler to prevent unnecessary rehydration on page load#66
Closed
d8corp wants to merge 6 commits intonanostores:mainfrom
Closed
Draft: Add event.persisted check in restore handler to prevent unnecessary rehydration on page load#66d8corp wants to merge 6 commits intonanostores:mainfrom
d8corp wants to merge 6 commits intonanostores:mainfrom
Conversation
Member
I afraid of this change. If I create a store, it will not subscribe to So we could have error here:
|
# Conflicts: # index.js
Contributor
Author
|
@ai, please review these changes. Should we keep the Our I also added:
|
Contributor
Author
# Conflicts: # README.md # package.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
📌 Summary
This PR enhances the reliability of the
persistentAtomstore hydration logic by introducing a check forevent.persistedin thepageshowevent handler (restorefunction). This ensures that state is only restored from persistent storage when the page is being restored from the bfcache (back-forward cache), and not on the initial page load — where the store is already correctly initialized fromlocalStorage.🔍 Problem
Previously, the
restorefunction was called on everypageshowevent — including the initial page load. This caused redundant and potentially conflicting rehydration of the store fromlocalStorage.✅ Solution
Added this guard clause in the
restorefunction:This ensures that state restoration only occurs when:
This matches the intended behavior of persistent storage: recover state after a cached navigation, not reapply it on every load.
💡 Why This Matters
pageshow.persisted === trueis the standard way to detect bfcache restoration.🛠 Changes
if (e && !e.persisted) returntorestorefunction inpersistentAtomuseTestStorageEngine, etc.) remain unchanged and compatible📚 Background
pageshoweventevent.persistedistrueif the page was restored from bfcache,falseon initial load.📌 Recommendation
This is a bug fix and performance improvement, not a feature. It makes the library behave more predictably in modern browsers with bfcache enabled (which is now the default in Chrome, Firefox, and Safari).
Merge this to improve user experience and eliminate edge-case bugs.