Compiling code and deploying static view files in magento2

Compiling code and deploying static view files in magento2


Code compilation

To compile the code in magento2, we use setup:di:compile command which performs the following tasks :
  • Application code generation – for factories, proxies
  • Interceptor generation – improve code generation of interceptors
  • Interception cache generation – improve cache generation of interceptors
  • Area configuration aggregation – improve dependency injection configurations per area
  • Repositories code generation – create code for APIs
  • Service data attributes generation – create extension classes for data objects
Switch to the magento root folder (it is generally public_html) and run the above command -
      bin/magento setup:di:compile

The above command will show the output as below.
============================================
Compilation was started.
Plugin list generation... 9/9 [============================] 100% 46 secs 428.0 MiB
Generated code and dependency injection configuration successfully
============================================


Deploying static assets 

The deployment commands of static view files enable us to write static files to the Magento 2 file system. These static view files are located in Magento’s static directory (your Magento2 install dir/pub/static) and some files are cached in your Magento 2 install dir/var/view_preprocessed directory.

For deployment of static view files, follow these steps:

  • Log in to your Magento 2 server as, or switch to, the Magento 2 file system owner.
  • Delete the content under <your Magento install dir>/pub/static.
  • Run the static view files deployment tool <your Magento install dir>/bin/magento setup:static-content:deploy.
Use the following commands to deploy static view files :
  1. cd pub/static && find . -depth -name .htaccess -prune -o -delete && cd ../.. 
  2. rm -rf var/cache/* var/view_preprocessed/* generated/* pub/static/* var/page_cache/* var/composer_home/*
  3. php bin/magento setup:static-content:deploy -f
  4. chmod -R 777 pub/static
Flush the magento cache using the above commands to make the site load fresh content -
  1. bin/magento cache:clean
  2. bin/magento cache:flush


    • Related Articles

    • How to configure base URLs for a Magento 1 website?

      In this article, we are going to change the base URLs of a Magento 1 website, using the swiss army knife for Magento developers, sysadmins and devops i.e. n98-magerun. Base URLs can be also changed from the Magento Site backend as well as from the ...
    • How to configure base URLs for a Magento 2 website?

      In this article, we are going to change the base URLs of a Magento 2 website, using the swiss army knife for Magento developers, sysadmins and devops i.e. n98-magerun2. Base URLs can be also changed from the Magento Site backend as well as from the ...
    • Switching modes in magento2

      An overview To improve security and ease-of-use, magento has built-in commands that switches magento modes from developer to production and vice versa. Production mode also has better performance because static view files are populated in the ...
    • Magento Customer login does not work in Chrome browser

      Problem description Customers can not log in into Magento with Chrome browser, the login page just reloads without any errors or warnings. But when customer logins in other browsers and admin back-end login function properly. Cause Magento sets an ...
    • How to flush nginx pagespeed cache on a Ubuntu server?

      What is nginx pagespeed? It is actually a third party dynamic module for nginx, which can be compiled into nginx server. It is provided by Google PageSpeed, a suite of tools for measuring and optimizing the performance of your website.  Flushing ...