Skip to content

Commit

Permalink
PWA texteditor
Browse files Browse the repository at this point in the history
  • Loading branch information
bit-turtle authored Nov 1, 2024
1 parent 9078ba3 commit c206969
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 48 deletions.
21 changes: 19 additions & 2 deletions texteditor.html
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,25 @@
</script>

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

// Register the Service Worker
async function registerSW() {
if ('serviceWorker' in navigator) {
try {
await navigator
.serviceWorker
.register('texteditor.service.js');
}
catch (e) {
console.error("Failed To Register Service Worker");
}
}
else {
console.warn("Service Workers Are Not Supported");
}
}
</script>

Expand Down
Binary file added texteditor.icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 16 additions & 14 deletions texteditor.manifest.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
{
"name": "Text Editor",
"description": "A Simple Text Editor",
"start_url": "/",
"lang": "en-US",
"theme_color": "cornflowerblue",
"display": "standalone",
"icons": [
{
"src": "texteditor.png",
"sizes": "72x72",
"type": "image/png"
}
]
}
"name":"Text Editor",
"short_name":"TextEditor",
"start_url":"texteditor.html",
"display":"standalone",
"background_color":"lightblue",
"theme_color":"cornflowerblue",
"scope": ".",
"description":"A Simple Text Editor.",
"icons":[
{
"src":"texteditor.icon.png",
"sizes":"250x250",
"type":"image/png"
}
]
}
Binary file removed texteditor.png
Binary file not shown.
47 changes: 15 additions & 32 deletions texteditor.service.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,19 @@
const CACHE_NAME = `simple-texteditor`;
var staticCacheName = "texteditor";

// 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("install", function (e) {
e.waitUntil(
caches.open(staticCacheName).then(function (cache) {
return cache.addAll(["/", "/texteditor.html"]);
})
);
});

self.addEventListener('fetch', event => {
event.respondWith((async () => {
const cache = await caches.open(CACHE_NAME);
self.addEventListener("fetch", function (event) {
console.log(event.request.url);

// 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.
}
}
})());
});
event.respondWith(
caches.match(event.request).then(function (response) {
return response || fetch(event.request);
})
);
});

0 comments on commit c206969

Please sign in to comment.