How to compress and optimise an Angular2 application

前端 未结 2 666
粉色の甜心
粉色の甜心 2020-12-07 07:28

I want to make my angular-cli application faster!

Right, so I have spent a number of days updating my NG2 application to work with angular-cli. At first i

相关标签:
2条回答
  • 2020-12-07 07:58

    For those interested in the HTACCESS file I am using, here it is. This does force https which slows things down by about 100ms:

    #REDIRECT ROUTES TO INDEX (fixes broken routes with angular)
    RewriteEngine on
    RewriteCond %{HTTP:X-Forwarded-Proto} !https
    RewriteCond %{HTTPS} off
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
    RewriteCond %{REQUEST_FILENAME} -s [OR]
    RewriteCond %{REQUEST_FILENAME} -l [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^.*$ - [NC,L]
    RewriteRule ^(.*) /index.html [NC,L]
    #ENABLE GZIP COMPRESSION TO IMPROVE PERFORMANCE
    AddOutputFilterByType DEFLATE text/plain
    AddOutputFilterByType DEFLATE text/html
    AddOutputFilterByType DEFLATE text/xml
    AddOutputFilterByType DEFLATE text/css
    AddOutputFilterByType DEFLATE application/xml
    AddOutputFilterByType DEFLATE application/xhtml+xml
    AddOutputFilterByType DEFLATE application/rss+xml
    AddOutputFilterByType DEFLATE application/javascript
    AddOutputFilterByType DEFLATE application/x-javascript
    # SET EXPIRE HEADERS TO IMPROVE PERFORMANCE
    <ifModule mod_expires.c>
      ExpiresActive On
      ExpiresDefault "access plus 2 days"
      ExpiresByType image/x-icon "access plus 1 year"
      ExpiresByType image/jpeg "access plus 1 year"
      ExpiresByType image/jpg "access plus 1 year"
      ExpiresByType image/png "access plus 1 year"
      ExpiresByType image/gif "access plus 1 year"
      ExpiresByType application/x-shockwave-flash "access plus 1 month"
      ExpiresByType text/css "access plus 1 month"
      ExpiresByType text/javascript "access plus 1 month"
      ExpiresByType application/pdf "access plus 1 month"
      ExpiresByType application/javascript "access plus 2 week"
      ExpiresByType application/x-javascript "access plus 2 week"
      ExpiresByType text/javascript "access plus 2 week"
      ExpiresByType text/html "access plus 600 seconds"
      ExpiresByType application/xhtml+xml "access plus 600 seconds"
    </ifModule>
    # END Expire headers
    # BEGIN Cache-Control Headers
    <ifModule mod_headers.c>
      <filesMatch "\.(ico|jpe?g|png|gif|swf)$">
        Header set Cache-Control "public"
      </filesMatch>
      <filesMatch "\.(css)$">
        Header set Cache-Control "public"
      </filesMatch>
      <filesMatch "\.(js)$">
        Header set Cache-Control "public"
      </filesMatch>
      <filesMatch "\.(x?html?|php)$">
        Header set Cache-Control "private, must-revalidate"
      </filesMatch>
    </ifModule>
    # END Cache-Control Headers
    
    0 讨论(0)
  • 2020-12-07 08:05

    There are some things that let you increase speed your app, among others:

    1. Use lazy loading of modules every where possible (link)
    2. build a project with the production profile
    3. Use Ahead of Time (AOT) compiling (link).
    4. Using Angular-Universal let you move part of rendering of project to server-side

    As of 2019: Ahead of Time (AOT) compiling is enabled by default when the ng build --prod option is given.

    0 讨论(0)
提交回复
热议问题