I'm working on a React webpage using the Gatsby framework and I'm doing continuous deployment using AWS while I get a good page speed value.
One of the improvement opportunities is Remove unused Javascript and it explains this:
Sorry, follow this link. I don't have 10 of reputation to post images :(
Sorry, follow this link. I don't have 10 of reputation to post images :( x2
This is the code where I'm creating the map (using @react-google-maps/api
):
useEffect(() => {
const loadScriptByURL = (id, url, callback) => {
const isScriptExist = document.getElementById(id);
if (!isScriptExist) {
let script = document.createElement("script");
script.type = "text/javascript";
script.src = url;
script.id = id;
script.onload = function () {
if (callback) callback();
};
document.body.appendChild(script);
}
if (isScriptExist && callback) callback();
}
loadScriptByURL("recaptcha-key", `https://www.google.com/recaptcha/api.js?render=${SITE_KEY}`, function () {
console.log("Script loaded!");
});
}, []);
This is the code where I'm calling recaptcha:
<LoadScript
googleMapsApiKey='...'>
<GoogleMap
mapContainerClassName={styles.mapSection}
center={defaultProps.center}
zoom={defaultProps.zoom}
onUnmount={() => {}}
onLoad={() => {}}>
<Marker position={markerLocation} onClick={infoToggleOpen}>
...
</Marker>
</GoogleMap>
</LoadScript>
What's the most proper way (or the easiest one) to solve the Remove unused Javascript error for these plugins? How can I use React.lazy()
or loadable-components
(because I tried and it doesn't work for me) to solve this?