Redirect requests for specific JS/CSS files to your local versions for development.
I sometimes find myself needing to test my local development JS/CSS files on staging/production without actually comitting the file and making a deployment. This can be because staging/production has specific content that you can't replicate locally, or if one or more of your front-end components need to execute on the correct domain.
So I (cough ChatGPT) wrote this script that replaces the URLs in the <script> and tags that you specify. More than that, it can also redirect requests for specific endpoints to your local development server for further testing flexibility.
To use this script, you need to install Tampermonkey (or equivalent for your browser). Then:
- Add the script to Tampermonkey (you can just copy paste the script)
- Update the @namespace and @match configuration to match the domain where you want the script to be active
- Update the
config
object. The key is the URL you want to replace and the value is the URL you want to replace it with. - Save the script
- Enable it in Tampermonkey
CORS and CSP settings on your site may block your local scripts from running on your page. You may need to use additional browser extensions to temporarily disable CORS or CSP.