Skip to content

Commit 9338467

Browse files
authored
feat: lazy load design library (#3632)
* lazy load design library * display errors * code rabbit's qa fixes * fix: handle if there are no errors --------- Co-authored-by: [email protected] <>
1 parent 6464e82 commit 9338467

File tree

26 files changed

+75
-116
lines changed

26 files changed

+75
-116
lines changed

src/components/design-library-list/favorite-button.js

Lines changed: 0 additions & 53 deletions
This file was deleted.

src/components/design-library-list/images/heart-fill.svg

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/components/design-library-list/images/heart.svg

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/components/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ export { default as SortControl } from './sort-control'
2020
export { default as PanelAdvancedSettings } from './panel-advanced-settings'
2121
export { default as PanelDesignLibrary } from './panel-design-library'
2222
export { default as InspectorPanelControls } from './panel-tabs/inspector-panel-controls'
23-
export { default as DesignLibraryList } from './design-library-list'
2423
export { default as PanelTabs } from './panel-tabs'
2524
export { default as Button } from './button'
2625
export { default as DesignPanelBody } from './design-panel-body'

src/components/modal-design-library/index.js

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
/**
2-
* Internal dependencies
3-
*/
4-
import { ModalDesignLibrary } from './modal'
5-
61
/**
72
* WordPress dependencies
83
*/
94
import { applyFilters } from '@wordpress/hooks'
10-
import { useMemo, useCallback } from '@wordpress/element'
5+
import {
6+
useMemo, useCallback, Suspense, lazy,
7+
} from '@wordpress/element'
118
import { useLocalStorage } from '~stackable/util'
129

1310
export const Switcher = props => {
@@ -21,19 +18,25 @@ export const Switcher = props => {
2118
// disabled, always default to the first version
2219
const apiVersion = versions.includes( _apiVersion ) ? _apiVersion : versions[ 0 ]
2320

21+
// Lazy-load the ModalDesignLibrary to reduce initial bundle size
22+
// Note: We import the named export and set it as default for React.lazy
23+
const LazyModalDesignLibrary = lazy( () => import( /* webpackChunkName: "design-library" */ '~stackable/lazy-components/design-library' ) )
24+
2425
const ModalComponent = useMemo( () => {
25-
return applyFilters( 'stackable.design-library.modal-component', ModalDesignLibrary, apiVersion )
26+
return applyFilters( 'stackable.design-library.modal-component', LazyModalDesignLibrary, apiVersion )
2627
}, [ apiVersion ] )
2728

2829
const onChangeApiVersion = useCallback( v => setApiVersion( v ), [] )
2930

3031
return (
31-
<ModalComponent
32-
hasVersionSwitcher={ versions.length > 1 }
33-
apiVersion={ apiVersion }
34-
onChangeApiVersion={ onChangeApiVersion }
35-
{ ...props }
36-
/>
32+
<Suspense fallback={ null }>
33+
<ModalComponent
34+
hasVersionSwitcher={ versions.length > 1 }
35+
apiVersion={ apiVersion }
36+
onChangeApiVersion={ onChangeApiVersion }
37+
{ ...props }
38+
/>
39+
</Suspense>
3740
)
3841
}
3942

src/design-library/index.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ export const fetchDesignLibrary = async ( forceReset = false, version = '', type
2929
designLibrary[ type ] = designsPerType
3030

3131
if ( type === 'patterns' ) {
32-
designs = designsPerType[ LATEST_API_VERSION ]
32+
designs = designsPerType[ LATEST_API_VERSION ] ?? {}
3333
} else {
34-
pages = designsPerType[ LATEST_API_VERSION ]
34+
pages = designsPerType[ LATEST_API_VERSION ] ?? {}
3535
}
3636
}
3737

38-
return designLibrary[ type ][ version || LATEST_API_VERSION ]
38+
return designLibrary[ type ]?.[ version || LATEST_API_VERSION ] ?? designLibrary[ type ]
3939
}
4040

4141
export const fetchDesign = async designId => {
@@ -63,6 +63,13 @@ export const getDesigns = async ( {
6363
} ) => {
6464
const designLibrary = await fetchDesignLibrary( reset, LATEST_API_VERSION, type )
6565

66+
if ( designLibrary.wp_remote_get_error || designLibrary.content_error ) {
67+
const error = designLibrary.wp_remote_get_error ?? designLibrary.content_error
68+
// eslint-disable-next-line no-console
69+
console.error( 'Stackable: ', error )
70+
return { error }
71+
}
72+
6673
// pre-fetch patterns
6774
if ( type === 'pages' ) {
6875
await fetchDesignLibrary()
File renamed without changes.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { createContext, useContext } from '@wordpress/element'
2+
3+
export const DesignLibraryContext = createContext( null )
4+
5+
export const useDesignLibraryContext = () => {
6+
return useContext( DesignLibraryContext )
7+
}

src/components/design-library-list/default.json renamed to src/lazy-components/design-library/design-library-list/default.json

File renamed without changes.

src/components/design-library-list/design-library-list-item.js renamed to src/lazy-components/design-library/design-library-list/design-library-list-item.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
/**
22
* Internal dependencies.
33
*/
4-
import ProControl from '../pro-control'
5-
import Button from '../button'
64
import { DesignPreview } from './design-preview'
75
import { useShadowRoot } from './use-shadow-root'
86
import { usePreviewRenderer } from './use-preview-renderer'
@@ -13,7 +11,9 @@ import { useAutoScroll } from './use-auto-scroll'
1311
*/
1412
import { isPro, i18n } from 'stackable'
1513
import classnames from 'classnames'
16-
import { Tooltip } from '~stackable/components'
14+
import {
15+
Tooltip, Button, ProControl,
16+
} from '~stackable/components'
1717

1818
/**
1919
* WordPress dependencies.

0 commit comments

Comments
 (0)