Skip to content

Instantly share code, notes, and snippets.

@jakearchibald
Last active July 5, 2016 12:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jakearchibald/4927b34bb14c3681a3c1c1ce6ede5b09 to your computer and use it in GitHub Desktop.
Save jakearchibald/4927b34bb14c3681a3c1c1ce6ede5b09 to your computer and use it in GitHub Desktop.
fetch('/page-data', {
credentials: 'include'
}).then(r => r.json()).then(data => {
document.querySelector('.content').innerHTML = data.html;
});
<!DOCTYPE html>
<h1>This is the static header</h1>
<div class="content"></div>
<script src="/app-v1.js"></script>
self.addEventListener('install', event => {
event.waitUntil(
caches.open('static-v1').then(cache =>
cache.addAll([
'/shell-v1.html',
'/app-v1.js'
])
)
);
// Serve same-origin fetches for '/' with the cached copy of shell-v1.html.
event.declareRoute({
path: '/'
}, new CachesSource('/shell-v1.html'), {
// Also trigger request for /page-data.
// This will go through the route below
preloads: [
new Request('/page-data', {credentials: 'include'})
]
});
// Fetch page-data straight from the network
event.declareRoute({
path: '/page-data'
}, new FetchSource());
// Serve any-origin fetches using the cache, or a network fetch.
event.declareRoute({
origin: ''
}, new FallbackSources(
new CachesStorage(),
new FetchStorage()
));
});
self.addEventListener('activate', event => {
// delete old caches
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment