Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Improving the Performance of an Ionic App

Brett Uglow
September 19, 2017

Improving the Performance of an Ionic App

Slides from my presentation at Angular Meetup in Melbourne on Tuesday 19th Sept 2017 (https://www.meetup.com/AngularJS-Melbourne/events/242702029/)

Brett Uglow

September 19, 2017
Tweet

More Decks by Brett Uglow

Other Decks in Technology

Transcript

  1. Why Ionic? u Works on browsers u Works on native

    devices u Single code base u Native look-and-feel u Angular J
  2. Strategy u Break down problem into smaller pieces: 1. Server

    optimisations 2. Client optimisations u Do biggest-benefit optimisations first
  3. Web page <App/> 2a. AOT Compilation u Angular supports just-in-time

    (JIT) and ahead-of-time (AOT) compilation Source Compiler Source Compiler Web page <App/> JIT (traditional) AOT
  4. 2a. AOT Compilation - How u Use ngc instead of

    Typescript tsc u Requires changes to tsconfig.json u In Ionic, ionic-app-scripts build --prod
  5. 2a. AOT Compilation - Results Before: After: 5,549,246 bytes 1,421,729

    bytes 74% ⬇ With GZIP: 397,500 bytes 93% ⬇
  6. 2b. Code Splitting u Side effect of supporting deep-links: code

    splitting u @IonicPage({segment: '/radar/:radarId'}) u Reduced main bundle size slightly (a few kilobytes) app.bundle.js 1.js app.bundle.js 2.js
  7. 2b. Code Splitting Setup radar-detail .module.ts radar-detail .page.ts Rest of

    the source code nav.push('RadarDetailPage') (weak reference)
  8. 2b. Code Splitting – Page Module // radar-detail.module.ts import {

    NgModule } from '@angular/core' import { IonicPageModule } from 'ionic-angular' import { RadarDetailPage } from './radar-detail' @NgModule({ declarations: [RadarDetailPage], imports: [IonicPageModule.forChild(RadarDetailPage)], entryComponents: [RadarDetailPage] }) export class RadarDetailPageModule {}
  9. 2b. Code Splitting – Page Component // radar-detail.component.ts import {

    Component } from '@angular/core' import { IonicPage, NavParams } from 'ionic-angular' @IonicPage({ name: 'RadarDetailPage', segment: 'radar/:radarId' }) @Component({ templateUrl: 'radar.html', selector: 'radar-detail' }) export class RadarDetailPage { ... }
  10. 2c. Service Workers power PWAs u Progressive Web Apps (PWAs)

    u Performance uCaching static assets uOffline-mode u Push Notifications u Native app behaviours uHome screen icons uNo browser chrome
  11. 2c. Service Workers with Ionic Source files service- worker.js service-

    worker template Bundles Bundles, static assets Webpack ionic-app-scripts --prod Workbox workbox generate:sw && workbox inject:manifest
  12. 2c. service-worker.js importScripts('workbox-sw.prod.v2.0.1.js'); // not the actual filename const workbox

    = new WorkboxSW(); // Go to the Network first for API requests const cacheFirstStrategy = workbox.strategies.cacheFirst(); const networkFirstStrategy = workbox.strategies.networkFirst({networkTimeoutSeconds: 10}); // Go to the network first for APIs, then to the cache workbox.router.registerRoute('http*://*/api/*', networkFirstStrategy, 'GET'); // Cache the ionicons.woff2?v... file (querystring prevents cache-hit) workbox.router.registerRoute('/assets/fonts/*', cacheFirstStrategy); // The precache array will be populated by workbox-cli. See package.json workbox.precache([]);
  13. 2c. SW Lessons u Workbox is powerful u Start simple

    then add capabilities u Debugging can be tricky
  14. Summary u Performance is a feature u Bang-for-buck optimisations first

    u Use AoT compilation (default in Angular 5) u Service Workers are powerful – start simply then add capability
  15. Resources u Odecee Tech Radar app – https://techradar.odeceelabs.com.au u AOT

    Compiler - https://angular.io/guide/aot-compiler u Blog article for this presentation: part 1 & part 2