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

PWA: expect the unexpected

PWA: expect the unexpected

Avatar for Artur Kania

Artur Kania

May 07, 2019

Other Decks in Programming

Transcript

  1. PWA - WEB, BUT BETTER... 3 ▪ Fast Respond quickly

    to user interactions even on flaky networks ▪ Reliable Load instantly even in uncertain network conditions ▪ Engaging Feel like a natural app on the device (?) “With great power comes great responsibility.” (Ben Parker, uncle of Spider-Man)
  2. REQUIREMENTS: ▪ Your website is served via HTTPS ▪ Service

    worker is served via HTTPS ▪ Manifest is served via HTTPS Avoid mixed content (HTTP and HTTPS used in parallel) 6
  3. REQUIREMENTS: ▪ Manifest must be served from your website domain

    ▪ The same for Service worker ▪ You cannot have it on CDN 11
  4. SUMMARY 13 ▪ It works on my machine! (as always)

    ▪ It controls your domain, serve it from your domain ▪ Tools and libraries won’t do everything for you (OK, it depends, sometimes they can)
  5. SERVICE WORKER Script that your browser runs in the background,

    separate from a web page. Responsible for controlling network requests 15
  6. SERVICE WORKER SCOPE When your service worker is served from

    different location and you want to control root directory: ▪ Add { scope: '/' } as registration parameter ▪ Add Service-Worker-Allowed: / header to service worker file 19
  7. CACHING STRATEGIES: ▪ Cache First (Cache Falling Back to Network)

    ▪ Network First (Network Falling Back to Cache) ▪ Network Only ▪ Cache Only (Needs custom precaching) ▪ Stale-While-Revalidate 23
  8. SERVICE WORKER - FETCH 26 ▪ Do not cache everything!

    ▪ Limit caching to GET requests only ▪ Use network only strategy for other HTTP methods ▪ Limit caching to your domains* (if applicable)
  9. PRECACHING 28 ▪ Ability to save a set of files

    to the cache when the service worker is installing ▪ Useful for caching all build files (js, css)
  10. Non-CORS FAIL BY DEFAULT Fetching a resource from a third

    party URL will fail if it doesn't support CORS. You can add a no-CORS option to the Request to overcome this, although this will cause an 'opaque' response, which means you won't be able to tell if the response was successful or not. ▪ no-cors - risky as request may fail ▪ configuring CORS properly - can be problematic with some CDNs 30
  11. ROUGH EDGES AND GOTCHS ▪ Debugging service worker ▪ Credentials

    such as cookies are not included ▪ Responsive images srcset attribute or the <picture> 31
  12. SUMMARY 32 ▪ You may not encounter the majority of

    these problems! ▪ Try to build service worker by yourself ▪ There’s always more than one solution
  13. MANIFEST JSON file that tells the browser about your web

    application and how it should behave when 'installed' on the user's mobile device or desktop. 34
  14. 40

  15. 43

  16. 44

  17. 45

  18. 46 SUMMARY 46 ▪ Build it! ▪ You don’t need

    the whole team for that ▪ You don’t need months for that ▪ Results can be really unexpected!