Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[core] only hydrate values present in paths #268

Open
3 tasks done
conor-odro opened this issue Jan 9, 2024 · 0 comments
Open
3 tasks done

[core] only hydrate values present in paths #268

conor-odro opened this issue Jan 9, 2024 · 0 comments
Labels
✨ enhancement New feature or request

Comments

@conor-odro
Copy link

conor-odro commented Jan 9, 2024

Clear and concise description of the problem

I'm unsure if it's an intended feature of this plugin or an overlooked "bug", but recently I encountered an issue where I was persisting an entire store to localStorage by accident in a Production app. To remedy this I added the paths option to only persist the values I required, however users who already had the entire store in their localStorage (including stale values) are finding the Pinia store being fully hydrated by whats in localStorage rather than just the values present in paths.

Suggested solution

function hydrateStore(
  store: Store,
-  { storage, serializer, key, debug }: Persistence,
+ { storage, serializer, key, paths, debug }: Persistence,
) {
  try {
    const fromStorage = storage?.getItem(key)
-   if (fromStorage)
-     store.$patch(serializer?.deserialize(fromStorage))
+   if (fromStorage){
+     const deserialisedStorage = serializer?.deserialize(fromStorage);
+     const hydratedObject = Array.isArray(paths) ? pick(deserialisedStorage, paths) : deserialisedStorage;
+     store.$patch(hydratedObject)
+   }
  }
  catch (e) {
    if (debug)
      console.error('[pinia-plugin-persistedstate]', e)
  }
}

If you agree with the above solution I'm more than happy to put a PR in for it. Alternatively if this is an intended feature of the plugin then we could add another option to the Persistence interface to toggle my new feature on/off as I think other people may also find this useful.

Alternative

No response

Additional context

No response

Validations

@conor-odro conor-odro added the ✨ enhancement New feature or request label Jan 9, 2024
@prazdevs prazdevs changed the title Only hydrate values present in paths [core] only hydrate values present in paths Jan 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
✨ enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant