Skip to content

Commit 87c4e98

Browse files
chrisdwheatleytimneutkens
authored andcommitted
Add a sw-precache example (vercel#2237)
* add sw-precache example * remove npm run dev command
1 parent e7a85f7 commit 87c4e98

File tree

5 files changed

+112
-0
lines changed

5 files changed

+112
-0
lines changed

examples/with-sw-precache/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
[![Deploy to now](https://deploy.now.sh/static/button.svg)](https://deploy.now.sh/?repo=https://github.com/zeit/next.js/tree/master/examples/with-sw-precache)
2+
3+
# sw-precache example
4+
5+
## How to use
6+
7+
Download the example [or clone the repo](https://github.com/zeit/next.js):
8+
9+
```bash
10+
curl https://codeload.github.com/zeit/next.js/tar.gz/master | tar -xz --strip=2 next.js-master/examples/with-sw-precache
11+
cd with-sw-precache
12+
```
13+
14+
Install it and run:
15+
16+
```bash
17+
npm install
18+
npm run build
19+
npm start
20+
```
21+
22+
Deploy it to the cloud with [now](https://zeit.co/now) ([download](https://zeit.co/download))
23+
24+
```bash
25+
now
26+
```
27+
28+
## The idea behind the example
29+
30+
You'll often want your Service Worker to be registered at the root level to give it access to your whole application.
31+
32+
This example shows how this can be achieved alongside [sw-precache](https://github.com/GoogleChrome/sw-precache) (via [the webpack plugin](https://github.com/goldhand/sw-precache-webpack-plugin)).
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const SWPrecacheWebpackPlugin = require('sw-precache-webpack-plugin')
2+
3+
module.exports = {
4+
webpack: (config) => {
5+
config.plugins.push(
6+
new SWPrecacheWebpackPlugin({
7+
verbose: true,
8+
staticFileGlobsIgnorePatterns: [/\.next\//],
9+
runtimeCaching: [
10+
{
11+
handler: 'networkFirst',
12+
urlPattern: /^https?.*/
13+
}
14+
]
15+
})
16+
)
17+
18+
return config
19+
}
20+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"scripts": {
3+
"build": "next build",
4+
"start": "NODE_ENV=production node server.js"
5+
},
6+
"dependencies": {
7+
"next": "latest",
8+
"react": "^15.4.2",
9+
"react-dom": "^15.4.2",
10+
"sw-precache-webpack-plugin": "^0.11.3"
11+
}
12+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import React from 'react'
2+
3+
export default class extends React.PureComponent {
4+
componentDidMount() {
5+
if ('serviceWorker' in navigator) {
6+
navigator.serviceWorker
7+
.register('/service-worker.js')
8+
.then(registration => {
9+
console.log('service worker registration successful')
10+
})
11+
.catch(err => {
12+
console.warn('service worker registration failed')
13+
})
14+
}
15+
}
16+
render() {
17+
return (
18+
<p>Check the console for the Service Worker registration status.</p>
19+
)
20+
}
21+
}

examples/with-sw-precache/server.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const { createServer } = require('http')
2+
const { join } = require('path')
3+
const { parse } = require('url')
4+
const next = require('next')
5+
6+
const dev = process.env.NODE_ENV !== 'production'
7+
const app = next({ dev })
8+
const handle = app.getRequestHandler()
9+
10+
app.prepare()
11+
.then(() => {
12+
createServer((req, res) => {
13+
const parsedUrl = parse(req.url, true)
14+
const { pathname } = parsedUrl
15+
16+
if (pathname === '/service-worker.js') {
17+
const filePath = join(__dirname, '.next', pathname)
18+
app.serveStatic(req, res, filePath)
19+
} else {
20+
handle(req, res, parsedUrl)
21+
}
22+
})
23+
.listen(3000, (err) => {
24+
if (err) throw err
25+
console.log('> Ready on http://localhost:3000')
26+
})
27+
})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy