Skip to content

Commit

Permalink
Fixed (Hopefully) PWA texteditor
Browse files Browse the repository at this point in the history
  • Loading branch information
bit-turtle authored Nov 1, 2024
1 parent 9150b60 commit 86b6b9d
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 42 deletions.
36 changes: 21 additions & 15 deletions texteditor.html
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
<!DOCTYPE html>
<html>
<head>
<title>Text Editor</title>

<!-- PWA -->
<link rel="manifest" href="texteditor.manifest.json" />
<!-- ios support -->
<link rel="apple-touch-icon" href="texteditor.png" />
<meta name="apple-mobile-web-app-status-bar" content="cornflowerblue" />
<meta name="theme-color" content="cornflowerblue" />
<link rel="manifest" href="texteditor.manifest.json">

<meta name="mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="application-name" content="Text Editor">
<meta name="apple-mobile-web-app-title" content="Text Editor">
<meta name="theme-color" content="cornflowerblue">
<meta name="msapplication-navbutton-color" content="cornflowerblue">
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
<meta name="msapplication-starturl" content="/">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

<link rel="icon" type="image/png" sizes="72x72" href="texteditor.png">
<link rel="apple-touch-icon" type="image/png" sizes="72x72" href="texteditor.png">

<title>Text Editor</title>

<style>
* { margin: 0; }
Expand Down Expand Up @@ -267,15 +276,12 @@
}
}, false);

if ("serviceWorker" in navigator) {
window.addEventListener("load", function() {
navigator.serviceWorker
.register("/texteditor.service.js")
.then(e=>{})
.catch(e=>{})
})
}
</script>

<script>
if('serviceWorker' in navigator) {
navigator.serviceWorker.register('/texteditor.service.js', { scope: '/' });
}
</script>

</body>
Expand Down
20 changes: 10 additions & 10 deletions texteditor.manifest.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"name": "Text Editor",
"short_name": "Text",
"start_url": "texteditor.html",
"display": "standalone",
"description": "A Simple Text Editor",
"start_url": "/",
"lang": "en-US",
"theme_color": "cornflowerblue",
"background_color": "lightblue",
"orientation": "portrait-primary",
"display": "standalone",
"icons": [
{
"src": "/texteditor.png",
"type": "image/png", "sizes": "72x72"
}
{
"src": "texteditor.png",
"sizes": "72x72",
"type": "image/png"
}
]
}
}
49 changes: 32 additions & 17 deletions texteditor.service.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,36 @@
const staticDevCoffee = "dev-coffee-site-v1";
const assets = [
"/texteditor.html",
"/texteditor.png"
];
const CACHE_NAME = `simple-texteditor`;

self.addEventListener("install", installEvent => {
installEvent.waitUntil(
caches.open(staticDevCoffee).then(cache => {
cache.addAll(assets)
})
)
// Use the install event to pre-cache all initial resources.
self.addEventListener('install', event => {
event.waitUntil((async () => {
const cache = await caches.open(CACHE_NAME);
cache.addAll([
'/',
'/texteditor.html',
'/texteditor.png'
]);
})());
});

self.addEventListener("fetch", fetchEvent => {
fetchEvent.respondWith(
caches.match(fetchEvent.request).then(res => {
return res || fetch(fetchEvent.request)
})
)
self.addEventListener('fetch', event => {
event.respondWith((async () => {
const cache = await caches.open(CACHE_NAME);

// Get the resource from the cache.
const cachedResponse = await cache.match(event.request);
if (cachedResponse) {
return cachedResponse;
} else {
try {
// If the resource was not in the cache, try the network.
const fetchResponse = await fetch(event.request);

// Save the resource in the cache and return it.
cache.put(event.request, fetchResponse.clone());
return fetchResponse;
} catch (e) {
// The network failed.
}
}
})());
});

0 comments on commit 86b6b9d

Please sign in to comment.