Deploying Static Content for Magento 2

NB! This article is incomplete and is being worked on, if you have questions regarding this – contact us on the chat.

Configuration of static files

Signing of static files should be enabled. This allows you to deploy changes in the theme, without needing to flush down stream caches. 

If you are having trouble with either JavaScript and CSS not updating, or not loading at all after a new deploy of static files (magento setup:static-content:deploy), this is most likely your issue.

To check your current settings, Magento 2 must be set to developer mode first. You do this by running the following command:

$ magento deploy:mode:set developer

When Magento 2 is set to developer mode, in the Magento 2 admin panel, go to:

Admin > Stores > Configuration > Advanced > Developer > Static Files Settings

The setting for Sign Static Files should be set to “yes”. 

This means that Magento 2 will generate a unique URL when static assets are re-deployed (magento setup:static-content:deploy ). This again, makes sure that the HTML will contain unique URLs for all static resources. this will bypass the previously cached versions in down stream caches (proxies, CDNs or the browser).

Make sure to set Magento into production mode again afterwards:

$ magento deploy:mode:set production

Pre-requisites for static signing to work

For static signing to work, the webserver must first know how to treat the signed URLs. It does this with the pub/static/.htaccess file. This file should contain the following code:

# Servebolt version of pub/static/.htaccess
<IfModule mod_php7.c>    
php_flag engine 0
# To avoid situation when web server automatically adds extension to pathOptions -MultiViews
<IfModule mod_rewrite.c>    
RewriteEngine On    
## you can put here your pub/static folder path relative to web root    
#RewriteBase /magento/pub/static/    
# Remove signature of the static files that is used to overcome the browser cache    RewriteRule ^version.+?/(.+)$ $1 [L]    
RewriteCond %{REQUEST_FILENAME} !-f    
RewriteCond %{REQUEST_FILENAME} !-l    
RewriteRule .* ../static.php?resource=$0 [L]   
# Detects if moxieplayer request with uri params and redirects to uri without params    <Files moxieplayer.swf>     	
RewriteCond %{QUERY_STRING} !^$     	
RewriteRule ^(.*)$ %{REQUEST_URI}? [R=301,L]     

# Removed Magento 2 default bundled settings that are set by nginx or otherwise