diff --git a/.gitignore b/.gitignore index 0887d157..d2210a24 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,6 @@ tests/Release /*.js /*.ulx -/*.z5 \ No newline at end of file +/*.z5 + +!service-worker.js \ No newline at end of file diff --git a/index.html b/index.html index 7c6aab5c..46914f3a 100644 --- a/index.html +++ b/index.html @@ -7,7 +7,8 @@ - + +
@@ -24,6 +25,9 @@

Parchment

Or, click here to play a story file on your device

+
@@ -34,4 +38,36 @@

Parchment

+ + + + \ No newline at end of file diff --git a/manifest.json b/manifest.json new file mode 100644 index 00000000..f75ff163 --- /dev/null +++ b/manifest.json @@ -0,0 +1,54 @@ +{ + "name": "Parchment", + "short_name": "Parchment", + "start_url": "/", + "display": "standalone", + "description": "An interpreter for Interactive Fiction", + "lang": "en-us", + "dir": "auto", + "theme_color": "#000000", + "background_color": "#000000", + "orientation": "portrait-primary", + "icons": [ + { + "src": "src/web/icons/manifest-icon-192.maskable.png", + "sizes": "192x192", + "type": "image/png", + "purpose": "any" + }, + { + "src": "src/web/icons/manifest-icon-192.maskable.png", + "sizes": "192x192", + "type": "image/png", + "purpose": "maskable" + }, + { + "src": "src/web/icons/manifest-icon-512.maskable.png", + "sizes": "512x512", + "type": "image/png", + "purpose": "any" + }, + { + "src": "src/web/icons/manifest-icon-512.maskable.png", + "sizes": "512x512", + "type": "image/png", + "purpose": "maskable" + } + ], + "screenshots": [ + { + "src": "/src/web/icons/parchment-screenshot.png", + "sizes": "1920x984", + "type": "image/png", + "description": "A screenshot of the home page" + } + ], + "prefer_related_applications": false, + "shortcuts": [ + { + "name": "Parchment", + "url": "/", + "description": "Opens the main application" + } + ] +} \ No newline at end of file diff --git a/service-worker.js b/service-worker.js new file mode 100644 index 00000000..a95d236f --- /dev/null +++ b/service-worker.js @@ -0,0 +1,93 @@ + + // Based off of https://github.com/pwa-builder/PWABuilder/blob/main/docs/sw.js + + /* + Welcome to our basic Service Worker! This Service Worker offers a basic offline experience + while also being easily customizeable. You can add in your own code to implement the capabilities + listed below, or change anything else you would like. + + + Need an introduction to Service Workers? Check our docs here: https://docs.pwabuilder.com/#/home/sw-intro + Want to learn more about how our Service Worker generation works? Check our docs here: https://docs.pwabuilder.com/#/studio/existing-app?id=add-a-service-worker + + Did you know that Service Workers offer many more capabilities than just offline? + - Background Sync: https://microsoft.github.io/win-student-devs/#/30DaysOfPWA/advanced-capabilities/06 + - Periodic Background Sync: https://web.dev/periodic-background-sync/ + - Push Notifications: https://microsoft.github.io/win-student-devs/#/30DaysOfPWA/advanced-capabilities/07?id=push-notifications-on-the-web + - Badges: https://microsoft.github.io/win-student-devs/#/30DaysOfPWA/advanced-capabilities/07?id=application-badges + */ + + const HOSTNAME_WHITELIST = [ + self.location.hostname, + 'fonts.gstatic.com', + 'fonts.googleapis.com', + 'cdn.jsdelivr.net' + ] + + // The Util Function to hack URLs of intercepted requests + const getFixedUrl = (req) => { + var now = Date.now() + var url = new URL(req.url) + + // 1. fixed http URL + // Just keep syncing with location.protocol + // fetch(httpURL) belongs to active mixed content. + // And fetch(httpRequest) is not supported yet. + url.protocol = self.location.protocol + + // 2. add query for caching-busting. + // Github Pages served with Cache-Control: max-age=600 + // max-age on mutable content is error-prone, with SW life of bugs can even extend. + // Until cache mode of Fetch API landed, we have to workaround cache-busting with query string. + // Cache-Control-Bug: https://bugs.chromium.org/p/chromium/issues/detail?id=453190 + if (url.hostname === self.location.hostname) { + url.search += (url.search ? '&' : '?') + 'cache-bust=' + now + } + return url.href + } + + /** + * @Lifecycle Activate + * New one activated when old isnt being used. + * + * waitUntil(): activating ====> activated + */ + self.addEventListener('activate', event => { + event.waitUntil(self.clients.claim()) + }) + + /** + * @Functional Fetch + * All network requests are being intercepted here. + * + * void respondWith(Promise r) + */ + self.addEventListener('fetch', event => { + // Skip some of cross-origin requests, like those for Google Analytics. + if (HOSTNAME_WHITELIST.indexOf(new URL(event.request.url).hostname) > -1) { + // Stale-while-revalidate + // similar to HTTP's stale-while-revalidate: https://www.mnot.net/blog/2007/12/12/stale + // Upgrade from Jake's to Surma's: https://gist.github.com/surma/eb441223daaedf880801ad80006389f1 + const cached = caches.match(event.request) + const fixedUrl = getFixedUrl(event.request) + const fetched = fetch(fixedUrl, { cache: 'no-store' }) + const fetchedCopy = fetched.then(resp => resp.clone()) + + // Call respondWith() with whatever we get first. + // If the fetch fails (e.g disconnected), wait for the cache. + // If there’s nothing in cache, wait for the fetch. + // If neither yields a response, return offline pages. + event.respondWith( + Promise.race([fetched.catch(_ => cached), cached]) + .then(resp => resp || fetched) + .catch(_ => { /* eat any errors */ }) + ) + + // Update the cache with the version we fetched (only for ok status) + event.waitUntil( + Promise.all([fetchedCopy, caches.open("pwa-cache")]) + .then(([response, cache]) => response.ok && cache.put(event.request, response)) + .catch(_ => { /* eat any errors */ }) + ) + } + }) diff --git a/src/web/icons/apple-icon-180.png b/src/web/icons/apple-icon-180.png new file mode 100644 index 00000000..884124f8 Binary files /dev/null and b/src/web/icons/apple-icon-180.png differ diff --git a/src/web/icons/manifest-icon-192.maskable.png b/src/web/icons/manifest-icon-192.maskable.png new file mode 100644 index 00000000..d4083601 Binary files /dev/null and b/src/web/icons/manifest-icon-192.maskable.png differ diff --git a/src/web/icons/manifest-icon-512.maskable.png b/src/web/icons/manifest-icon-512.maskable.png new file mode 100644 index 00000000..1001c556 Binary files /dev/null and b/src/web/icons/manifest-icon-512.maskable.png differ diff --git a/src/web/icons/parchment-screenshot.png b/src/web/icons/parchment-screenshot.png new file mode 100644 index 00000000..b7f4b680 Binary files /dev/null and b/src/web/icons/parchment-screenshot.png differ diff --git a/src/web/parchment.css b/src/web/parchment.css index 24515419..3fb69cee 100644 --- a/src/web/parchment.css +++ b/src/web/parchment.css @@ -51,4 +51,20 @@ body { bottom: 0; position: absolute; width: 100%; +} + +/* Hides the install button if loaded as a standalone app +since that would mean that it's already installed*/ +@media all and (display-mode: standalone) { + #pwainstallbutton { + display: none; + } +} + +/* Firefox Desktop does not support PWA install + So we hide the install button */ +@-moz-document url-prefix() { + #pwainstallbutton { + display: none; + } } \ No newline at end of file