Magento – magento 2 : Flush static content from admin panel forces to run static:deploy command everytime

cachemagento2PHP

After flushing the static cache files from the System -> Tools -> Cache Management -> Flush Static Files Cache from the admin panel…

enter image description here

magento admin panel is looking like below…

enter image description here

and for the solution i have to run the php bin/magento setup:static-content:deploy
command everytime.

When i installed Magento 2, static files were generated automatically and not by running the command.

What i need to do so that it will start generating the static files automatically after i have flushed them from backend? Thanks in advance…

Best Answer

After flushing the static files you have three options in terms of regenerating them:

1) Compile using grunt (Or an alternative JS based less compiler) when working locally on a theme etc

2) Deploy the theme using magento's native setup:static:content-deploy command

3) Server/Client Side compilation

To do option 3, you have to ensure that your environment supports the generation of static files. You can find the setup for these in the nginx.conf.example (if using nginx) and the .htacces.sample (if using apache). For example, the nginx.conf.sample has the following section:

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";
}

This section essentially routes the request for a file through the pub/static.php which in turn generates the pub/static files with relevant symlinks attached to them (Missing this is most likely the reason yours in not being generated)

Related Topic