In this post, I am going to show how to do Magento 2 Static Content Deploy.
What is Static View?
Static View is made of two terms.
Static refers to the caching of the static data for the site. If the file is not dynamically generated, then the file is static. It includes images and CSS generated from LESS.
On the other hand, “View” is the presentation layer of the Model-View-behaviour.
The deployment commands of Static View files allows you to write static content file for the Magento sites while your Magento 2 store is into production mode.
This step by step guide will help you to deploy Magento 2 Static Content.
Static view files are located in the <magento_root>/pub/static directory, and some are cached in the <magento_root>/var/view_preprocessed directory as well.
Magento 2 Static Content Deploy and Magento Modes
There are three Magento Modes:
The deployment of Static View files is affected by the Magento modes.
- Default and Developer Mode: Magento generates the Static View on-demand. However, the rest are cached in a file for quick access.
- Production Mode: Static files are not generated or cached
You can find the mode of your Magento store by quickly running the following commands in the root directory:
php bin/magento deploy:mode:show
You have to write static view files manually to the file system of your Magento 2 store by using the CLI command. After running the CLI command, you can restrict permissions to limit vulnerabilities and for the prevention of malicious overwriting of files.
Cleaning the Old files
There are two ways to this.
To clean the generated static View, follow these steps:
Delete all the files under the pub/static directory but leave the .htaccess file.
You can do this with the following command.
find . -depth -name .htaccess -prune -o -delete
The automated way to flush the cache is:
- Open the admin panel of your Magento 2 store.
- Navigate to SYSTEM → Tools → Cache Management
- Click on the Flush Static Files Cache.
Magento 2 Deploy Static Content Using CLI
Follow the below steps to configure and deploy Magento 2 Static Content via CLI.
- Login to Magento 2 server as the owner of the file system
- Delete the files under the pub/static directory, but leave the .htaccess file.
- Now run the code to enable static view files deployment.
php bin/magento setup:static-content:deploy -f
Note: Do ensure that the pub/static directory system is writable when you enable merging static view files in your Magento Admin
Deploy Static Content via command line
Check all the options by running the following command.
php bin/magento setup:static-content:deploy -h
If you follow the post, you can easily deploy Magento 2 static content on your store.
Still, if there is any doubt or problem, please leave them in the comment box.