Magento 2 Static Content Deploy Guide in Details
In this blog, we will learn about setup:static-content:deploy command for deploying static content. All Magento 2 extensions are located in app folder, so there is no access to these folders from the browser, but modules have static content like CSS, js and images, so browsers cannot download these static files. So, static content deploy allows you to move the extension files to the pub/static folder to download in the browser.
Magento 2 static content deploy
Magento 2 Default mode and Developer mode automatically generate static files and cache the rest of the files.
In Magento 2 for Production mode, static files are not generated or cached automatically, so we manually run the following command:
php bin/magento setup:static-content:deploy |
Magento 2 rebuild static content
For rebuilding static content by running the following command:
php bin/magento setup:static-content:deploy |
This command run before deploying to production mode.
Magento 2 static content deploy not working
If static content deployed in Magento 2 is not working properly, then run the following commands:
php bin/magento setup:upgrade |
php bin/magento setup:static-content:deploy -f for Magento 2.2.x ,Magento 2.3.x ,Magento 2.4.x |
php bin/magento setup:static-content:deploy for Magento 2.1.x |
Magento 2 clear static content
For clear static content in Magento 2, there are three possible ways:
Using Admin Panel: go to System > Tools > Cache Management and select Flush Static Files Cache
Manually: you can clear the pub/static and var/view_preprocessed directories and subdirectories. !!! The exception is pub/static/.htaccess
Using Command line: run the command rm -rf pub/static/*.
Magento 2 static content deploy theme
For deploying specific themes, run the following command:
php bin/magento setup:static-content:deploy --theme Magento/backend --theme Vendor/mytheme |
Magento 2 static content deploy command line
If you are using Windows, then make sure you added PHP executable file to your Environment Path. After that, open up your Windows Command Prompt (cmd) and run below command:
cd path to your magento folder |
php bin/magento setup:static-content:deploy |
Magento 2 CSS not loading after static content deploy
If CSS is not loading after static content deploy, then run the following commands in the Magento 2 root folder:
php bin/magento setup:static-content:deploy |
php bin/magento indexer:reindex |
php bin/magento cache:flush |
php bin/magento cache:clean |
Magento 2 static content deploy strategy
Standard:
In the Standard strategy, all static view files for all packages are deployed and processed by \Magento\Framework\App\View\Asset\Publisher.
Quick:
In quick strategy, many files are duplicated in this method, the time required for deployment for multiple locales is reduced.
Compact:
Compact strategy avoids file duplication and this strategy minimizes the space taken by the published view files.
The files are deployed according to the following patterns:
Pattern | Description |
<area>/<theme>/<locale> |
Deployed files are specific to a 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. |
Conclusion:
Using this blog we learn about how to use static deploy file and use of setup:static-content:deploy command.In case any query regrading above commands then you can contact us or let us know in comment section.