-3

I’m trying to improve my website performance and Google PageSpeed Insights shows that many JavaScript and CSS files are unused or partially unused.

I want to remove or disable these unnecessary files, but I’m not sure what the correct or safest method is.

My setup:

  • Platform: PHP

  • Issue: Unused JS/CSS increasing load time and negatively affecting performance scores

What I want to achieve:

  • Remove or stop loading unused JS/CSS

  • Load only required assets on specific pages

  • Avoid breaking theme functionality

What I have tried:

  • Checking PageSpeed “Remove unused CSS/JS” section

  • Tried disabling some files, but some parts of the site break

  • Searched documentation but did not find a clear solution

New contributor
karan kashyap is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
10
  • 3
    Set up a testing environment that accurately replicates the live one as far as possible. Try removing the files from that. Then regression test any areas that seem likely to be affected - or the whole site, if you're unsure. If you're happy with the results, make the same changes on the live site (and then smoke-test that, as well). If you're not happy, go back and revise your changes. (Remember to use source control, or at least back up your files, so you can revert changes easily). Commented yesterday
  • 1
    But... what I've just described is simply the standard development procedure for any changes to an application...why do you think this case is different/special such that you need to ask about it separately? Commented yesterday
  • 2
    Also bear in mind that while the insights in something like PageSpeed may be helpful, they are not necessarily 100% accurate and it might not know everything about your site. Maybe some things are unused in one area, but used in another. So you maybe need to consider removing references to that JS/CSS (e.g. links to JS/CSS files found in HTML docs) in places where it's not needed, rather than entirely removing the files themselves. Just a thought. Of course without a minimal reproducible example of the scenario we can only give you this extremely, standard, generic advice. Commented yesterday
  • 2
    To add to the above... If for example your application has code which some users will use but others will not, then analysis tools may not be exercising all of the app's functionality to observe all of what gets used. As an example, consider a SPA which includes functionality for admin users. Most users will never use that functionality. In cases like that you might look into terminology like "code splitting", which seeks to lazy-load uncommonly used modules only when they're invoked, rather than when the page initially loads. Commented yesterday
  • 1
    Why closing this. Close 95% other questions then. SO is dead Commented 21 hours ago

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.