admin管理员组文章数量:1314216
Javascripts which are used but google page speed insights shows it is not used. Is there anyway the i can remove it. Here i am sharing the screen shot of PageSpeedInsight report.
In the above screenshot u can see 8 js files were unused. But it is getting used on my app.
Javascripts which are used but google page speed insights shows it is not used. Is there anyway the i can remove it. Here i am sharing the screen shot of PageSpeedInsight report.
In the above screenshot u can see 8 js files were unused. But it is getting used on my app.
Share Improve this question edited Aug 18, 2020 at 8:01 SDK asked Aug 18, 2020 at 5:44 SDKSDK 1,5183 gold badges28 silver badges55 bronze badges 6- 1 I guess Google is telling you it's not needed for the start of the page? So you could decide to load it in only when it is used the first time. It's not saying it's pletely unused, it's saying some code in those files are unused. – Joel Harkes Commented Aug 18, 2020 at 5:48
- 2 i think its also telling you that you only use parts of the files. It suggests you to split the files and only import the chunks you actually need. – The Fool Commented Aug 18, 2020 at 5:49
- you can dynamically load javascript files. load the files on scroll. and after you append them to the head tag remove your event listener – Ilijanovic Commented Aug 18, 2020 at 6:14
-
"Is there anyway the i can remove it" - yes, simply edit your markup and remove the
<script>
tag that loads this file. Otherwise, if this is needed, just ignore the warnings. The main problem could be that you are loading multiple files from multiple domains – Nico Haase Commented Aug 18, 2020 at 6:23 - @DhanushKumarS Can you share more details on how you are loading the script files? So that the answers can be more specific. – Ashish Yadav Commented Aug 18, 2020 at 6:36
2 Answers
Reset to default 4NOTE: This answer is due to confusion. The OP is not using React but the report includes the React example. This might be helpful to others anyways.
If your ponents are loaded dynamically ( only after a user request for it ).
You can use React.lazy()
as suggested in the report for code splitting so that you don't load the large bundle when not necessary.
This solution is for non SSR.
BEFORE:
import ComponentB from './ComponentB';
function ComponentA() {
return (
<>
{/* After certain action probably like routes switch or any? */}
<ComponentB />
</>
);
}
AFTER:
import React, { lazy } from 'react';
const ComponentB = lazy(() => import("./ComponentB.js"));
function ComponentA() {
return (
<>
{/* After certain action probably like routes switch or any? */}
<ComponentB />
</>
);
}
Reference: https://reactjs/docs/code-splitting.html
You could load your script files on scroll. When the user starts to scroll down you append the script tag to your head and remove the event listener again.
Only add scripts that arent in the viewport at the beginning like recaptchas. The are usually somehwere at the bottom.
function dynamicLoad(url) {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = url;
document.getElementsByTagName("head")[0].appendChild(script);
}
window.addEventListener("scroll", loadScripts);
function loadScripts() {
//load here as many dynamic scripts as you want
dynamicLoad("recaptcha url");
dynamicLoad("facebook url");
//end ------
window.removeEventListener("scroll", loadScripts);
}
本文标签: Google PageSpeed Insights showing unused javascript but it is usedStack Overflow
版权声明:本文标题:Google PageSpeed Insights showing unused javascript but it is used - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741931215a2405584.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论