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

Fast PWAs With Angular

Mike Hartington
February 16, 2019
26

Fast PWAs With Angular

Progressive Web Apps! We’re constantly hearing about PWAs and why we should care about them. But when it comes to making one, we’re often left to build out all the core features of a PWA, by hand. Thankfully, we have Angular, and have the right tools to help us out. We’ll looks at patterns you can take in your Angular app to start to make it an Progressive Web App, and what tools are available to make sure our app is ready to be a PWA.

Mike Hartington

February 16, 2019
Tweet

Transcript

  1. const routes: Routes = [ { path: '', component: LandingPage

    }, { path: 'browse', loadChildren: './pages/browse/browse.module#BrowsePageModule' }, { path: 'search', loadChildren: './pages/search/search.module#SearchModule' }, { path: 'album/:id', loadChildren: './pages/album/album.module#AlbumPageModule' }, { path: 'playlist/:id', loadChildren: './pages/playlists/playlists.module#PlaylistsPageModule' } ];
  2. [ { path: '', redirectTo: '/tutorial', pathMatch: 'full' }, {

    path: 'app', loadChildren: './pages/tabs-page/tabs-page.module#TabsModule' }, { path: 'tutorial', loadChildren: './pages/tutorial/tutorial.module#TutorialModule', canLoad: [CheckTutorial] } ]
  3. import { Injectable } from '@angular/core'; import { CanLoad, Router

    } from '@angular/router'; import { Storage } from '@ionic/storage'; @Injectable({ providedIn: 'root' }) export class CheckTutorial implements CanLoad { constructor(private storage: Storage, private router: Router) {} canLoad() { return this.storage.get('ion_did_tutorial').then(res => { if (res) { this.router.navigate(['/app', 'tabs', 'schedule']); return false; } else { return true; } }); } }
  4. This is how Ionic works Each component, a single ES-Module

    Ionic will handling loading the rest FUN FACT
  5. self.addEventListener('activate', (event) => { event.waitUntil( // then create our named

    cached caches .open('my-sw-cache') .then(function(cache) { // once created, lets add some local resources return cache.addAll([ './build/main.js', './build/main.css' ]); }) .then(function(){ console.log('Service worker is ready, and assets are cached'); })); }); https://serviceworke.rs
  6. { "index": "/index.html", "assetGroups": [ { "name": "app", "installMode": "prefetch",

    "resources": { "files": ["/index.html", "/*.css", "/*.js"] } }, ], "dataGroups": [...] }
  7. import { ServiceWorkerModule } from '@angular/service-worker'; @NgModule({ declarations: [AppComponent], imports:

    [ ServiceWorkerModule.register('/ngsw-worker.js', { enabled: environment.production }), BrowserModule,
  8. { "name": "Star Track", "short_name": "StarTrack", "theme_color": "#ffffff", "background_color": "#ffffff",

    "description": "The best Apple Music Client out there", "display": "standalone", "orientation": "portrait", "start_url": "/browse", "icons": [ . { "src": "assets/icons/icon-192x192.png", "sizes": "192x192", "type": "image/png" }, . ] }
  9. <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width,

    initial-scale=1.0"> <link rel="manifest" href="manifest.json"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> </head>