Every Magento website has two main kinds of content files: static files and dynamic files. Static files are stored on local (or client-side) devices, whereas dynamic ones are “stored” on the server-side. Static files are typically HTML pages and images that are served to all users at all times similarly, whereas dynamic files usually generate upon user interaction.
Static View Files in Magento 2 is a Magento feature that ensures your store continues to perform as expected on various user devices. How? With the help of Static View Files Deployment, you can ensure that store pages are correctly configured, static views files get downloaded by the browser when requested, and static content appears per user requests.
To simplify, the deployment commands of Static View Files can write static files to the Magento for production (LIVE) mode. If you are curious about where these files are stored, you can locate them easily in Magento’s static directory, usually found under <Magento 2 install dir>/pub/static. Furthermore, some files are cached in <Magento 2 install dir>/var/view_preprocessed.
It’s important to note that you should always pick the fastest Magento hosting solution for your eCommerce venture. A speedy server will always be able to serve static files faster.
In this guide, you will learn about the various aspects of deploying static view files in Magento 2 using CLI for Production Mode, as well as how to use this feature in Development Mode and how to troubleshoot Deployment Tool errors.
How to Deploy Static View Files in Magento 2 using CLI
There is one requisite for this process: You need to have the required permission to write a file inside your Magento 2 directory for performing this action. This is how you can deploy static view files in Magento 2 via CLI:
- Start by logging into your Magento 2 server and ensure you have permission to write files to the Magento 2 file system.
- From the bash shell, use the following syntax to switch to the Magento file system owner:
su <Magento file system owner> -s /bin/bash -c <command>
- Add /bin after your Magento 2 install directory path. This enables you to run Magento 2 commands from any directory. Here are a few examples:
cd <your Magento 2 install dir>/bin and run them as ./magento <command name>
php <your Magento install dir>/bin/magento <command name>
- Remove all the content inside your Magento install dir>/pub/static.
- Use this syntax to roll-out static view files via deployment tool
<your Magento install dir>/bin/magento setup:static-content:deploy
However, you need to take care of a few things.
First, you should ensure you have writable rights for pub/static directory in order to achieve successful merging of the files inside your Magento admin.
Second, to deploy successfully, use the correct language format. For example, for pt_DE language, the command looks like this:
magento –ansi setup:static-content:deploy pt_DE
Here is what your screen may look like once you have used this command:
How To Clean Previously Generated Static View Files
There might be a few cases where you might have to clean previously created static view files to generate the correct output for your users. For such cases, the treatment is simple. Just follow these easy steps:
- Manual Way of Clean Up: Except for pub/static/.htaccess you should clean up pub/static and var/view_preprocessed directories and their subdirectories. Use the following command for clearing generated static view files:
find . -depth -name .htaccess -prune -o -delete
- Automated Way of Clean Up: From the Magento 2 admin panel, navigate to System > Tools > Cache Management and click on the option labeled Flush Static Files Cache.
It is advised that you carry out such operations in deployment mode only.
Troubleshooting Deployment Tool of Static View Files
Sometimes, you will see an error when you run the Deployment Tool for Static View Files after completing a Magento 2 installation. Some will be left scratching their heads when they will see the following error:
ERROR: You need to install the Magento application before running this utility.
Worry not! The solution is easy and you need to follow the below-mentioned steps:
- Install Magento 2 through the command line or setup wizard
- Login to Magento 2 system and make sure you have the permission to write/change files
- Delete contents of [your Magento 2 install dir]/pub/static
- Run the static view files deployment tool as told in the section titled “how to deploy static view files” in this article.
This short guide hopefully helps you in your Magento 2 journey. While static files are essential for a website’s interface, it is also a brilliant idea to pick the fastest Magento hosting platform for your online shop. Devrims is a one-click Magento cloud hosting provider built for online stores of all scopes and sizes. Just spin a server at no cost for six (6) days to explore the fastest Magento hosting platform out there. When ready, just upgrade the account from trial to paid via adding your credit card details and get your first Magento store mitigated for free.