I tested my site in Google PageSpeed Insights, get score 96 for desktop and 56 for mobile, one of the opportunities is to remove unused javascript. My site is respective so javascripts are for both desktop and mobile. I want to know how to optimize theses javascripts?
Asked
Active
Viewed 3,254 times
1
-
If you are looking for an automated tool, there isn't. You will have to manually add a check of device and then load files manually. If you are using package builder, look for chunks and only load necessary chucks on respective device – Rajesh Jun 15 '20 at 03:53
-
Does this answer your question? [How to Remove Unused javascript From Website](https://stackoverflow.com/questions/41478936/how-to-remove-unused-javascript-from-website) – Don't Panic Jun 15 '20 at 03:57
-
1Also https://stackoverflow.com/questions/49381864/remove-unused-javascript-code-based-on-coverage-report, https://stackoverflow.com/questions/28497156/optimization-is-there-any-way-to-find-and-remove-unused-css-and-javascript-comm, ... and many others. – Don't Panic Jun 15 '20 at 03:58
-
@Rajesh I know what you meaning, for example, a js file is unused for mobile but is used for desktop, for respective page both mobile and desktop use the same HTML file, I don't know how to include a js file for the different platform in the same HTML file. – Mil0R3 Jun 15 '20 at 04:17
-
@Mil0R3 In the scenario of a script being used on desktop but not on mobile just grab the screen width and inject the script if the screen size is appropriate, you can do this with https://stackoverflow.com/a/11744120/2702894 to get the screen width and then https://stackoverflow.com/a/22534608/2702894 within an if statement. This would be an inline script within your HTML. – GrahamTheDev Jun 15 '20 at 06:44