Magento 2 Static Content Deployment

If you have been into the Magento 2 development for a while you would have definitely executed the magento 2 command in order to deploy the static content.

Magento 2 Static Content Deployment

Today we are going to look into more details on static view content deployment and how to do it. Static content deployment includes both the static files as well as the view files. Here the “static” refers to all the data like images, the CSS files that are generated via the compilation of LESS files and the “view” refers to the view part in MVC concept which is the presentation layer.

The execution of the static content deployment would generate both the static as well as the view files required for Magento 2. You would usually need to execute this command while switching the store to production mode or whenever you make changes to the coding like CSS or Javascript.

The static files in Magento 2 are stored in the /pub/static folder. If you take a look at this folder, it will contain a version number saved in the deployed_version.txt file and 2 folders admin HTML and frontend which would contain all the generated static content of both the website and admin panel respectively. The var/view_preprocessed folder would also have some of the cached view files.

Clearing Static Files Cache:

There are 2 ways to clear the static files cache. One is via the Magento Admin Panel and the other one is directly from the folder or through command line interface.

     1. Login to Magento admin panel.

     2. Navigate to System -> Tools -> Cache Management and click on the Flush Static Files Cache button.

In the case of Windows OS, to clear the files manually you can just navigate to <your magento2 dir>/pub/static directory and delete the files except the .htaccess file.

Do the same process in linux also. cd to /pub/static directory and delete all the files except the .htaccess file.

Deploying Magento 2 Static Content via CLI:

     1. Login to your Magento 2 server.

     2. Navigate to your Magento 2 installation directory and remove the contents of /pub/static/ folder except the .htaccess file.

     3. Execute the following command:     

               a. php bin/magento setup:static-content:deploy -f

    4. In some servers, you might face some issues that your webpage might not display properly even after executing the static content deploy when working with multiple languages. In that case you would need to deploy the specific language static file and you can do it in the following way:

               a. php bin/magento setup:static-content:deploy –f en_GB

     5. In the above command, en_GB is your language pack. You can modify it to the language pack that you are using.

Write Your Comment

Only registered users can write comments. Please, log in or register