Fixing up offline page

This commit is contained in:
Brandon Rozek 2024-10-19 21:38:51 -04:00
parent 3161801ca5
commit 5f0441e629
No known key found for this signature in database
GPG key ID: 26E457DA82C9F480

View file

@ -8,17 +8,19 @@ hidden: true
Your device is offline. You may have some pages cached for offline viewing, Your device is offline. You may have some pages cached for offline viewing,
otherwise please wait for the internet to reconnect to continue browsing. otherwise please wait for the internet to reconnect to continue browsing.
[Homepage](/)
{{< unsafe >}} {{< unsafe >}}
<ul id='offline-posts'></ul> <ul id='offline-posts'>
<li><a href='/'>Homepage</a></li>
</ul>
<script> <script>
function daysAgo(date) { function daysAgo(date) {
date.setHours(0, 0, 0, 0); date.setHours(0, 0, 0, 0);
const time = date.getTime(); const time = date.getTime();
const today = new Date();
today.setHours(0, 0, 0, 0);
const now = today.getTime(); const now = today.getTime();
const delta = ((now - time) / 1000 / 60 / 60 / 24) | 0; const delta = ((now - time) / 1000 / 60 / 60 / 24) | 0;
@ -35,19 +37,18 @@ function daysAgo(date) {
async function listPages() { async function listPages() {
// Get a list of all of the caches for this origin // Get a list of all of the caches for this origin
const cacheNames = await caches.keys();
const result = []; const result = [];
const cache = await caches.open('v1::website');
for (const name of cacheNames) { // Get a list of entries. Each item is a Request object
// Open the cache for (const request of await cache.keys()) {
if (name.includes('/blog')) { const url = request.url;
const cache = await caches.open(name); if (url.includes('/blog')) {
console.log(url)
// Get a list of entries. Each item is a Request object const post = await cache.match(request);
for (const request of await cache.keys()) { if (post.headers.get('content-type') === 'text/html') {
const url = request.url;
const post = await cache.match(request);
const body = await post.text(); const body = await post.text();
console.log(body)
const title = body.match(/<title>(.*)<\/title>/)[1]; const title = body.match(/<title>(.*)<\/title>/)[1];
result.push({ result.push({
url, url,
@ -55,18 +56,14 @@ async function listPages() {
title, title,
visited: new Date(post.headers.get('date')), visited: new Date(post.headers.get('date')),
}); });
}
} }
}
} }
const el = document.querySelector('#offline-posts'); const el = document.querySelector('#offline-posts');
if (result.length) { if (result.length) {
el.innerHTML = result el.innerHTML = result
.sort((a, b) => {
return a.published.toJSON() < b.published.toJSON() ? 1 : -1;
})
.map((res) => { .map((res) => {
let html = `<li> let html = `<li>
<a href="${res.url}">${res.title}</a> <a href="${res.url}">${res.title}</a>