How to remove unused JavaScript - step by step advice.

Hi,

Could somebody guide me how can I remove unused JavaScript?

I have managed to identify coverage in the developer console and I would like to remove a few of unused JS from the code.

Could you please advise if this is to be done in the theme-> edit code-> and where to go next? Screenshots would be appreciated, as I am not really familiar with the technical terminology.

Thank you!

Maya

@Maya91

Welcome to the community and thanks for asking this question. I would begin with discouraging you to remove any code unless its too much of overhead or affecting the performance. This will get overwritten with new theme updates and you are likely to break something if you aren’t careful.

However if you do want to do this, you can find most of JavaScript code under the assets directory when you click edit code. Name is usually theme.js.liquid but there could be more files and have different names depending on theme and customization made already.

@Propero thanks for your response!

Yes, this is for performance improvement as my website is really scoring low recently (otherwise I would not attempt to play with the fire! :).

Can I still try to remove the code from the copy of the theme template? If I mess something up will it cover my back? - reassurance question.

I would still say stay away unless you find js files ( entire file) which are not referenced at all in the liquid file. To get real coverage you need better tests and still, you don’t know how the user will behave with your front end.

If you have a complete understanding of entire theme code, you can re-assure yourself saying you know what you are doing and you can always restore from back-up :slight_smile:

Hi, we had exactly the same problem and improved our own store by stripping the content for header and removing unnecessary javascript loaded by apps depending on the page (content, cart, homepage, product…) As we noticed that quite a few people have that problem, we created an app doing that for you https://apps.shopify.com/pagespeed-javascript