Skip to content

Commit

Permalink
Add lifecycle example
Browse files Browse the repository at this point in the history
  • Loading branch information
shadowwalker committed Jan 22, 2020
1 parent a4b8492 commit b7c4212
Show file tree
Hide file tree
Showing 18 changed files with 7,037 additions and 86 deletions.
27 changes: 27 additions & 0 deletions examples/lifecycle/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@


# next-pwa - lifecycle and register worflow control example

[TOC]

This example demostrates how to use `next-pwa` plugin to turn a `next.js` based web application into a progressive web application easily.

It demonstrates how to control the service worker registration workflow (instead automatically register the service worker) and add event listener to handle the lifecycle events. This gives you more control through out the PWA lifecycle. The key here is set `register` option in `next.config.js` to `false`, then call `window.workbox.register()` to register the service worker on your own.

## Usage

[![Open in Gitpod](https://gitpod.io/button/open-in-gitpod.svg)](https://gitpod.io/#https://github.com/shadowwalker/next-pwa/)

``` bash
cd examples/lifecycle
yarn install
yarn build
yarn start
```

## Recommend `.gitignore`

```
**/public/precache.*.js
**/public/sw.js
```
8 changes: 8 additions & 0 deletions examples/lifecycle/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const withPWA = require('next-pwa')

module.exports = withPWA({
pwa: {
dest: 'public',
register: false
}
})
17 changes: 17 additions & 0 deletions examples/lifecycle/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "next-pwa-example",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"scripts": {
"dev": "next",
"build": "next build",
"start": "next start"
},
"dependencies": {
"next": "^9.2.0",
"next-pwa": "^2.0.2",
"react": "^16.12.0",
"react-dom": "^16.12.0"
}
}
49 changes: 49 additions & 0 deletions examples/lifecycle/pages/_document.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import Document, { Head, Main, NextScript } from 'next/document'

const APP_NAME = 'next-pwa example'
const APP_DESCRIPTION = 'This is an example of using next-pwa plugin'

export default class extends Document {
static async getInitialProps(ctx) {
return await Document.getInitialProps(ctx)
}

render() {
return (
<html lang='en' dir='ltr'>
<Head>
<meta name='application-name' content={APP_NAME} />
<meta name='apple-mobile-web-app-capable' content='yes' />
<meta name='apple-mobile-web-app-status-bar-style' content='default' />
<meta name='apple-mobile-web-app-title' content={APP_NAME} />
<meta name='description' content={APP_DESCRIPTION} />
<meta name='format-detection' content='telephone=no' />
<meta name='mobile-web-app-capable' content='yes' />
<meta name='theme-color' content='#FFFFFF' />
<meta name='viewport' content='minimum-scale=1, initial-scale=1, width=device-width, shrink-to-fit=no, viewport-fit=cover' />

<link rel='apple-touch-icon' sizes='180x180' href='/icons/apple-touch-icon.png' />
<link rel='manifest' href='/manifest.json' />
<link rel='shortcut icon' href='/icons/favicon.ico' />
<style>{
`
html, body, #__next {
height: 100%;
}
#__next {
margin: 0 auto;
}
h1 {
text-align: center;
}
`
}</style>
</Head>
<body>
<Main />
<NextScript />
</body>
</html>
)
}
}
74 changes: 74 additions & 0 deletions examples/lifecycle/pages/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { useEffect } from 'react'
import Head from 'next/head'

export default () => {
// This hook only run once in browser after the component is rendered for the first time.
// It has same effect as the old componentDidMount lifecycle callback.
useEffect(()=>{
if (typeof window !== 'undefined' && 'serviceWorker' in navigator && window.workbox !== undefined) {
// add event listeners to handle any of PWA lifecycle event
// https://developers.google.com/web/tools/workbox/reference-docs/latest/module-workbox-window.Workbox#events
window.workbox.addEventListener('installed', event => {
console.log(`Event ${event.type} is triggered.`)
console.log(event)
})

window.workbox.addEventListener('controlling', event => {
console.log(`Event ${event.type} is triggered.`)
console.log(event)
})

window.workbox.addEventListener('activated', event => {
console.log(`Event ${event.type} is triggered.`)
console.log(event)
})

// ISSUE - this is not working as expected, why?
// I could only make message event listenser work when I
// manually add this listenser into sw.js file
window.workbox.addEventListener('message', event => {
console.log(`Event ${event.type} is triggered.`)
console.log(event)
})

/*
window.workbox.addEventListener('redundant', event => {
console.log(`Event ${event.type} is triggered.`)
console.log(event)
})
window.workbox.addEventListener('waiting', event => {
console.log(`Event ${event.type} is triggered.`)
console.log(event)
})
window.workbox.addEventListener('externalinstalled', event => {
console.log(`Event ${event.type} is triggered.`)
console.log(event)
})
window.workbox.addEventListener('externalactivated', event => {
console.log(`Event ${event.type} is triggered.`)
console.log(event)
})
window.workbox.addEventListener('externalwaiting', event => {
console.log(`Event ${event.type} is triggered.`)
console.log(event)
})
*/

// never forget to call register as auto register is turned off in next.config.js
window.workbox.register()
}
}, [])

return (
<>
<Head>
<title>next-pwa example</title>
</Head>
<h1>Next.js + PWA = AWESOME!</h1>
</>
)
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/lifecycle/public/icons/favicon.ico
Binary file not shown.
Binary file added examples/lifecycle/public/icons/icon-512x512.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions examples/lifecycle/public/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "next-pwa",
"short_name": "next-pwa",
"display": "standalone",
"orientation": "portrait",
"theme_color": "#FFFFFF",
"background_color": "#FFFFFF",
"start_url": "/",
"icons": [
{
"src": "/icons/android-chrome-192x192.png",
"sizes": "192x192",
"type": "image/png"
},
{
"src": "/icons/icon-512x512.png",
"sizes": "512x512",
"type": "image/png"
}
]
}
4 changes: 4 additions & 0 deletions examples/lifecycle/worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
self.addEventListener('message', event => {
console.log(`Worker message event is triggered.`)
console.debug(event)
})
Loading

0 comments on commit b7c4212

Please sign in to comment.