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:
- Default
- Developer
- Production
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
Now, if your store is running on developer mode, you will have to clean the old files of CSS, JavaScript, Layouts, Images, etc…
There are two ways to this.
To clean the generated static View, follow these steps:
Manual Way
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
Automated Way
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
Output:
Usage:
setup:static-content:deploy [options] [--] [<languages>]...
Arguments:
languages Space-separated list of ISO-636 language codes for which to output static view files.
Options:
-f, --force Deploy files in any mode.
-s, --strategy[=STRATEGY] Deploy files using specified strategy. [default: "quick"]
-a, --area[=AREA] Generate files only for the specified areas. [default: ["all"]] (multiple values allowed)
--exclude-area[=EXCLUDE-AREA] Do not generate files for the specified areas. [default: ["none"]] (multiple values allowed)
-t, --theme[=THEME] Generate static view files for only the specified themes. [default: ["all"]] (multiple values allowed)
--exclude-theme[=EXCLUDE-THEME] Do not generate files for the specified themes. [default: ["none"]] (multiple values allowed)
-l, --language[=LANGUAGE] Generate files only for the specified languages. [default: ["all"]] (multiple values allowed)
--exclude-language[=EXCLUDE-LANGUAGE] Do not generate files for the specified languages. [default: ["none"]] (multiple values allowed)
-j, --jobs[=JOBS] Enable parallel processing using the specified number of jobs. [default: 0]
--symlink-locale Create symlinks for the files of those locales, which are passed for deployment, but have no customizations.
--content-version=CONTENT-VERSION Custom version of static content can be used if running deployment on multiple nodes to ensure that static content version is identical and caching works properly.
--refresh-content-version-only Refreshing the version of static content only can be used to refresh static content in browser cache and CDN cache.
--no-javascript Do not deploy JavaScript files.
--no-css Do not deploy CSS files.
--no-less Do not deploy LESS files.
--no-images Do not deploy images.
--no-fonts Do not deploy font files.
--no-html Do not deploy HTML files.
--no-misc Do not deploy files of other types (.md, .jbf, .csv, etc.).
--no-html-minify Do not minify HTML files.
-h, --help Display this help message
-q, --quiet Do not output any message
-V, --version Display this application version
--ansi Force ANSI output
--no-ansi Disable ANSI output
-n, --no-interaction Do not ask any interactive question
-v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
Help:
Deploys static view files
Also Read
How to Install Magento 2.4 with Command Line?
How to Change Magento 2 Base URL?
Magento 2 Commands: A Useful list for Magento Developers
Magento 2 Requirements that You Must Take care of
Final Words
Speed plays an important factor in the success of eCommerce store. You can boost the speed of the Magento 2 store by deploying the static content.
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.