Magento 2 deploy static content

What is the deploy static content 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 modes:

  • The default/developer modes: Magento 2 generates static files 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 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 deployment tool for static files: <your Magento install dir>/bin/magento setup:static-content:deploy.

Step 4: You can enter the 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 deploying static content or excluding it when deploying static content.
  • 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.
Rate the answer?