These days, most modern websites are always changing. Websites constantly modify their layout, content, and access configurations, basing on the request of the users. In many cases, depending on who accesses it, different contents are displayed to each user. This content is called dynamic content; it can change on a per-request basis.
Static content, on the contrary, changes little over time, it does not vary to the request of each access. Therefore, it’s generally time-saving with cache, process, and transfer.
This Magento 2 static content deploy tutorial would give you a complete guide to configure deploy static content (or static view file). After the deployment, developers will be able to write static files to the system when your system is on the Magento 2 production model.
Configuring Magento 2 Static Content Deploy in Developer Mode
Table of Contents
NEED TO CHECK NOW >>> Magento EAV Model – Things You Might Not Know.
In developer mode, you have to clean the old files of CSS, JavaScript, Layouts, etc. to make sure that static files run well when you enable a new module. At that time, you have something to do with Magento 2 Static Content Deploy.
Here are the steps you can follow to clean generated static view files:
Step 1: Use the below command to manually clear all the files under pub/static directory except .htaccess file:
find. -depth -name .htaccess -prune -o -delete
Step 2: In your Magento 2 Admin Panel, you can do an automated clearing of generated static view files by navigating to System/ Tools/ Cache Management and clicking on the Flush Static Files Cache.
Using CLI to Deploy Magento 2 Static Content in Magento Production Mode
When the website is in Magento 2 production mode, you can find static view files in dir/pub/static and some of them in dir/var/view_preprocessed directory.
Next, those static view files need to be written to your Magento 2 file system manually by the CLI command.
Step 1: Login or Switch to your Magento 2 file system owner.
Step 2: Remove the content under <your Magento install dir>/pub/static.
Step 3: Run the static view files deployment tool by <your Magento install dir>/bin/magento setup:static-content:deploy.
Note: when you enable merging static view files in your Magento Admin, pub/static directory system must be writable.
Troubleshooting Tool of Magento 2 Static Content Deploy
READ NOW >>> Magento 2 Grunt Installation and Configuration.
While making Magento static content deploy, you might be facing the below issue.
If you come up with the error above, just follow these steps to tackle the problem.
- Install Magento 2 using the command line or setup wizard Using Command-Line or Setup Wizard for Magento 2 installation
- Login or Switch to your Magento 2 system owner
- Remove contents under <your Magento 2 install dir>/pub/static
- And run the Magento 2 Static Content deploy tool <your Magento install dir>/bin/Magento setup:static-content:deploy.
Tips for Developers to Customize Magento Static Content Deploy Tool
When you want to customize the implementation of the Magento 2 static content deploy tool, we recommend you use only atomic file writing for files that should be available on the client. In case you use non-atomic file writing, those files might be loaded on the client with partial content.
To make the file atomic, you should try writing to files stored in a temporary directory then copy or move them to the destination directory (from where they are loaded to clients) after writing is over.
Above we have introduced you to ways of Static Content Deploy in Magento product mode and Magento developer mode. Hope they are helpful.
About BSS Commerce:
We are one of the leading Magento extension providers and web development services in the world. With experienced and certified Magento developers, we commit to bring high-quality products and services to optimize our business effectively. Contact now to let us know your problems. We are willing to support you every time.