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
[Custom fields] Create hook to lazy load custom field components #14873
Conversation
|
||
if (componentUids.length) { | ||
const componentPromises = componentUids.map((uid) => { | ||
const customField = customFieldsRegistry.get(uid); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ℹ️ Instead of using this just for custom fields, in the future we could replace this with the fields API if we ensure all fields added are dynamic imports and we add custom fields to the Fields object when the app starts.
Codecov ReportBase: 59.68% // Head: 59.73% // Increases project coverage by
Additional details and impacted files@@ Coverage Diff @@
## main #14873 +/- ##
==========================================
+ Coverage 59.68% 59.73% +0.05%
==========================================
Files 1342 1345 +3
Lines 32629 32664 +35
Branches 6224 6227 +3
==========================================
+ Hits 19473 19513 +40
+ Misses 11293 11289 -4
+ Partials 1863 1862 -1
Flags with carried forward coverage won't be shown. Click here to find out more.
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. ☔ View full report at Codecov. |
const customFieldsRegistry = useCustomFields(); | ||
|
||
useEffect(() => { | ||
const lazyLoadComponents = async (uids, components) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ℹ️ Two independent arrays that then become key value pairs on an object feels a bit strange but I didn't see another way to make it work with Promise.all
. I think it is reliable since Promise.all
returns them in the same order, but open to other suggestions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not a full review as I need to look more into it, but I wanted to share my first thoughts.
Also thanks for the detailed PR notes, it's super useful especially with the resources you link to
packages/core/admin/admin/src/content-manager/hooks/useLazyComponents/index.js
Show resolved
Hide resolved
packages/core/admin/admin/src/content-manager/hooks/useLazyComponents/index.js
Show resolved
Hide resolved
...core/admin/admin/src/content-manager/hooks/useLazyComponents/tests/useLazyComponents.test.js
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for adding the tests 👌
I had the chance to test it more thoroughly and this seems solid to me. My only suggestion would be to add an ErrorBoundary component where the Suspense used to be, because currently if one of the dynamic imports fails the app crashes entirely
@soupette I will apply Rémi's feedback but we were also wondering if you might have the time to give this a review? |
...es/core/admin/admin/src/content-manager/pages/EditView/utils/getCustomFieldUidsFromLayout.js
Outdated
Show resolved
Hide resolved
@@ -21,7 +21,7 @@ permissions: {} | |||
jobs: | |||
deploy: | |||
permissions: | |||
contents: write # to push pages branch (peaceiris/actions-gh-pages) | |||
contents: write # to push pages branch (peaceiris/actions-gh-pages) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure why I have this diff for github workflows 🤷. I'm assuming husky made these changes when I committed after merging and resolving conflicts but not sure how they made it on main...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea to use reselect here 👍
What does it do
React.lazy
andReact.Suspense
useLazyComponents
hook that handles the lazy loading of custom field componentsEditView
page vs theInputs
component and returns theLoadingPageIndicator
until all components have loadedWhy is it needed
React.lazy
is not intended to be used inside a component, it is designed to be used outside the component with other imports: https://reactjs.org/docs/code-splitting.html, https://beta.reactjs.org/apis/react/lazyReact.lazy
inuseMemo
to prevent a re-render. I found similar workarounds here, but I think this is ill advised since the useMemo React docs state:React.Suspense
to be the direct parent of the lazy loaded component (no idea why that worked)How to test it
Related issue(s)/PR(s)
fixes #14489