-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow installing as an offline Progressive Web App (#3)
* Update readme.md Updated link to ngn's implementation of k9 in readme that was pointed to a deleted sr.ht page * accessibility updates for web interface: * updated version of the language bar * always use local lb.js * * * * * Added PWA Manifest * Added favicon * Show APL logo in title * Service Worker to cache urls for offline use * Now working offline! * log error if any url failed to add to cache * log error url * manifest: specify full start url Previously, in installed PWA, it was using sohang3112.github.io as base url (i.e., without /ngn-apl) * Disable cache * add preview screenshot image * replace sohang3112 with abrudz * undo formatting refactors to preserve original code style of abrudz repo * discard formatting changes - keep original repo's code style * ignore testing scripts in testing/ folder --------- Co-authored-by: Eli Mellen <3342274+eli-oat@users.noreply.github.com> Co-authored-by: eli-oat <hi@eli.li>
- Loading branch information
1 parent
6c3487f
commit 3a4cd1b
Showing
9 changed files
with
151 additions
and
5 deletions.
There are no files selected for viewing
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,40 @@ | ||
testing/ | ||
util.sh | ||
|
||
# Created by https://www.toptal.com/developers/gitignore/api/macos | ||
# Edit at https://www.toptal.com/developers/gitignore?templates=macos | ||
|
||
### macOS ### | ||
# General | ||
.DS_Store | ||
.AppleDouble | ||
.LSOverride | ||
|
||
# Icon must end with two \r | ||
Icon | ||
|
||
|
||
# Thumbnails | ||
._* | ||
|
||
# Files that might appear in the root of a volume | ||
.DocumentRevisions-V100 | ||
.fseventsd | ||
.Spotlight-V100 | ||
.TemporaryItems | ||
.Trashes | ||
.VolumeIcon.icns | ||
.com.apple.timemachine.donotpresent | ||
|
||
# Directories potentially created on remote AFP share | ||
.AppleDB | ||
.AppleDesktop | ||
Network Trash Folder | ||
Temporary Items | ||
.apdisk | ||
|
||
### macOS Patch ### | ||
# iCloud generated files | ||
*.icloud | ||
|
||
# End of https://www.toptal.com/developers/gitignore/api/macos |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,12 @@ | ||
{ | ||
"name": "ngn/apl", | ||
"icons": [ | ||
{ | ||
"src": "icons/apl-logo.png", | ||
"type": "image/png", | ||
"sizes": "512x512" | ||
} | ||
], | ||
"start_url": "https://abrudz.github.io/ngn-apl/web/index.html", | ||
"display": "standalone" | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,70 @@ | ||
console.log('Service Worker running') | ||
const CACHE_NAME = 'ngn/apl-v1'; | ||
const urlsToCache = [ | ||
'../icons/apl-logo.png', | ||
'../web/index.html', | ||
'../web/Apl385.woff', // font | ||
'../web/index.js', | ||
'../web/lb.js', | ||
'../apl.js', | ||
'../t.js', | ||
'../t.apl' | ||
]; | ||
|
||
async function addToCache() { | ||
try { | ||
let cache = await caches.open(CACHE_NAME); | ||
console.log('Opened cache'); | ||
await cache.addAll(urlsToCache); | ||
console.log('All urls cached') | ||
} catch(error) { | ||
console.error('One or more URLs failed to cache:', error.url || error); | ||
} | ||
} | ||
|
||
self.addEventListener('install', function(event) { | ||
console.log('Install - Add required urls to cache') | ||
event.waitUntil(addToCache()); | ||
}); | ||
|
||
// Function acts as proxy - any network request goes through this function | ||
async function fetchEvent(event) { | ||
let cacheResponse = await caches.match(event.request); | ||
console.log('Cache hit', event.request.url) | ||
if (cacheResponse) { | ||
return cacheResponse; | ||
} | ||
|
||
// Ideally there should be no cache miss | ||
// as all urls should already be added to cache when service worker was installed | ||
console.log('Cache missed', event.request.url) | ||
|
||
// IMPORTANT: Clone the request. A request is a stream and | ||
// can only be consumed once. Since we are consuming this | ||
// once by cache and once by the browser for fetch, we need | ||
// to clone the response. | ||
let fetchRequest = event.request.clone(); | ||
|
||
let response = await fetch(fetchRequest); | ||
// Check if we received a valid response | ||
if(!response || response.status !== 200 || response.type !== 'basic') { | ||
console.log('Invalid response', event.request.url, response); | ||
return response; | ||
} | ||
|
||
// IMPORTANT: Clone the response. A response is a stream | ||
// and because we want the browser to consume the response | ||
// as well as the cache consuming the response, we need | ||
// to clone it so we have two streams. | ||
let responseToCache = response.clone(); | ||
|
||
let cache = await caches.open(CACHE_NAME); | ||
await cache.put(event.request, responseToCache); | ||
console.log('Put in cache', event.request.url); | ||
|
||
return response; | ||
} | ||
|
||
self.addEventListener('fetch', function(event) { | ||
event.respondWith(fetchEvent(event)); | ||
}); |
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