7

When I want to force reload CSS and JS on my sites (non-Magento 2), I usually just add a query string to the end of the URLs. For example: css/styles.css?v=1

I tried this in M2 by adding adding the above to my style files in my custom default_head_blocks.xml Although the style files are being loaded (I can click on the links in 'View Source' and it shows me the correct CSS file content) but the page isn't being styled.

Any idea why it's not working? Or, do you know how to force reload CSS / JS on the client side in M2?

2

Magento 2 has this ability built-in called "Sign Static Files". It doesn't use the query string, but uses a generated subfolder starting with "version....", but basically the same idea. You can find it here... Stores > Configuration > Advanced > Developer > Static Files Settings > Sign Static Files

Here's more info on Using Static File Signatures.


Side note for anyone here using Magento 1... for Magento 1 I have used the Queryfier extension by Bubble Code and it does a great job (it uses the query string method).

2
  • This option is only available in developer mode. What is the flow for production mode? Sep 17 '20 at 14:39
  • 1
    These settings are available via command line using config:set since they need to be set prior to the code being built in Production mode. You can run bin/magento config:set dev/static/sign 1 to enable this. More details available at... devdocs.magento.com/guides/v2.4/config-guide/cache/… Sep 18 '20 at 19:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.