0

I'm using magento 2.1.2 in production mode with merge/minify/bundle enabled. It works good and the created files like /static/_cache/merged/6c3c8136610cacb3de381ea2afc99190.min.js has header expires setted to 1y. I think this setting is correct.

The problem is when new js changes are loaded. After flush all magento cache the new merged file contains new js changes correclty, but it has the same name as the old file, so the browser, due to the header expires, doesn't reload it and shows the old version.

To see the new changes i have to force the cache to clear with ctrl+F5.

I think a possible solution could be to give different name to merged file when they have different content or adopt a versioning system.

Do you know how to achieve this?

2 Answers 2

4

You can Enable the Sign static files option in system configuration and redeploy static files : Store=>Configuration=>Advanced=>Developer=>Static file signing settings to yes

Make sure that you have correctly set up your webserver.

You can based on the nginx sample configuration or on the htaccess.

Make sure you have this kind of lines :

Nginx:

location /static/ {
    # Uncomment the following line in production mode
    # expires max;

    # Remove signature of the static files that is used to overcome the browser cache
    location ~ ^/static/version {
        rewrite ^/static/(version\d*/)?(.*)$ /static/$2 last;
    }

    location ~* \.(ico|jpg|jpeg|png|gif|svg|js|css|swf|eot|ttf|otf|woff|woff2)$ {
        add_header Cache-Control "public";
        add_header X-Frame-Options "SAMEORIGIN";
        expires +1y;

        if (!-f $request_filename) {
            rewrite ^/static/(version\d*/)?(.*)$ /static.php?resource=$2 last;
        }
    }
    location ~* \.(zip|gz|gzip|bz2|csv|xml)$ {
        add_header Cache-Control "no-store";
        add_header X-Frame-Options "SAMEORIGIN";
        expires    off;

        if (!-f $request_filename) {
           rewrite ^/static/(version\d*/)?(.*)$ /static.php?resource=$2 last;
        }
    }
    if (!-f $request_filename) {
        rewrite ^/static/(version\d*/)?(.*)$ /static.php?resource=$2 last;
    }
    add_header X-Frame-Options "SAMEORIGIN";
}

Apache : pub/static/.htaccess

<IfModule mod_rewrite.c>
    RewriteEngine On

    # 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]
</IfModule>
6
  • Like the comment to the other answer, have i to create manually the versionXXXX folder?
    – LucScu
    Nov 16, 2016 at 7:41
  • No you do not have to create the folder versionXXXX. Your webserver should make an internal redirection to the correct resource thanks to the configuration below. Do you use Apache or Nginx ? Nov 16, 2016 at 8:58
  • i'm using nginx. I see the line in conf file for the redirection, but browser gives 404! Note that I'm using magento 2.1.2 in production mode. Any hint?
    – LucScu
    Nov 16, 2016 at 10:25
  • Can you check that the require file without versionXXX exists in the file system ? Nov 16, 2016 at 15:13
  • Also are we supposed to uncomment expires max in production mode as it says in the beginning of the file. # expires max;
    – Zoya
    Dec 10, 2019 at 16:45
1

Enable Sign static files option in system configuration and redeploy static files.

1
  • enabled. Frontend require /static/versionXXXXX/ files but it returns 404 beacuse in file system no versionXXXXXX folder is created
    – LucScu
    Nov 15, 2016 at 10:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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