Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
229 additions
and
4 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
importScripts('workbox-sw.prod.v2.1.2.js') | ||
|
||
const workbox = new WorkboxSW({ | ||
skipWaiting: true, | ||
clientsClaim: true | ||
}) | ||
|
||
// following array will be filled with filenames | ||
// from `build/` folder when `generate-sw` script runs | ||
workbox.precache([]) | ||
|
||
// the listener catches all http requests coming from | ||
// the browser at my website | ||
self.addEventListener('fetch', event => { | ||
const url = new URL(event.request.url) | ||
// I want to let event through without modifying if | ||
// any of the following conditions are met | ||
if ( | ||
// if it's a request for a precached file | ||
isPrecached(url) || | ||
// if it's a request for a static file (not index.html) | ||
isStaticFile(url) || | ||
// if it's an external request to another domain | ||
isExternal(url) || | ||
// if it's a GET request to /api/* url | ||
isGetApi(event, url) | ||
) { return } | ||
|
||
// when an API action happens, for example, | ||
// "DELETE /api/session" that logs user out, | ||
// I let the request through and update index.html | ||
// cache after it's done | ||
if (event.request.method !== 'GET') { | ||
return event.respondWith( | ||
fetch(event.request) | ||
.then(response => { | ||
updateIndexCache() | ||
return response | ||
}) | ||
) | ||
} | ||
|
||
// I serve index.html network-first on any request that | ||
// reached this line | ||
event.respondWith( | ||
fetch(indexRequest()) | ||
.then(response => { | ||
updateIndexCache() | ||
return response | ||
}) | ||
.catch(() => caches.match(indexRequest())) | ||
) | ||
}) | ||
|
||
function isPrecached({ href }) { | ||
return workbox._revisionedCacheManager._parsedCacheUrls.includes(href) | ||
} | ||
|
||
function isStaticFile({ pathname }) { | ||
return pathname.includes('.') && pathname !== '/index.html' | ||
} | ||
|
||
function isExternal({ origin }) { | ||
return origin !== location.origin | ||
} | ||
|
||
// if your api has a different prefix, e.g., /api/v1/, | ||
// just update RegExp accordingly | ||
function isGetApi({ request }, { pathname }) { | ||
return request.method === 'GET' && /^\/api\/.+/.test(pathname) | ||
} | ||
|
||
async function updateIndexCache() { | ||
const cache = await caches.open('dynamic-v1') | ||
cache.add(indexRequest()) | ||
} | ||
|
||
function indexRequest() { | ||
return new Request('index.html', { credentials: 'same-origin' }) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module.exports = { | ||
"globDirectory": "build/", | ||
"globPatterns": [ | ||
"**/*.{json,ico,html,js,css,woff2,woff}" | ||
], | ||
"swSrc": "./src/service-worker.js", | ||
"swDest": "build/service-worker.js", | ||
"globIgnores": [ | ||
"../workbox-cli-config.js", | ||
"asset-manifest.json", | ||
"index.html" | ||
] | ||
}; |