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

Building Progressive Web Apps with Polymer

Kelvin Gobo
November 10, 2017

Building Progressive Web Apps with Polymer

Progressive webs apps are one of the hottest things in web development at the moment. In this talk, I give a general overview about them and how to use polymer - a Javascript library built on web components to build them - to get started building PWAs very easily..

Kelvin Gobo

November 10, 2017
Tweet

More Decks by Kelvin Gobo

Other Decks in Technology

Transcript

  1. Web apps have been restricted to the BROWSER But what

    if there was a way around that...
  2. Recap Webapps have been restricted to the browser PWAs and

    Service workers give us a way beyond those restrictions to do more with web apps
  3. Structuring your PWA Tip No matter the kind of app

    you are building, structure plays an important role.
  4. import React, { Component } from 'react'; class AppToolbar extends

    Component { render() { return ( <img src="menu-icon.jpg" alt="menu icon"> <h1 className="App-title">Welcome to React</h1> ); } } export default AppToolbar; import { Component } from '@angular/core'; @Component({ selector: 'app-toolbar' , template: ` <img src=”menu-icon” alt=”menu icon”> <h1>App shell</h1> `, styleUrls: ['./app.component.css'] }) export class AppToolbar {} Components in popular frameworks
  5. Framework give you a lot... But it comes at a

    cost: ➢ Size ➢ Load times ➢ No standards
  6. Web Components Specifications a set of web platform APIs that

    allow you to create new custom, reusable, encapsulated HTML tags to use in web pages and web apps.
  7. Specs Web components are based on four main specifications: ➔

    Custom Elements ➔ Shadow DOM ➔ HTML Imports ➔ HTML Template
  8. Custom Elements ➢ lays the foundation for designing and using

    new types of DOM elements ➢ <devfest-home>, <image-slider>
  9. HTML Imports ➢ defines the inclusion and reuse of HTML

    documents in other HTML documents ➢ <link rel=”import” src=”devfest-home.html”>
  10. HTML Template ➢ defines how to declare fragments of markup

    that go unused at page load, but can be instantiated later on at runtime ➢ <template> <img src=”some-image.jpg” alt=”some image”> <div>Some title</div> </template>
  11. // register the new element class AppToolbar extends HTMLElement {

    ... } // register the new element customElements.define('app-toolbar', AppToolbar); Creating a vanilla custom element
  12. // register the new element class AppToolbar extends HTMLElement {

    constructor() { super(); // always call super() first in the constructor. // Add event listeners, shadow root // Don’t inspect attributes or children } } // register the new element customElements.define('app-toolbar', AppToolbar); Lifecycle callbacks
  13. // register the new element class AppToolbar extends HTMLElement {

    connectedCallback(){ // called anytime your element is inserted into the DOM // running setup code like rendering or fetching resources } disconnectedCallback(){ // called anytime your element is removed from the DOM // remove event listeners } } // register the new element customElements.define('app-toolbar', AppToolbar); Lifecycle callbacks
  14. // register the new element class AppToolbar extends HTMLElement {

    // Monitor the 'src' attribute for changes. static get observedAttributes() { return ['title']; } // Respond to attribute changes. attributeChangedCallback(attr, oldValue, newValue) { // set attr if there is a new value this[attr] = newValue; } } // register the new element customElements.define('app-toolbar', AppToolbar); Lifecycle callbacks
  15. // register the new element class AppToolbar extends HTMLElement {

    set height(height) { this.height = height; } get height(){ return this.height; } } // register the new element customElements.define('app-toolbar', AppToolbar); Setters/Getters
  16. “Our mission is to make life better for users and

    developers, by helping them unlock the web platform’s full potential and by spurring the web platform to evolve and Improve” - The Polymer Team
  17. class AppToolbar extends Polymer.Element { static get is() { return

    'app-toolbar'; } } Creating Custom elements in Polymer
  18. class AppToolbar extends Polymer.Element { static get is() { return

    'app-toolbar'; } } window.customElements.define(AppToolbar.is, AppToolbar); Creating Custom elements in Polymer
  19. class AppToolbar extends Polymer.Element { static get is() { return

    'my-view404'; } static get properties() { return { title: String, }; } } window.customElements.define(AppToolbar.is, AppToolbar); Creating Custom elements in Polymer
  20. class AppToolbar extends Polymer.Element { static get is() { return

    'my-view404'; } static get properties() { return { title: String, }; } constructor() { this.page = 'view404'; } connectedCallback() { this.page = 'view404'; } } window.customElements.define(AppToolbar.is, AppToolbar); Creating Custom elements in Polymer
  21. “Polymer App Toolbox is a collection of components, tools and

    templates for building Progressive Web Apps with Polymer.”
  22. Features ➢ Component-based architecture using Polymer and web components ➢

    Responsive design using the app layout components. ➢ Offline caching as a progressive enhancement, using service workers ➢ Build tooling to support serving your app multiple ways: HTTP/1 or HTTP/2
  23. PRPL Pattern Push critical resources for initial route Render the

    initial route Pre-cache components for the remaining routes Lazy-load remaining routes on demand
  24. Useful Links Polymer project - https://www.polymer-project.org Workbox - https://developers.google.com/web/tools/workbox/ Web

    components - https://www.webcomponents.org/introduction Progressive Web Apps -https://developers.google.com/web/progressive-web-apps