Debug Production Code Live With Local Overrides
How many times did you come across an issue that was causing you a great deal of headache because it was creating a nasty bug on production but was not reproducible locally?
All the necessary steps it takes to re-check whether a change locally does really solve the problem could be daunting and in fact, often unnecessary.
This is what I came across recently (again) and tried to figure out a way to debug my code on the real deal and verify what changes I need to make without having to commit, push, build and deploy then check the results again and again. This lead me to use Chrome DevToolās Local Overrides. Itās not a recent thing since itās been with Chrome since version 65.
First I started looking into proxies to rewrite network requests and replace the minified and obfuscated production js with a local file, but I didnāt really felt like using an additional software is the right way. So I looked into Chrome extensions, there has to be one. And there was some, but either they were overly complicated for the job or had terrible UI. And the fact of adding another extension to Chrome didnāt really help either.
So I looked into Chrome itself, because I remember editing files and seeing my changes. The only problem with this is that when you have a minified file, you canāt really read it. Luckily thereās a tool inside Chrome called āPretty printā, which does exactly that: formats the file to a readable format. The only problem with this, however, is that you canāt edit the pretty printed file.
We can easily get around this by copy-pasting the pretty printed file to the original one, so now nothing prevents us from editing production-ready code right in the browser. Only refreshes. See, if you edit the file, save it and refresh the page, everything is gone, it is not persisted through reloads. This is where local overrides come into place.
How Does it Work?
In simple terms, Chrome map your resources to local files, and whenever a network request is made, it intercepts them and loads the local resource instead of requesting them from the server.
How to Enable it?
First, open up your DevTools and go to your Sources tab. Here youāll see all of the source files loaded for a given page. Now change the āPageā tab to āOverridesā on the left-hand side. If itās not visible, just click on the arrows to open the dropdown.
Click on āSelect folder for overridesā ā this is where all your resources will live. Preferably, create one just for the purpose of using local overrides and point to that folder.
Now you will see a checkbox saying āEnable Local Overridesā which you need to check. A prompt will be shown at the top of your screen, click on allow to give DevTools read and write permission to the directory youāve just created.
Now if you go back to the āSourcesā tab, anytime you edit a resource, it gets saved to your local repository. So whenever you refresh the page, the changes are persisted.
Letās See a Live Example
The following error is coming on my page and the animation for my heart icon is just not seem to work at all.š
Looking at the source code of my site, itās hard to guess what actually goes wrong as itās minified into one single line:
Using the built-in prettifier doesnāt really help either as variables, function names, and everything else is obfuscated beyond recognition:
To get around this, I looked at the local code and rewritten the production function into a more readable format. After that, I got the following output which is a little bit more dev friendly:
I also inserted a breakpoint at the line in question that causes the error. We can see that indeed, the second parameter is not a function but a string, but as we know, addEventListener
expects a callback function as a second parameter, which is mistakenly passed as the first one. All we have to do is switch the two params around and now the beating animation works as expected:
As stated by the documentation, there are some limitations to it, but nothing really major and nothing that should prevent you from achieving what this post meant to demonstrate.
Limitations
- Anything that is edited in the DOM tree will get lost. You need to edit the HTML in the āSourcesā panel instead.
- If your style is coming from an HTML file and you edit the CSS in the Styles panel, it will get lost as well. Anything thatās in the HTML, you need to edit the HTML file in the āSourcesā panel instead.
Summary
This sums up the Local Overrides feature in Chrome. You can not only use it to speed up your development and see changes live by persisting them through page reloads, but it also helps you in cases where environment differences makes it difficult to debug a faulty function that wonāt reproduce the same issue locally, it does on a staging or production env. All thatās left to do, is to go ahead and fix those nasty bugs you were sitting on for days. šŗ
Rocket Launch Your Career
Speed up your learning progress with our mentorship program. Join as a mentee to unlock the full potential of Webtips and get a personalized learning experience by experts to master the following frontend technologies: