-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update VSCode settings and add SW component
- Loading branch information
1 parent
27a5f42
commit 4c00e3a
Showing
6 changed files
with
113 additions
and
43 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
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
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,28 @@ | ||
import { useState } from "preact/hooks"; | ||
import { useToast } from "./useToast.ts" | ||
function SW() { | ||
const [loading, setLoading] = useState(false) | ||
const { showToast } = useToast() | ||
async function handleInstall() { | ||
const resp = await fetch("/sw-cache.json") | ||
const data = await resp.json() | ||
const keys = await caches.keys() | ||
for (const key of keys) { | ||
await caches.delete(key) | ||
} | ||
const cache = await caches.open("cache-v1") | ||
await cache.addAll(data) | ||
showToast({ | ||
msg: "تم تحميل الدروس بنجاح", | ||
type: "info", | ||
}) | ||
} | ||
return <div class="w-full py-3"> | ||
<button onClick={handleInstall} disabled={loading} class="w-full btn btn-primary"> | ||
{ | ||
loading ? <span class="loading loading-spinner loading-xs"></span> : "تحميل الدروس" | ||
} | ||
</button> | ||
</div>; | ||
} | ||
export default SW; |
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,32 @@ | ||
import { Head } from "$fresh/runtime.ts"; | ||
import Footer from "@/components/Footer.tsx"; | ||
|
||
export default function Offline() { | ||
return <> | ||
<Head> | ||
<title>الصفحه غير موجوده</title> | ||
</Head> | ||
<main class="flex flex-col h-full-minus-bar"> | ||
<div class="px-4 py-8 mx-auto grow"> | ||
<div class="max-w-screen-md mx-auto flex flex-col md:mt-8 items-center justify-center"> | ||
<img | ||
class="my-6" | ||
src="/logo.webp" | ||
width="128" | ||
height="128" | ||
title="لوغو نخلة جي اس" | ||
alt="Website logo" | ||
/> | ||
<h1 class="text-4xl font-bold">نعتذر منكم لعدم توفر الصفحة</h1> | ||
<p class="my-4"> | ||
أنت غير متصل بالانترنت، تأكد من اتصالك بالانترنت وحاول مرة اخرى. | ||
</p> | ||
<a title="العودى الى الصفحة الرئيسية" href="/" class="underline"> | ||
العودى الى الصفحة الرئيسية | ||
</a> | ||
</div> | ||
</div> | ||
<Footer /> | ||
</main> | ||
</> | ||
} |
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 |
---|---|---|
@@ -1,38 +1,49 @@ | ||
const CACHE_NAME = 'cache-' + Math.floor(Math.random() * 1000000); | ||
self.addEventListener('install', async function(event) { | ||
const cacheNames = await caches.keys(); | ||
await Promise.all( | ||
cacheNames.map(function(cacheName) { | ||
if (cacheName !== CACHE_NAME) { | ||
return caches.delete(cacheName); | ||
} | ||
}) | ||
); | ||
const resp = await fetch('/sw-cache.json'); | ||
const FILES_TO_CACHE = await resp.json(); | ||
console.log('Installing Service Worker...'); | ||
console.log('Caching Files...'); | ||
console.log(FILES_TO_CACHE.join('\n')); | ||
event.waitUntil( | ||
caches.open(CACHE_NAME).then(function(cache) { | ||
return cache.addAll(FILES_TO_CACHE); | ||
}) | ||
); | ||
const CACHE_NAME = 'cache-v1'; | ||
self.addEventListener('install', async function (event) { | ||
const cacheNames = await caches.keys(); | ||
await Promise.all( | ||
cacheNames.map(function (cacheName) { | ||
if (cacheName !== CACHE_NAME) { | ||
return caches.delete(cacheName); | ||
} | ||
}) | ||
); | ||
const resp = await fetch('/sw-cache.json'); | ||
const FILES_TO_CACHE = await resp.json(); | ||
caches.open(CACHE_NAME).then(function (cache) { | ||
return cache.addAll(FILES_TO_CACHE); | ||
}) | ||
|
||
}); | ||
self.addEventListener('activate', function(event) { | ||
|
||
}); | ||
self.addEventListener('fetch', function(event) { | ||
// console.log('Fetching:', event.request.url); | ||
event.respondWith( | ||
caches.match(event.request).then(function(response) { | ||
// I KNOW THIS IS NOT THE BEST WAY TO DO THIS | ||
if (!response) { | ||
caches.open(CACHE_NAME).then(function(cache) { | ||
cache.add(event.request.url); | ||
}); | ||
} | ||
return response || fetch(event.request); | ||
self.addEventListener('activate', function (event) { | ||
event.waitUntil( | ||
caches.keys().then(function (cacheNames) { | ||
return Promise.all( | ||
cacheNames.map(function (cacheName) { | ||
if (cacheName !== CACHE_NAME) { | ||
return caches.delete(cacheName); | ||
} | ||
}) | ||
); | ||
); | ||
}) | ||
); | ||
event.waitUntil( | ||
caches.open(CACHE_NAME).then(async function (cache) { | ||
const resp = await fetch('/sw-cache.json'); | ||
const FILES_TO_CACHE = await resp.json(); | ||
return cache.addAll(FILES_TO_CACHE); | ||
}) | ||
); | ||
}); | ||
self.addEventListener('fetch', function (event) { | ||
event.respondWith( | ||
caches.match(event.request).then(function (response) { | ||
if (!response) { | ||
caches.open(CACHE_NAME).then(function (cache) { | ||
cache.add(event.request.url); | ||
}); | ||
} | ||
return response || fetch(event.request); | ||
}) | ||
); | ||
}); |