Magento 2 Static Content Deploy – A Complete Guide
The tutorial includes the stepwise method to deploy static content in Magento 2.
Trace the static view files in the static directory of your Magento 2 store at your Magento install dir>/pub/static. Some files are also cached in the Magento install dir>/var/view_preprocessed directory.
One needs to write static view files manually to the file system of the Magento 2 store with CLI command. After running the CLI command, restrict permissions to limit vulnerabilities as well as for the prevention of malicious overwriting of files.
Note: It is required to clean the old files of CSS, Javascript, layouts, etc to make sure that static files run smoothly on enabling a new module in the developer mode.
Clean the generated static view files with the below steps:
Manual method:
Clear all the files under pub/static directory except .htaccess file with the below command:
find . -depth -name .htaccess -prune -o -delete
Automated method:
- Login to Admin panel
- Go to Systems > Tools > Cache Management
- Click the Flush Static Files Cache.
Method for Magento 2 Static Content Deploy via Command Line:
Navigate to Magento admin root folder. Show usages:
1 |
php bin/magento setup:static-content:deploy -f |
Usage:
1 |
setup:static-content:deploy [options] [--] [<languages>]... |
Arguments:
|
||
---|---|---|
Options | Description | |
-f | —force | Deploy files in any mode |
-s | –strategy[=STRATEGY] | Deploy files using specified strategy. |
-a | –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 messages |
-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 |
Simply run the below command to deploy static content in your store when you install an extension
1 |
php bin/magento setup:static-content:deploy -f |
That was all about the Magento 2 Static Content Deploy!
Follow the guide that’ll help to boost the speed of your Magento 2 store!
Please feel free to mention any doubts in the tutorial in the Comments section below. I’d be happy to help 🙂
Also, a reminder to rate the post with 5 stars!
Thank you
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
4 Comments
Hey,
This article is very helpful.
When the site is in production then we run the “PHP bin/magento setup:upgrade –keep-generated” command to avoid the site down.
Currently, I use the “PHP bin/magento setup:static-content:deploy” to deploy theme. The Site goes down, until the command don’t run completely
Can you please let me know, what command needs to use for static content deployment?
Hello Mohit,
With the bin/magento setup:upgrade command, pub/static directory will get deleted and again generated due to which the site will go down.
As the site will be in production mode, pub/static directory and var/view_preprocessed will also get regenrated due to which until the static-deploy command completes, the site won’t load properly.
If you have any caching mechanism like Cloudflare, the site won’t go down.
Thank you.
What is the correct syntax to exclude multiple themes? I’ve tried several variations and have not had any success.
Hey,
Please check the following:
php bin/magento setup:static-content:deploy --exclude-theme Magento/luma --exclude-theme Vendor/Yourtheme -f
Thank you.