Magento 2 static content deploy is the Magento feature that helps to keep the user experience at the top levels. Why? It ensures your store pages are built correctly, static view files are downloaded by the browser, and static content is displayed according to user requests.
In this comprehensive guide, you will find everything you need to know about static content deploy in Magento 2: what it is, reasons to use it, how to deploy static content in Magento 2 in three different ways and how to reduce deployment time to zero.
Moreover, we’ll cover the static content deployment strategies and examples to help you comprehend the topic better.
Post Contents [hide]
- Static View Files Deployment Overview
- Why Use Static Content Deploy in Magento 2?
- Static Content Deployment Strategies
- How to Run Static Content Deploy in Magento 2?
- Examples of Magento Static Content Deploy
- Troubleshooting the Static View Files Deployment
- Reduce Downtime during Static Content Deploy
Regardless if you’re just starting with Magento development or if you’ve got some knowledge already, this article will help to hone your development skills or discover something new about the static content deployment in Magento.
We’ve got a lot to cover, but let’s start with the basics first.
Static View Files Deployment Overview
The static view file deployment allows you to write static files into the Magento system when setting Magento for production mode.
Static part refers to the files that can be cached if not generated dynamically and view — to the Model-View-Controller layer.
You can find the static view files in the /pub/static (contains js, css, html and others) or /var/view_preprocessed directory (contains phtml files).
It is also important to note that the deployment of static view files is determined by the Magento modes:
- In default and developer mode Magento generates static files on demand and caches the rest of the files.
- In production static files are not generated or cached, you have to write static view files manually by the command. We’ll talk about it later on.
Why Use Static Content Deploy in Magento 2?
Though static content deploy in Magento 2 is used by developers only, a lot of non-tech people still need to know why it is used and how it contributes to the user experience.
Static Content Availability
All Magento 2 extensions are located in the app or vendor folder. This is how the module file structure works in Magento. Correspondingly there is no access to these folders from the browser.
So, static content deploy allows you to move the extension files to the pub/static folder to make them available for download in the browser.
When Magento moves the files to the pub/static folder it checks whether the files were overridden in your theme or child theme.
Browsers can read only CSS while most styles are written with the LESS language. So for the browser to read these styles you need to compile them to CSS. This is another reason to use static content deploy to make LESS comprehensible to the browsers.
If you have HTML minification enabled in the admin, static content deploy in Magento will prepare the minified files and move them into the var/view_preprocessed folder.
So, if you make any changes in templates, JS, CSS, images, or install Magento 2 extensions, you have to use Magento static content deploy to make those changes available for the browser to download and display to the users.
Static Content Deployment Strategies
Regardless of the purpose, you use static content deploy for, Magento 2 allows you to incorporate different static content deploy strategies. They provide different deployment results and are used in different use cases.
Let’s review those cases and cover each of the strategies.
This static content deployment strategy is regular and most recognized. In the case of standard strategy, all static files for all packages are deployed and processed by \Magento\Framework\App\View\Asset\Publisher.
Also, all files for an arbitrary locale are deployed in each theme.
The quick strategy works the same way as a standard one. However, in all other locales in a theme, except for the arbitrary locale, files that override a deployed theme are defined and deployed.
The other files are copied from the deployed locale since they are considered similar (independent of the locale, area, or theme) for all locales.
Quick strategy name speaks for itself. Though a lot of files are duplicated in this method, the time required for deployment for multiple locales is reduced.
Unlike in quick strategy, in a compact one files are inherited from the base themes and locales which means they are stored in the base subdirectories. This avoids file duplication and allows you to store these reactions in the map files for each theme, locale, and area combination.
Then, the files are deployed to the subdirectories based on the following patterns:
|<area>/<theme>/<locale>||Deployed files are specific for certain area, theme, and locale.|
|<area>/Magento/base/<locale>||Deployed files are specific for certain areas and locales, but similar for all themes.|
|<area>/<theme>/default||Deployed files are similar for all locales of a certain theme of a particular area.|
|<area>/Magento/base/default||Deployed files are specific for a certain area, but similar for all locales and themes.|
|base/Magento/base/<locale>||Deployed files are specific for a particular locale, but similar for all themes and areas.|
|base/Magento/base/default||Deployed files are similar for all themes, areas, and locales.|
How to Run Static Content Deploy in Magento 2?
Once you choose what static content deployment strategy to utilize in your store development, you need to explore the way you can do this.
As we’ve already mentioned, you can find static view files in /pub/static or dir/var/view_preprocessed directories. However, some files need to be written to Magento 2 file system manually.
In developer mode, you need to clean the old CSS, JS, layout files to avoid static files issues and make sure all new files will run correctly in a new module.
There are a few possible ways how you can clean the old files in Magento 2.
To deploy static view files in Magento 2 manually:
- Log in to your Magento server as the file system owner.
- Delete all files in /pub/static folder. Don’t delete the .htaccess file.
- Run the static content deployment command:
Note: pub/static directory must be writable, if you enable merging of the static view files in the admin. If the pub/static is not writable, the static-content:deploy won’t run.
Pro tip: we recommend to delete only the following files from the pub/static folder:
Magento also allows you to clear the generated static view files from the admin panel. You just need to go to the Systems > Tools > Cache Management and click the Flush Static Files Cache button.
Via CLI Command
Running static content deploy via CLI command is the most common way of cleaning static view files in Magento 2.
Generally, the CLI command used for static content deploy is
However, it can depend on the different parameters you need to use. None of them is obligatory, but we’ll review all of them to provide you a better understanding of how you can vary the static content deploy in Magento.
||-- language||Generates files for languages you specify. By default, static content deploy generates files for all languages.|
|--exclude-language||Generates files for all languages, except excluded language.|
||--theme <theme>||Generates files for specified themes. By default, files are generated for all themes.|
|--exclude-theme <theme>||Generates files for all themes, except excluded themes.|
|-a||--area||Generates files for all specified areas (adminhtml or frontend). By default, files are generated for all areas.|
|--exclude-area||Generates files for all areas, except for excluded ones.|
||--jobs||Enables parallel processing with the specified number of jobs. The default value is 0 which doesn't run the parallel processing.|
|--symlink-locale||Creates symlinks for the locales that have no customizations and are passed for development.|
|--content-version=CONTENT-VERSION||Uses the custom version of static content to ensure the version of static content is identical and caching works well when deploying on multiple nodes.|
|--no-css||Doesn't deploy CSS files.|
|--no-less||Doesn't deploy LESS files.|
|--no-images||Doesn't deploy images.|
|--no-fonts||Doesn't deploy font files.|
|--no-html||Doesn't deploy HTML files.|
|--no-misc||Doesn't deploy other file types (.md, .jbf, .csv, .json, .txt, .htc, .swf).|
|--no-html-minify||Doesn't minify HTML files.|
|- s||- s quick||Uses quick deployment strategy. This is a default option if nothing else is selected.|
|- s standard||Uses standard deployment strategy to deploy all files in all packages.|
|- s compact||Uses compact deployment strategy.|
|--no-parent||--no-parent||Doesn't generate files for the parent or current themes. It is recommended to use this parameter if you don't use your parent or current theme explicitly to increase the time of the process.|
|-f||-- force||Deploys files in any mode. By default only works in production mode.|
That’s quite a list. You obviously won’t use all parameters in one command. However, there are different use cases you might want to explore.
Examples of Magento Static Content Deploy
Now that you know why static content deploy is used, what strategies are there, let's delve into the development and check the examples of how you can run static content deploy commands.
Magento 2 static content deploy specific theme
When you run static content deploy by default it deploys all files on all themes which takes time. To run it only for a specific theme, use the following parameters:
bin/magento setup:static-content:deploy --theme="ThemeVendor/themename"
Or if you want to exclude a specific theme from deployment:
bin/magento setup:static-content:deploy --exclude-theme="ThemeVendor/themename"
Static content deploy in developer and default mode
Pro tip: If you only use static content deploy to install, update or delete Magento 2 extensions, you can simply run:
php bin/magento setup:static-content:deploy -f
Magento 2 deploy static content language
There can appear different issues with the static content deployment. So if you use different languages, we recommend you to run it for specified languages.
bin/magento setup:static-content:deploy de_DE
Static content deploy in Magento 2 is very flexible, so you can combine different parameters when deploying static view files to the Magento file system.
For example, you can run deployment in default or development mode for a specific theme and locale.
bin/magento setup:static-content:deploy -d --theme="ThemeVendor/themename" de_DE
Besides, you can exclude all fonts and CSS from deployment on a specific locale.
bin/magento setup:static-content:deploy de_DE --no-css --no-fonts
There are multiple other parameters combinations you can use depending on your case. You'll definitely get to use some of them.
Troubleshooting the Static View Files Deployment
The most common issues with static content deploy are connected with the permissions and users that are granted the right to run the deployment commands. One of them is bash: bin/magento: Permission denied.
All you need to do in this case is just review user permissions and make sure you run the commands from the Magento root directory. Very often the system will provide the instruction on fixing the permission issue if there appear any.
However, there is one more issue. It is the following:
ERROR: You need to install the Magento application before running this utility.
To fix it you should:
- Install Magento using CLI or setup wizard.
- Log in to the Magento server as a file system owner.
- Delete all files from the pub/static directory, except for the .htaccess file.
- Run static content deployment command:
Reduce Downtime during Static Content Deploy
Usually, the system requires some time to deploy static content to the Magento system. Very often deployment time is associated with website downtime.
Since you already know there are different deployment strategies. Some allow you to run deployment faster than others, but you can't avoid downtime completely which is bad in terms of customer experience.
So, you should consider more effective ways of running static content deploy in Magento 2. Review these zero downtime deployment commands to avoid downtime and provide an uninterrupted experience for your customers.