Deploying static content in Magento 2 is a required step to make sure all CSS, JavaScript, and media files load correctly on the storefront.
These files don't change dynamically for each visitor. Instead, Magento generates them once and stores them in a publicly accessible directory. Serving prebuilt files reduces server processing, speeds up page loading, and provides a better shopping experience for customers.
What is Static Content in Magento?
Static content in Magento is a collection of prebuilt frontend files that don't change per request and can be cached and reused.
Magento Static Сontent files generally include:
- CSS files (fonts, colors, layout, styling)
- HTML templates
- JavaScript files (sliders, menus, popups, interactions)
- images (logos, icons, banners)
- translation files
- theme resources and libraries
During deployment, Magento collects these files from modules, themes, and libraries, processes and compiles them, and copies the final version into the /pub/static directory.
After deployment, the static content loads for visitors, improving page performance.
When to Deploy Static Content in Magento?
Static content should be deployed whenever any changes are made to modules, themes, or design assets, for example:
- when installing new extensions
- when updating modules
- when switching or customizing themes
- when changing styles
- when deploying to a live server
- when enabling production mode
- when adding a language or store view
Note: Static content deployment is only required in production Magento mode. In developer mode, Magento generates static files automatically.
How to Deploy Static Content?
To deploy static content in Magento 2:
Step 1: Log in or switch to your Magento 2 file system owner.
Step 2: Switch to the production mode:
php bin/magento deploy:mode:set production
Step 3: Clear static files. There are three ways to do that:
- by running the following CLI command: rm -rf pub/static/* var/view_preprocessed/*
- by going to Systems > Tools > Cache Management in the admin panel and clicking the Flush Static Files Cache button.
- manually deleting the pub/static and var/view_preprocessed directories and subdirectories.
Note: be careful not to delete the .htaccess file from this folder, as the site may lose all styling and JS functionality.
Step 4: Run the standard static content deployment command:
php bin/magento setup:static-content:deploy
While this command works in most cases, Magento also provides several useful variations that can speed up the deployment process.
Deploy Static Content Magento Command Variations
To make deployment faster and more flexible, Magento offers different command variations:
- to deploy a specific language —
php bin/magento setup:static-content:deploy en_US
- to deploy multiple languages —
php bin/magento setup:static-content:deploy fr_FR de_DE
- to make a force deployment even outside production mode —
php bin/magento setup:static-content:deploy -f
- to deploy only a specific theme —
php bin/magento setup:static-content:deploy -t Vendor/theme
- to make faster deployment (parallel jobs) —
php bin/magento setup:static-content:deploy -j 4
- to target specific parts of the site (adminhtml or frontend) —
php bin/magento setup:static-content:deploy--area[=AREA]
- to deploy static content without JavaScript files —
php bin/magento setup:static-content:deploy --no-javascript
- to deploy static content without images —
php bin/magento setup:static-content:deploy --no-images
How to Deploy Static Content in Magento Without Downtime?
To avoid downtime during static content deployment in Magento, you need to build the files in the background instead of the live store and switch them only once the deployment is finished. That's a feature offered by the extension by Magefan.
There's always downtime when deploying static content in Magento by default. That's because Magento rebuilds files directly in the pub/static folder on a live store. As a result, visitors may see missing styles, broken JavaScript, or pages that don't load correctly.
The only solution Magento offers here is maintenance mode. But it makes the entire storefront unavailable for the time of deployment, which leads to abandoned shopping carts and lost sales.
Magefan extension builds files in a separate folder, allowing visitors to see and use the old working version. This means no interruptions to the shopping process at all.
FAQs
To check the current mode in Magento, run the following CLI command:
php bin/magento deploy:mode:show
