Giter Club home page Giter Club logo

charistheo / pwa-reload-dialog Goto Github PK

View Code? Open in Web Editor NEW
3.0 2.0 1.0 1.5 MB

A minimal dialog message for PWAs that gives the user the option to reload the page on a new version of the web app. If the dialog is dismissed, the Service Worker will remain in the waiting phase and the new version of the website will not be loaded

Home Page: https://www.npmjs.com/package/pwa-reload-dialog

HTML 3.97% JavaScript 85.91% CSS 10.12%
progressive-web-app reload-dialog service-worker pwas service-workers

pwa-reload-dialog's Introduction

<pwa-reload-dialog>

npm version Dependency status

Example PWA reload dialog

A minimal dialog message for PWAs that gives the user the option to reload the page on a new version of the web app. If the dialog is dismissed, the Service Worker will remain in the waiting phase and the new version of the website will not be loaded.

Features

  • Custom Web Component
  • Accessible
  • Works seemlessly with PWAs

๐Ÿ‘ทโ€ Build with

๐Ÿงฑ Workbox

A demo of the reload dialog

๐Ÿš€ Getting started

Install

npm

  1. Inside your project directory run

    npm install pwa-reload-dialog
    
  2. Import component

    • Inside your app's JavaScript file (ex: app.js)

      import 'pwa-reload-dialog';
      

    OR

    • Add a <script> tag in an HTML file (ex: index.html)

      <script src="./node_modules/pwa-reload-dialog/dist/index.js"></script>
      

web

  <script type="module" src="https://unpkg.com/pwa-reload-dialog"></script>

Configure Service Worker

  1. Add this listener to your Service Worker file (ex: sw.js)

     addEventListener('message', event => {
       if (event.data && event.data.type === 'SKIP_WAITING') {
           skipWaiting();
       }
     });
    

Include component in HTML

  1. Include the <pwa-reload-dialog> element inside your HTML file (ex: index.html)

     <pwa-reload-dialog></pwa-reload-dialog>
    

โš ๏ธ The first time you push your code live the dialog will not be shown, however it will be shown on every new version after that.

โš ๏ธ In the above example the component will run on default options that are explained below ๐Ÿ‘‡๐Ÿ‘‡

๐Ÿ“š API Docs

JavaScript

  • swUrl: String - Local Service Worker JavaScript file url

    • Default = './sw.js'
  • swScope: String - The scope by which Service Worker has been registered

    • Default = '/'
  • labelText: String - Dialog main text

    • Default = 'A new version is available ๐Ÿ’Ž'
  • color: String - Dialog CSS text color

    • Default = '#303f9f'
  • background-color: String - Dialog CSS background color

    • Default = '#ff4081'
  • timeout: String|Number - Dialog's timeout until it is dismissed (ms)

    • Default = null

JavaScript API Example

const pwaReloadDialog = document.querySelector('pwa-reload-dialog');

pwaReloadDialog.swUrl = './service-worker.js';
pwaReloadDialog.swScope = '/';
pwaReloadDialog.labelText = 'Hello there! New version in town!';
pwaReloadDialog.timeout = 8000;
pwaReloadDialog.color = '#d500f9';
pwaReloadDialog.backgroundColor = '#232323';

HTML

You can either configure the component using JavaScript or even by HTML attributes.

HTML API Example

<pwa-reload-dialog 
  sw-url="./service-worker.js" 
  sw-scope="/"
  timeout="8000"
  label-text="Hello there! New version in town!"
  color="#d500f9"
  background-color="#232323"
></pwa-reload-dialog>

Polyfills

Install polyfills

  npm install @webcomponents/webcomponentsjs

Load polyfill

  <!-- load webcomponents bundle, which includes all the necessary polyfills -->
  <script src="node_modules/@webcomponents/webcomponentsjs/webcomponents-bundle.js"></script>

โ„น More info on polyfills

Logging

Logging is enabled only when the component has a dev attibute like so:

<pwa-reload-dialog dev></pwa-reload-dialog>

pwa-reload-dialog's People

Contributors

dependabot[bot] avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

syedpeer

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.