Skip to content

Commit 1e6af5f

Browse files
committed
BREAKING: Update to next.js recommended plugin signature
See comments: vercel/next.js#38498 (comment) Reference implementation: https://github.com/vercel/next.js/blob/canary/packages/next-mdx/index.js#L2
1 parent 7bcc597 commit 1e6af5f

File tree

18 files changed

+452
-489
lines changed

18 files changed

+452
-489
lines changed

README.md

Lines changed: 76 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@ This plugin is powered by [workbox](https://developer.chrome.com/docs/workbox/)
3131
>
3232
> **NOTE 2** - If you encounter error `TypeError: Cannot read property **'javascript' of undefined**` during build, [please consider upgrade to webpack5 in `next.config.js`](https://github.com/shadowwalker/next-pwa/issues/198#issuecomment-817205700).
3333
34-
----
34+
---
3535

3636
[![Open in Gitpod](https://img.shields.io/badge/Open%20In-Gitpod.io-%231966D2?style=for-the-badge&logo=gitpod)](https://gitpod.io/#https://github.com/shadowwalker/next-pwa/)
3737

3838
## Install
3939

4040
> If you are new to `next.js` or `react.js` at all, you may want to first checkout [learn next.js](https://nextjs.org/learn/basics/create-nextjs-app) or [next.js document](https://nextjs.org/docs/getting-started). Then start from [a simple example](https://github.com/shadowwalker/next-pwa/tree/master/examples/next-9) or [progressive-web-app example in next.js repository](https://github.com/vercel/next.js/tree/canary/examples/progressive-web-app).
4141
42-
``` bash
42+
```bash
4343
yarn add next-pwa
4444
```
4545

@@ -49,13 +49,13 @@ yarn add next-pwa
4949

5050
Update or create `next.config.js` with
5151

52-
``` javascript
53-
const withPWA = require('next-pwa')
52+
```javascript
53+
const withPWA = require('next-pwa')({
54+
dest: 'public'
55+
})
5456

5557
module.exports = withPWA({
56-
pwa: {
57-
dest: 'public'
58-
}
58+
// next.js config
5959
})
6060
```
6161

@@ -65,7 +65,6 @@ If you are using Next.js version 9 or newer, then skip the options below and mov
6565

6666
If you are using Next.js older than version 9, you'll need to pick an option below before continuing to Step 2.
6767

68-
6968
### Option 1: Host Static Files
7069

7170
Copy files to your static file hosting server, so that they are accessible from the following paths: `https://yourdomain.com/sw.js` and `https://yourdomain.com/workbox-*.js`.
@@ -89,23 +88,21 @@ const next = require('next')
8988
const app = next({ dev: process.env.NODE_ENV !== 'production' })
9089
const handle = app.getRequestHandler()
9190

92-
app.prepare()
93-
.then(() => {
94-
createServer((req, res) => {
95-
const parsedUrl = parse(req.url, true)
96-
const { pathname } = parsedUrl
97-
98-
if (pathname === '/sw.js' || /^\/(workbox|worker|fallback)-\w+\.js$/.test(pathname)) {
99-
const filePath = join(__dirname, '.next', pathname)
100-
app.serveStatic(req, res, filePath)
101-
} else {
102-
handle(req, res, parsedUrl)
103-
}
104-
})
105-
.listen(3000, () => {
106-
console.log(`> Ready on http://localhost:${3000}`)
107-
})
91+
app.prepare().then(() => {
92+
createServer((req, res) => {
93+
const parsedUrl = parse(req.url, true)
94+
const { pathname } = parsedUrl
95+
96+
if (pathname === '/sw.js' || /^\/(workbox|worker|fallback)-\w+\.js$/.test(pathname)) {
97+
const filePath = join(__dirname, '.next', pathname)
98+
app.serveStatic(req, res, filePath)
99+
} else {
100+
handle(req, res, parsedUrl)
101+
}
102+
}).listen(3000, () => {
103+
console.log(`> Ready on http://localhost:${3000}`)
108104
})
105+
})
109106
```
110107

111108
> The following setup has nothing to do with `next-pwa` plugin, and you probably have already set them up. If not, go ahead and set them up.
@@ -148,43 +145,43 @@ Create a `manifest.json` file in your `public` folder:
148145

149146
Add the following into `_document.jsx` or `_app.tsx`, in `<Head>`:
150147

151-
``` html
152-
<meta name='application-name' content='PWA App' />
153-
<meta name='apple-mobile-web-app-capable' content='yes' />
154-
<meta name='apple-mobile-web-app-status-bar-style' content='default' />
155-
<meta name='apple-mobile-web-app-title' content='PWA App' />
156-
<meta name='description' content='Best PWA App in the world' />
157-
<meta name='format-detection' content='telephone=no' />
158-
<meta name='mobile-web-app-capable' content='yes' />
159-
<meta name='msapplication-config' content='/icons/browserconfig.xml' />
160-
<meta name='msapplication-TileColor' content='#2B5797' />
161-
<meta name='msapplication-tap-highlight' content='no' />
162-
<meta name='theme-color' content='#000000' />
163-
164-
<link rel='apple-touch-icon' href='/icons/touch-icon-iphone.png' />
165-
<link rel='apple-touch-icon' sizes='152x152' href='/icons/touch-icon-ipad.png' />
166-
<link rel='apple-touch-icon' sizes='180x180' href='/icons/touch-icon-iphone-retina.png' />
167-
<link rel='apple-touch-icon' sizes='167x167' href='/icons/touch-icon-ipad-retina.png' />
168-
169-
<link rel='icon' type='image/png' sizes='32x32' href='/icons/favicon-32x32.png' />
170-
<link rel='icon' type='image/png' sizes='16x16' href='/icons/favicon-16x16.png' />
171-
<link rel='manifest' href='/manifest.json' />
172-
<link rel='mask-icon' href='/icons/safari-pinned-tab.svg' color='#5bbad5' />
173-
<link rel='shortcut icon' href='/favicon.ico' />
174-
<link rel='stylesheet' href='https://fonts.googleapis.com/css?family=Roboto:300,400,500' />
175-
176-
<meta name='twitter:card' content='summary' />
177-
<meta name='twitter:url' content='https://yourdomain.com' />
178-
<meta name='twitter:title' content='PWA App' />
179-
<meta name='twitter:description' content='Best PWA App in the world' />
180-
<meta name='twitter:image' content='https://yourdomain.com/icons/android-chrome-192x192.png' />
181-
<meta name='twitter:creator' content='@DavidWShadow' />
182-
<meta property='og:type' content='website' />
183-
<meta property='og:title' content='PWA App' />
184-
<meta property='og:description' content='Best PWA App in the world' />
185-
<meta property='og:site_name' content='PWA App' />
186-
<meta property='og:url' content='https://yourdomain.com' />
187-
<meta property='og:image' content='https://yourdomain.com/icons/apple-touch-icon.png' />
148+
```html
149+
<meta name="application-name" content="PWA App" />
150+
<meta name="apple-mobile-web-app-capable" content="yes" />
151+
<meta name="apple-mobile-web-app-status-bar-style" content="default" />
152+
<meta name="apple-mobile-web-app-title" content="PWA App" />
153+
<meta name="description" content="Best PWA App in the world" />
154+
<meta name="format-detection" content="telephone=no" />
155+
<meta name="mobile-web-app-capable" content="yes" />
156+
<meta name="msapplication-config" content="/icons/browserconfig.xml" />
157+
<meta name="msapplication-TileColor" content="#2B5797" />
158+
<meta name="msapplication-tap-highlight" content="no" />
159+
<meta name="theme-color" content="#000000" />
160+
161+
<link rel="apple-touch-icon" href="/icons/touch-icon-iphone.png" />
162+
<link rel="apple-touch-icon" sizes="152x152" href="/icons/touch-icon-ipad.png" />
163+
<link rel="apple-touch-icon" sizes="180x180" href="/icons/touch-icon-iphone-retina.png" />
164+
<link rel="apple-touch-icon" sizes="167x167" href="/icons/touch-icon-ipad-retina.png" />
165+
166+
<link rel="icon" type="image/png" sizes="32x32" href="/icons/favicon-32x32.png" />
167+
<link rel="icon" type="image/png" sizes="16x16" href="/icons/favicon-16x16.png" />
168+
<link rel="manifest" href="/manifest.json" />
169+
<link rel="mask-icon" href="/icons/safari-pinned-tab.svg" color="#5bbad5" />
170+
<link rel="shortcut icon" href="/favicon.ico" />
171+
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500" />
172+
173+
<meta name="twitter:card" content="summary" />
174+
<meta name="twitter:url" content="https://yourdomain.com" />
175+
<meta name="twitter:title" content="PWA App" />
176+
<meta name="twitter:description" content="Best PWA App in the world" />
177+
<meta name="twitter:image" content="https://yourdomain.com/icons/android-chrome-192x192.png" />
178+
<meta name="twitter:creator" content="@DavidWShadow" />
179+
<meta property="og:type" content="website" />
180+
<meta property="og:title" content="PWA App" />
181+
<meta property="og:description" content="Best PWA App in the world" />
182+
<meta property="og:site_name" content="PWA App" />
183+
<meta property="og:url" content="https://yourdomain.com" />
184+
<meta property="og:image" content="https://yourdomain.com/icons/apple-touch-icon.png" />
188185

189186
<!-- apple splash screen images -->
190187
<!--
@@ -198,17 +195,20 @@ Add the following into `_document.jsx` or `_app.tsx`, in `<Head>`:
198195
-->
199196
```
200197

201-
> Tip: Put the `viewport` head meta tag into `_app.js` rather than in `_document.js` if you need it.
198+
> Tip: Put the `viewport` head meta tag into `_app.js` rather than in `_document.js` if you need it.
202199
203-
``` typescript
204-
<meta name='viewport' content='minimum-scale=1, initial-scale=1, width=device-width, shrink-to-fit=no, user-scalable=no, viewport-fit=cover' />
200+
```typescript
201+
<meta
202+
name='viewport'
203+
content='minimum-scale=1, initial-scale=1, width=device-width, shrink-to-fit=no, user-scalable=no, viewport-fit=cover'
204+
/>
205205
```
206206

207207
## Offline Fallbacks
208208

209209
Offline fallbacks are useful when the fetch failed from both cache and network, a precached resource is served instead of present an error from browser.
210210

211-
To get started simply add a `/_offline` page such as `pages/_offline.js` or `pages/_offline.jsx` or `pages/_offline.ts` or `pages/_offline.tsx`. Then you are all set! When the user is offline, all pages which are not cached will fallback to '/_offline'.
211+
To get started simply add a `/_offline` page such as `pages/_offline.js` or `pages/_offline.jsx` or `pages/_offline.ts` or `pages/_offline.tsx`. Then you are all set! When the user is offline, all pages which are not cached will fallback to '/\_offline'.
212212

213213
**[Use this example to see it in action](https://github.com/shadowwalker/next-pwa/tree/master/examples/offline-fallback-v2)**
214214

@@ -221,17 +221,17 @@ You can also setup `precacheFallback.fallbackURL` in your [runtimeCaching config
221221
There are options you can use to customize the behavior of this plugin by adding `pwa` object in the next config in `next.config.js`:
222222

223223
```javascript
224-
const withPWA = require('next-pwa')
224+
const withPWA = require('next-pwa')({
225+
dest: 'public'
226+
// disable: process.env.NODE_ENV === 'development',
227+
// register: true,
228+
// scope: '/app',
229+
// sw: 'service-worker.js',
230+
//...
231+
})
225232

226233
module.exports = withPWA({
227-
pwa: {
228-
dest: 'public',
229-
// disable: process.env.NODE_ENV === 'development',
230-
// register: true,
231-
// scope: '/app',
232-
// sw: 'service-worker.js',
233-
//...
234-
}
234+
// next.js config
235235
})
236236
```
237237

@@ -260,7 +260,7 @@ module.exports = withPWA({
260260
- example: `['!img/super-large-image.jpg', '!fonts/not-used-fonts.otf']`
261261
- buildExcludes - an array of extra pattern or function to exclude files from being precached in `.next/static` (or your custom build) folder
262262
- default: `[]`
263-
- example: `[/chunks\/images\/.*$/]` - Don't precache files under `.next/static/chunks/images` (Highly recommend this to work with `next-optimized-images` plugin)
263+
- example: `[/chunks\/images\/.*$/]` - Don't precache files under `.next/static/chunks/images` (Highly recommend this to work with `next-optimized-images` plugin)
264264
- doc: Array of (string, RegExp, or function()). One or more specifiers used to exclude assets from the precache manifest. This is interpreted following the same rules as Webpack's standard exclude option.
265265
- cacheStartUrl - whether to cache start url
266266
- default: `true`
@@ -311,7 +311,7 @@ Here is the [document on how to write runtime caching configurations](https://de
311311
3. When you are debugging service worker, constantly `clean application cache` to reduce some flaky errors.
312312
4. If you are redirecting the user to another route, please note [workbox by default only cache response with 200 HTTP status](https://developer.chrome.com/docs/workbox/modules/workbox-cacheable-response#what_are_the_defaults), if you really want to cache redirected page for the route, you can specify it in `runtimeCaching` such as `options.cacheableResponse.statuses=[200,302]`.
313313
5. When debugging issues, you may want to format your generated `sw.js` file to figure out what's really going on.
314-
6. Force `next-pwa` to generate worker box production build by specify the option `mode: 'production'` in your `pwa` section of `next.config.js`. Though `next-pwa` automatically generate the worker box development build during development (by running `next`) and worker box production build during production (by running `next build` and `next start`). You may still want to force it to production build even during development of your web app for following reason:
314+
6. Force `next-pwa` to generate worker box production build by specify the option `mode: 'production'` in your `pwa` section of `next.config.js`. Though `next-pwa` automatically generate the worker box development build during development (by running `next`) and worker box production build during production (by running `next build` and `next start`). You may still want to force it to production build even during development of your web app for following reason:
315315
1. Reduce logging noise due to production build doesn't include logging.
316316
2. Improve performance a bit due to production build is optimized and minified.
317317
7. If you just want to disable worker box logging while keeping development build during development, [simply put `self.__WB_DISABLE_DEV_LOGS = true` in your `worker/index.js` (create one if you don't have one)](https://github.com/shadowwalker/next-pwa/blob/c48ef110360d0138ad2dacd82ab96964e3da2daf/examples/custom-worker/worker/index.js#L6).
Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
const withPWA = require('next-pwa')
2-
3-
module.exports = withPWA({
4-
pwa: {
5-
dest: 'public'
6-
}
1+
const withPWA = require('next-pwa')({
2+
dest: 'public'
73
})
4+
5+
module.exports = withPWA()

examples/cookie/next.config.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
const withPWA = require('next-pwa')
2-
3-
module.exports = withPWA({
4-
pwa: {
5-
dest: 'public',
6-
dynamicStartUrl: true, // this is same as default value
7-
dynamicStartUrlRedirect: '/login' // recommend to config this for best user experience if your start-url redirects on first load
8-
}
1+
const withPWA = require('next-pwa')({
2+
dest: 'public',
3+
dynamicStartUrl: true, // this is same as default value
4+
dynamicStartUrlRedirect: '/login' // recommend to config this for best user experience if your start-url redirects on first load
95
})
6+
7+
module.exports = withPWA()

examples/custom-ts-worker/README.md

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,39 +10,37 @@ Simply create a `worker/index.ts` and start implementing your service worker. `n
1010

1111
In this way, you get benefit of code splitting and size minimization automatically. Yes! `require` modules works! Yes! you can share codes between web app and the service worker!
1212

13-
> - In dev mode, `worker/index.ts` is not watch, so it will not hot reload.
13+
> - In dev mode, `worker/index.ts` is not watched, so it will not hot reload.
1414
1515
### Custom Worker Directory
1616

1717
You can customize the directory of your custom worker file by setting the `customWorkerDir` relative to the `basedir` in the `pwa` section of your `next.config.js`:
1818

19-
20-
``` javascript
21-
const withPWA = require('next-pwa')
19+
```javascript
20+
const withPWA = require('next-pwa')({
21+
customWorkerDir: 'serviceworker'
22+
// ...
23+
})
2224

2325
module.exports = withPWA({
24-
pwa: {
25-
customWorkerDir: 'serviceworker'
26-
...
27-
}
26+
// next.js config
2827
})
2928
```
3029

3130
In this example, `next-pwa` would look for `serviceworker/index.ts`.
3231

33-
3432
## Old Method (Still Works)
3533

3634
Basically you need to create a file such as `worker.js` in `public` folder, then add an option `importScripts` to `pwa` object in `next.config.js`:
3735

38-
``` javascript
39-
const withPWA = require('next-pwa')
36+
```javascript
37+
const withPWA = require('next-pwa')({
38+
dest: 'public',
39+
importScripts: ['/worker.js']
40+
})
4041

4142
module.exports = withPWA({
42-
pwa: {
43-
dest: 'public',
44-
importScripts: ['/worker.js']
45-
}
43+
// next.js config
4644
})
4745
```
4846

@@ -52,7 +50,7 @@ Then service worker generated will automatically import your code and run it bef
5250

5351
[![Open in Gitpod](https://img.shields.io/badge/Open%20In-Gitpod.io-%231966D2?style=for-the-badge&logo=gitpod)](https://gitpod.io/#https://github.com/shadowwalker/next-pwa/)
5452

55-
``` bash
53+
```bash
5654
cd examples/custom-ts-server
5755
yarn install
5856
yarn build
@@ -66,6 +64,3 @@ yarn start
6664
**/public/sw.js
6765
**/public/worker-*.js
6866
```
69-
70-
71-
Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
const withPWA = require('next-pwa')
2-
3-
module.exports = withPWA({
4-
pwa: {
5-
dest: 'public'
6-
}
1+
const withPWA = require('next-pwa')({
2+
dest: 'public'
73
})
4+
5+
module.exports = withPWA()

0 commit comments

Comments
 (0)