Skip to content

Commit 8676932

Browse files
committed
implement preload for lazy load
1 parent ca735ea commit 8676932

File tree

1 file changed

+64
-15
lines changed

1 file changed

+64
-15
lines changed

src/components/Sidebar/B4aSidebarSection.react.js

Lines changed: 64 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,28 @@
77
*/
88
import Icon from 'components/Icon/Icon.react';
99
import { Link } from 'react-router-dom';
10-
import React, { useEffect, useRef, useState } from 'react';
10+
import React, { lazy, Suspense, useState, useRef, useEffect } from 'react';
1111
import Popover from 'components/Popover/Popover.react';
1212
import Position from 'lib/Position';
1313
import styles from 'components/Sidebar/B4aSidebar.scss';
1414
import { amplitudeLogEvent } from 'lib/amplitudeEvents';
15-
import B4aApiIcon from 'components/Sidebar/icons/B4aApiIcon.react';
16-
import B4aDatabaseIcon from 'components/Sidebar/icons/B4aDatabaseIcon.react';
17-
import B4aCloudCodeIcon from 'components/Sidebar/icons/B4aCloudCodeIcon.react';
18-
import B4aWebDeploymentIcon from 'components/Sidebar/icons/B4aWebDeploymentIcon.react';
19-
import B4aMoreIcon from 'components/Sidebar/icons/B4aMoreIcon.react';
20-
import B4aOverviewIcon from 'components/Sidebar/icons/B4aOverviewIcon.react';
21-
import B4aAppSettingsIcon from 'components/Sidebar/icons/B4aAppSettingsIcon.react';
15+
16+
const LazyB4aApiIcon = lazy(() => import('components/Sidebar/icons/B4aApiIcon.react'));
17+
const LazyB4aDatabaseIcon = lazy(() => import('components/Sidebar/icons/B4aDatabaseIcon.react'));
18+
const LazyB4aCloudCodeIcon = lazy(() => import('components/Sidebar/icons/B4aCloudCodeIcon.react'));
19+
const LazyB4aWebDeploymentIcon = lazy(() => import('components/Sidebar/icons/B4aWebDeploymentIcon.react'));
20+
const LazyB4aMoreIcon = lazy(() => import('components/Sidebar/icons/B4aMoreIcon.react'));
21+
const LazyB4aOverviewIcon = lazy(() => import('components/Sidebar/icons/B4aOverviewIcon.react'));
22+
const LazyB4aAppSettingsIcon = lazy(() => import('components/Sidebar/icons/B4aAppSettingsIcon.react'));
23+
24+
// Preload functions for each lazy icon
25+
LazyB4aApiIcon.preload = () => import('components/Sidebar/icons/B4aApiIcon.react');
26+
LazyB4aDatabaseIcon.preload = () => import('components/Sidebar/icons/B4aDatabaseIcon.react');
27+
LazyB4aCloudCodeIcon.preload = () => import('components/Sidebar/icons/B4aCloudCodeIcon.react');
28+
LazyB4aWebDeploymentIcon.preload = () => import('components/Sidebar/icons/B4aWebDeploymentIcon.react');
29+
LazyB4aMoreIcon.preload = () => import('components/Sidebar/icons/B4aMoreIcon.react');
30+
LazyB4aOverviewIcon.preload = () => import('components/Sidebar/icons/B4aOverviewIcon.react');
31+
LazyB4aAppSettingsIcon.preload = () => import('components/Sidebar/icons/B4aAppSettingsIcon.react');
2232

2333
const sendEvent = () => {
2434
// eslint-disable-next-line no-undef
@@ -29,19 +39,47 @@ const sendEvent = () => {
2939
const getIconContent = (icon) => {
3040
switch (icon) {
3141
case 'b4a-api-icon':
32-
return <B4aApiIcon />;
42+
return (
43+
<Suspense fallback={null}>
44+
<LazyB4aApiIcon />
45+
</Suspense>
46+
);
3347
case 'b4a-database-icon':
34-
return <B4aDatabaseIcon />;
48+
return (
49+
<Suspense fallback={null}>
50+
<LazyB4aDatabaseIcon />
51+
</Suspense>
52+
);
3553
case 'b4a-cloud-code-icon':
36-
return <B4aCloudCodeIcon />;
54+
return (
55+
<Suspense fallback={null}>
56+
<LazyB4aCloudCodeIcon />
57+
</Suspense>
58+
);
3759
case 'b4a-web-deployment-icon':
38-
return <B4aWebDeploymentIcon />;
60+
return (
61+
<Suspense fallback={null}>
62+
<LazyB4aWebDeploymentIcon />
63+
</Suspense>
64+
);
3965
case 'b4a-more-icon':
40-
return <B4aMoreIcon />;
66+
return (
67+
<Suspense fallback={null}>
68+
<LazyB4aMoreIcon />
69+
</Suspense>
70+
);
4171
case 'b4a-app-overview-icon':
42-
return <B4aOverviewIcon />;
72+
return (
73+
<Suspense fallback={null}>
74+
<LazyB4aOverviewIcon />
75+
</Suspense>
76+
);
4377
case 'b4a-app-settings-icon':
44-
return <B4aAppSettingsIcon />;
78+
return (
79+
<Suspense fallback={null}>
80+
<LazyB4aAppSettingsIcon />
81+
</Suspense>
82+
);
4583
default:
4684
return null;
4785
}
@@ -53,6 +91,17 @@ const B4aSidebarSection = ({ active, children, name, link, icon, style, primaryB
5391
const [position, setPosition] = useState(null);
5492
const subSectionRef = useRef();
5593

94+
// Preload all icons on mount
95+
useEffect(() => {
96+
LazyB4aApiIcon.preload();
97+
LazyB4aDatabaseIcon.preload();
98+
LazyB4aCloudCodeIcon.preload();
99+
LazyB4aWebDeploymentIcon.preload();
100+
LazyB4aMoreIcon.preload();
101+
LazyB4aOverviewIcon.preload();
102+
LazyB4aAppSettingsIcon.preload();
103+
}, []);
104+
56105
useEffect(() => {
57106
if (showPopoverSection) {
58107
const node = subSectionRef.current;

0 commit comments

Comments
 (0)