generated from koddsson/web-dev
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #77 from koddsson/service-worker
make the website work offline
- Loading branch information
Showing
6 changed files
with
91 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,15 @@ | ||
import html from '@web/rollup-plugin-html' | ||
import {rollupPluginHTML as html} from '@web/rollup-plugin-html' | ||
import esbuild from 'rollup-plugin-esbuild' | ||
import {nodeResolve} from '@rollup/plugin-node-resolve' | ||
import {copy} from '@web/rollup-plugin-copy' | ||
|
||
export default { | ||
input: 'index.html', | ||
output: {dir: 'dist'}, | ||
plugins: [nodeResolve(), esbuild({target: 'esnext'}), html({input: 'index.html'})] | ||
plugins: [ | ||
nodeResolve(), | ||
esbuild({target: 'esnext'}), | ||
html({input: 'index.html'}), | ||
copy({patterns: './service-worker.js'}), | ||
], | ||
} |
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,26 @@ | ||
// There's probably a better caching strategy but this seemed the simplest to start with. | ||
// | ||
// From: https://web.dev/articles/offline-cookbook#on-network-response | ||
// | ||
// > If a request doesn't match anything in the cache, get it from the network, send it to the page, | ||
// > and add it to the cache at the same time. | ||
// | ||
// > If you do this for a range of URLs, such as avatars, you'll need to be careful you don't bloat | ||
// > the storage of your origin. If the user needs to reclaim disk space you don't want to be the | ||
// > prime candidate. Make sure you get rid of items in the cache you don't need any more. | ||
|
||
self.addEventListener('fetch', function (event) { | ||
event.respondWith( | ||
caches.open('rss-reader').then(function (cache) { | ||
return cache.match(event.request).then(function (response) { | ||
return ( | ||
response || | ||
fetch(event.request).then(function (response) { | ||
cache.put(event.request, response.clone()); | ||
return response; | ||
}) | ||
); | ||
}); | ||
}), | ||
); | ||
}); |
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,15 @@ | ||
if ('serviceWorker' in navigator) { | ||
// Register a service worker hosted at the root of the | ||
// site using the default scope. | ||
try { | ||
const registration = await navigator.serviceWorker.register('/service-worker.js') | ||
console.log('Service worker registration succeeded:', registration) | ||
} catch (error) { | ||
console.error(`Service worker registration failed: ${error}`) | ||
} | ||
} else { | ||
console.error('Service workers are not supported.') | ||
} | ||
|
||
// Stupidly need to tell TypeScript that this is a module like this | ||
export {} |