Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: setup service worker for offline usage (fix #46) #48

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,29 @@
window.onload = function () { 'use strict';
myGrooveWriter.runsOnPageLoad(); // Initialize the notes
};

const registerServiceWorker = async () => {
if ('serviceWorker' in navigator) {
try {
const registration = await navigator.serviceWorker.register(
'sw.js',
{
scope: './',
}
);
if (registration.installing) {
console.log('Service worker installing');
} else if (registration.waiting) {
console.log('Service worker installed');
} else if (registration.active) {
console.log('Service worker active');
}
} catch (error) {
console.error(`Registration failed with ${error}`);
}
}
};
registerServiceWorker();
</script>


Expand Down
54 changes: 54 additions & 0 deletions sw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
var version = '1.0.0';
var timeStamp = Date.now();

self.addEventListener('install', function(e) {
e.waitUntil(
caches.open('montulli.github.io').then(function(cache) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll probably need to replace that for mikeslessons.com/groove, and change GrooveScribe with groove, but my PR is for the gh-pages one.

return cache.addAll([
'/GrooveScribe/?timestamp=' + timeStamp,
'/GrooveScribe/index.html?timestamp=' + timeStamp,
'/GrooveScribe/css/groove_display_orange.css?timestamp=' + timeStamp,
'/GrooveScribe/css/groove_writer_orange.css?timestamp=' + timeStamp,
'/GrooveScribe/css/share-button.min.css?timestamp=' + timeStamp,
'/GrooveScribe/font-awesome/4.7.0/css/font-awesome.min.css?timestamp=' + timeStamp,
'/GrooveScribe/font-awesome/4.7.0/fonts/fontawesome-webfont.woff2?v=4.7.0?timestamp=' + timeStamp,
'/GrooveScribe/images/GScribe_Logo_lone_g.svg?timestamp=' + timeStamp,
'/GrooveScribe/images/GScribe_Logo_word_stack.svg?timestamp=' + timeStamp,
'/GrooveScribe/images/gscribe-icon-96.png?timestamp=' + timeStamp,
'/GrooveScribe/js/abc2svg-1.js?timestamp=' + timeStamp,
'/GrooveScribe/js/groove_utils.js?timestamp=' + timeStamp,
'/GrooveScribe/js/groove_writer.js?timestamp=' + timeStamp,
'/GrooveScribe/js/grooves.js?timestamp=' + timeStamp,
'/GrooveScribe/js/jsmidgen.js?timestamp=' + timeStamp,
'/GrooveScribe/js/pablo.min.js?timestamp=' + timeStamp,
'/GrooveScribe/js/share-button.min.js?timestamp=' + timeStamp,
'/GrooveScribe/MIDI.js/inc/Base64.js?timestamp=' + timeStamp,
'/GrooveScribe/MIDI.js/inc/base64binary.js?timestamp=' + timeStamp,
'/GrooveScribe/MIDI.js/inc/DOMLoader.XMLHttp.js?timestamp=' + timeStamp,
'/GrooveScribe/MIDI.js/inc/jasmid/midifile.js?timestamp=' + timeStamp,
'/GrooveScribe/MIDI.js/inc/jasmid/replayer.js?timestamp=' + timeStamp,
'/GrooveScribe/MIDI.js/inc/jasmid/stream.js?timestamp=' + timeStamp,
'/GrooveScribe/MIDI.js/js/MIDI/AudioDetect.js?timestamp=' + timeStamp,
'/GrooveScribe/MIDI.js/js/MIDI/LoadPlugin.js?timestamp=' + timeStamp,
'/GrooveScribe/MIDI.js/js/MIDI/Player.js?timestamp=' + timeStamp,
'/GrooveScribe/MIDI.js/js/MIDI/Plugin.js?timestamp=' + timeStamp,
'/GrooveScribe/soundfont/gunshot-ogg.js?timestamp=' + timeStamp,
])
.then(function() {
return self.skipWaiting();
});
})
)
});

self.addEventListener('activate', function(event) {
event.waitUntil(self.clients.claim());
});

self.addEventListener('fetch', function(event) {
event.respondWith(
caches.match(event.request, {ignoreSearch:true}).then(function(response) {
return response || fetch(event.request);
})
);
});