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

refactor(store) simplify key value store, add SWR through localStorage #7554

Merged
merged 1 commit into from
Sep 27, 2024

Conversation

bjoerge
Copy link
Member

@bjoerge bjoerge commented Sep 27, 2024

Description

While working on a list view optimization I discovered an re-rendering/re-fetching issue that was caused by the key value store first emitting the default value for sort order, and then fetching it from the server. In cases where a user has set a custom sort order, this would always result in two distinct settings to be returned from useStructureToolSetting('<someKey>'), which again would always trigger an extra re-render, and in the case of list views: a re-fetch.

This PR refactors the internals of the key value store to facilitate using LocalStorage as a read-through cache for user settings, and opting for a Stale-While-Revalidate (SWR) approach by first returning what's in local storage, and then fetching the latest settings value from the server, updating the local storage in case of a change. In the majority of cases, the localstorage value will match what's stored on the server, and in these cases we wont trigger any re-fetch/rerenders.

What to review

These are for the most part internal changes, and I took the opportuinity to simplify where I felt it made sense. There might be, however a change in behavior in certain cases in that subscribers of user settings keys now immediately will get a cached value instead of the default value. Let me know if you can think of cases where this can be problematic (I couldn't think of any)

Testing

Notes for release

n/a - internal

Copy link

vercel bot commented Sep 27, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
page-building-studio ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 27, 2024 1:57pm
performance-studio ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 27, 2024 1:57pm
test-compiled-studio ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 27, 2024 1:57pm
test-next-studio ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 27, 2024 1:57pm
test-studio ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 27, 2024 1:57pm
1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
studio-workshop ⬜️ Ignored (Inspect) Sep 27, 2024 1:57pm

Copy link
Contributor

No changes to documentation

Copy link
Contributor

Component Testing Report Updated Sep 27, 2024 2:11 PM (UTC)

✅ All Tests Passed -- expand for details
File Status Duration Passed Skipped Failed
comments/CommentInput.spec.tsx ✅ Passed (Inspect) 43s 15 0 0
formBuilder/ArrayInput.spec.tsx ✅ Passed (Inspect) 8s 3 0 0
formBuilder/inputs/PortableText/Annotations.spec.tsx ✅ Passed (Inspect) 30s 6 0 0
formBuilder/inputs/PortableText/copyPaste/CopyPaste.spec.tsx ✅ Passed (Inspect) 37s 11 7 0
formBuilder/inputs/PortableText/copyPaste/CopyPasteFields.spec.tsx ✅ Passed (Inspect) 0s 0 12 0
formBuilder/inputs/PortableText/Decorators.spec.tsx ✅ Passed (Inspect) 18s 6 0 0
formBuilder/inputs/PortableText/DisableFocusAndUnset.spec.tsx ✅ Passed (Inspect) 10s 3 0 0
formBuilder/inputs/PortableText/DragAndDrop.spec.tsx ✅ Passed (Inspect) 3m 0s 0 0 0
formBuilder/inputs/PortableText/FocusTracking.spec.tsx ✅ Passed (Inspect) 44s 15 0 0
formBuilder/inputs/PortableText/Input.spec.tsx ✅ Passed (Inspect) 1m 44s 21 0 0
formBuilder/inputs/PortableText/ObjectBlock.spec.tsx ✅ Passed (Inspect) 1m 16s 18 0 0
formBuilder/inputs/PortableText/PresenceCursors.spec.tsx ✅ Passed (Inspect) 8s 3 9 0
formBuilder/inputs/PortableText/RangeDecoration.spec.tsx ✅ Passed (Inspect) 26s 9 0 0
formBuilder/inputs/PortableText/Styles.spec.tsx ✅ Passed (Inspect) 18s 6 0 0
formBuilder/inputs/PortableText/Toolbar.spec.tsx ✅ Passed (Inspect) 36s 12 0 0
formBuilder/tree-editing/TreeEditing.spec.tsx ✅ Passed (Inspect) 0s 0 3 0
formBuilder/tree-editing/TreeEditingNestedObjects.spec.tsx ✅ Passed (Inspect) 0s 0 3 0

@bjoerge bjoerge marked this pull request as ready for review September 27, 2024 15:20
@bjoerge bjoerge requested a review from a team as a code owner September 27, 2024 15:20
Copy link
Member

@cngonzalez cngonzalez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, at least for the initial implementations of this feature!

@bjoerge bjoerge added this pull request to the merge queue Sep 27, 2024
Merged via the queue into next with commit d2ca86d Sep 27, 2024
50 checks passed
@bjoerge bjoerge deleted the simplify-key-value-store branch September 27, 2024 16:16
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.

3 participants