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

feat(sanity): memoize initial value resolver #6614

Merged
merged 3 commits into from May 16, 2024

Conversation

juice49
Copy link
Contributor

@juice49 juice49 commented May 9, 2024

Description

Wherever Studio allows users to create new documents, it runs getTemplatePermissions, which subsequently resolves the initial value of every schema type. It's troublesome for schema types that use an initialValue function (especially async function that may call an HTTP API), because it causes that function to be called at multiple unexpected times.

  • When rendering a document list containing a type with an initialValue function, a PaneHeaderCreateButton is rendered.
  • When the Studio navbar renders.
  • When rendering the document pane for any type of document, ReferenceInputOptionsProvider is rendered.

We cannot simply remove initial value resolution. Studio must resolve initial values before allowing the user to create a new document in case those values would preclude the user from viewing the document they just created. e.g. if the user is prohibited from viewing documents where the secret field is true, they should not be given the option to create such documents.

In order to minimise unnecessary calls to the initialValue function, this branch memoizes the initialValue function when it's called from getTemplatePermissions.

When a document is created, memoization isn't applied. This ensures newly created documents aren't given stale values. A contrived example of when this would be an issue is if an initialValue function returns the current time. This does mean it would be possible for the user to create a document they don't have permission to view if some piece of data controlling this changed since the initial getTemplatePermissions call, but this scenario seems unlikely.


To see this for yourself, try tracking how many times an initialValue function is called when loading Studio with the document pane visible.

Before change: 42 calls

Screenshot 2024-05-10 at 23 12 40

After change: 3 calls (once for the global document creation UI, once for the document list document creation UI, and once to populate the new document)

Screenshot 2024-05-10 at 23 13 29

What to review

Does this approach make sense? Are there any scenarios I've missed (such as various initial value template configurations)?

Testing

Added tests in packages/sanity/src/core/templates/__tests__/resolve.test.ts.

Notes for release

Adds memoization to minimise initial value function calls.

Copy link

vercel bot commented May 9, 2024

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

Name Status Preview Comments Updated (UTC)
performance-studio ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 16, 2024 3:55pm
test-next-studio ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 16, 2024 3:55pm
test-studio ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 16, 2024 3:55pm
1 Ignored Deployment
Name Status Preview Comments Updated (UTC)
studio-workshop ⬜️ Ignored (Inspect) Visit Preview May 16, 2024 3:55pm

Copy link
Contributor

No changes to documentation

Copy link
Contributor

github-actions bot commented May 10, 2024

Component Testing Report Updated May 16, 2024 3:59 PM (UTC)

File Status Duration Passed Skipped Failed
comments/CommentInput.spec.tsx ✅ Passed (Inspect) 36s 15 0 0
formBuilder/ArrayInput.spec.tsx ✅ Passed (Inspect) 6s 3 0 0
formBuilder/inputs/PortableText/Annotations.spec.tsx ✅ Passed (Inspect) 26s 6 0 0
formBuilder/inputs/PortableText/copyPaste/CopyPaste.spec.tsx ✅ Passed (Inspect) 31s 11 7 0
formBuilder/inputs/PortableText/Decorators.spec.tsx ✅ Passed (Inspect) 14s 6 0 0
formBuilder/inputs/PortableText/FocusTracking.spec.tsx ✅ Passed (Inspect) 36s 15 0 0
formBuilder/inputs/PortableText/Input.spec.tsx ✅ Passed (Inspect) 1m 17s 20 0 0
formBuilder/inputs/PortableText/ObjectBlock.spec.tsx ✅ Passed (Inspect) 1m 3s 18 0 0
formBuilder/inputs/PortableText/PresenceCursors.spec.tsx ✅ Passed (Inspect) 7s 3 9 0
formBuilder/inputs/PortableText/RangeDecoration.spec.tsx ✅ Passed (Inspect) 21s 9 0 0
formBuilder/inputs/PortableText/Styles.spec.tsx ✅ Passed (Inspect) 14s 6 0 0
formBuilder/inputs/PortableText/Toolbar.spec.tsx ✅ Passed (Inspect) 29s 12 0 0

binoy14
binoy14 previously approved these changes May 15, 2024
Copy link
Contributor

@binoy14 binoy14 left a comment

Choose a reason for hiding this comment

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

Makes sense to me, thanks!

@juice49 juice49 added this pull request to the merge queue May 16, 2024
Merged via the queue into next with commit 39bab8a May 16, 2024
39 checks passed
@juice49 juice49 deleted the fix/sdx-1321-memoize-initial-value-resolver branch May 16, 2024 19:35
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.

None yet

2 participants