Magento 2 deploy static content

What is the Magento 2 static content deploy command and when do you need it?

When working in Magento production mode static files can be written to the file system with the static view files deployment command. After your content is deployed and you need to install a new module it won’t work without the command, as long as the extension runs the already-existing (generated) and cached files. Therefore, you should deploy the new files for they get to the static content and become usable by the Magento new module.

The ‘static view files’ are assets cached for a site from MVC, e.g.: images, CSS, fonts, JavaScript used by a theme.

The files are also placed in the <your Magento install dir>/pub/static directory, and some are cached in the <your Magento install dir>/var/view_preprocessed directory.

Static view files deployment depends on Magento 2 modes:

  • The default/developer modes: Magento 2 generates static content at your request where the rest are cached for access acceleration.
  • The production modethe static files aren’t generated or cached by request, only by command.

Clear static files in Magento 2

When installing a new extension in the developer mode it can load new static files, such as JavaScript, CSS, layouts, images, etc. To avoid issues coming from the files stacking, you need to delete the previous ones to make the new module run smoothly.

You can manage the process in several ways: 

  1. via the Admin Panel: go to System > Tools > Cache Management and select Flush Static Files Cache;
  2. manually: you can clear the pub/static and var/view_preprocessed directories and subdirectories. !!! the exception is pub/static/.htaccess (a hidden Magento 2 file);
  3. via the Command line: type the command rm -rf pub/static/*. 

 How to deploy static view files?

Step 1: Log in to the server, as the Magento file system owner: 

  • for the bash shell: run the following command to switch to the Magento file system owner and enter the targeted command simultaneously: su <Magento file system owner> -s /bin/bash -c <command>. If Magento server doesn’t let you in, try the following: sudo -u <Magento file system owner>  <command>;
  • through your system PATH: add <your Magento install dir>/bin to it. Due to the reason that bash shells have different syntax, you should learn your variables, e.g.: bash shell for CentOS: export PATH=$PATH:/var/www/html/magento2/bin;
  • avail of the next commands: cd <your Magento install dir>/bin as ./magento <command name>; php <your Magento install dir>/bin/magento <command name>.

Step 2: Remove the <your Magento install dir>/pub/static content (see Clear static files in Magento 2). 

Step 3: Use  the next Magento 2 deployment tool for static files: <your Magento install dir>/bin/magento setup:static-content:deploy.

This command rebuilds the mapping to project files.

Step 4: You can enter the Magetno 2 command options to see all the parameters (the step is optional):  magento setup:static-content:deploy [<list of languages>] [-t|--theme[="<theme>"]] [--exclude-theme[="<theme>"]] [-l|--language[="<language>"]] [--exclude-language[="<language>"]] [-a|--area[="<area>"]] [--exclude-area[="<area>"]] [-j|--jobs[="<number>"]]  [--no-javascript] [--no-css] [--no-less] [--no-images] [--no-fonts] [--no-html] [--no-misc] [--no-html-minify] [-d|--dry-run].

The command parameters and values are the following:

  • The [<list of languages>] ; [-l|--language[="<language>"]]; [--exclude-language[="<language>"]] options include the list of language codes for static view files outputting, allow generating files for specified languages (one language code can be specified at a time) or files for comma-separated list of language codes.
  • The [-t|--theme[="<theme>"]]; [--exclude-theme[="<theme>"]] options include themes for Magento static content deploy or excluding it when deploying static content. For example like this: bin/magento setup:static-content:deploy en_US --exclude-theme Magento/luma --no-html-minify
  • The  [-a|--area[="<area>"]]; [--exclude-area[="<area>"]] option allows file generation for specified areas or all (no specified) areas.
  • The [-j|--jobs[="<number>"]] option allows for a specified number of jobs usage during parallel processing.
  • The  [--no-javascript] option allows for saving the JavaScript files undeployed.
  • The [--no-css] option allows for saving the CSS files undeployed.
  • The [--no-less] option allows for saving the LESS files undeployed.
  • The [--no-images] option allows for saving the images undeployed.
  • The [--no-fonts] option allows for saving the font files undeployed.
  • The [--no-html] option allows for saving the HTML files undeployed.
  • The [--no-misc] option allows for saving other file types like .md, .jbf, .csv, .json, .txt, .htc, .swf undeployed.
  • The [--no-html-minify] option allows for not minifying the HTML files.
  • The  [-d|--dry-run] option allows for viewing the files outputting without any changes.

To ensure fast website performance, keep your cache warming scheduled and automated with Full Page Cache Warmer for M2.

See also: How to create a static CMS page in Magento 2.

 

How can we help you?

Didn’t you find the answer to your question? We are always happy to help you out.

© 2009-2024 Amasty. All Rights Reserved.