justin.deal/dist/offline.html

253 lines
7.1 KiB
HTML
Raw Normal View History

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Offline | Justin Deal</title>
<style>
:root {
--color-bg: #fbf1c7;
--color-text: #3c3836;
--color-accent: #d65d0e;
--color-muted: #7c6f64;
--font-mono: 'IBM Plex Mono', monospace;
--font-display: 'Press Start 2P', monospace;
}
@media (prefers-color-scheme: dark) {
:root {
--color-bg: #282828;
--color-text: #ebdbb2;
--color-accent: #fe8019;
--color-muted: #a89984;
}
}
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
font-family: var(--font-mono);
background-color: var(--color-bg);
color: var(--color-text);
line-height: 1.6;
display: flex;
flex-direction: column;
min-height: 100vh;
padding: 2rem;
}
main {
flex: 1;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
text-align: center;
max-width: 800px;
margin: 0 auto;
}
h1 {
font-family: var(--font-display);
font-size: 2rem;
margin-bottom: 1rem;
color: var(--color-accent);
}
p {
margin-bottom: 1.5rem;
font-size: 1.1rem;
}
.offline-icon {
font-size: 4rem;
margin-bottom: 2rem;
animation: pulse 2s infinite;
}
.button {
display: inline-block;
background-color: var(--color-accent);
color: var(--color-bg);
padding: 0.75rem 1.5rem;
border-radius: 0.25rem;
text-decoration: none;
font-weight: bold;
transition: transform 0.2s, box-shadow 0.2s;
cursor: pointer;
margin-top: 1rem;
}
.button:hover {
transform: translateY(-2px);
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}
.cached-pages {
margin-top: 2rem;
text-align: left;
width: 100%;
max-width: 400px;
}
.cached-pages h2 {
font-size: 1.2rem;
margin-bottom: 1rem;
color: var(--color-accent);
}
.cached-pages ul {
list-style: none;
}
.cached-pages li {
margin-bottom: 0.5rem;
}
.cached-pages a {
color: var(--color-text);
text-decoration: none;
border-bottom: 1px solid var(--color-accent);
padding-bottom: 2px;
}
.cached-pages a:hover {
color: var(--color-accent);
}
@keyframes pulse {
0% {
opacity: 1;
}
50% {
opacity: 0.6;
}
100% {
opacity: 1;
}
}
</style>
</head>
<body>
<main>
<div class="offline-icon">📶</div>
<h1>You're Offline</h1>
<p>It looks like you've lost your internet connection. Some pages may still be available if you've visited them before.</p>
<button class="button" id="retry-button">Retry Connection</button>
<div class="cached-pages" id="cached-pages">
<h2>Available Pages</h2>
<p>Loading cached pages...</p>
<ul id="cached-pages-list"></ul>
</div>
</main>
<script>
// Check if we're actually offline
function checkConnection() {
return navigator.onLine;
}
// Update UI based on connection status
function updateConnectionStatus() {
if (checkConnection()) {
// We're back online, reload the page
window.location.reload();
} else {
// Still offline
document.querySelector('.offline-icon').textContent = '📶';
document.querySelector('h1').textContent = "You're Offline";
}
}
// Listen for online/offline events
window.addEventListener('online', updateConnectionStatus);
window.addEventListener('offline', updateConnectionStatus);
// Retry button
document.getElementById('retry-button').addEventListener('click', () => {
document.querySelector('.offline-icon').textContent = '🔄';
document.querySelector('h1').textContent = "Checking Connection...";
// Try to fetch the homepage
fetch('/')
.then(() => {
// If successful, we're online
window.location.reload();
})
.catch(() => {
// Still offline
updateConnectionStatus();
});
});
// List cached pages if service worker and caches are available
if ('caches' in window && 'serviceWorker' in navigator) {
caches.open('justin-deal-v1')
.then(cache => {
return cache.keys()
.then(requests => {
const cachedPagesList = document.getElementById('cached-pages-list');
if (requests.length === 0) {
document.getElementById('cached-pages').innerHTML = '<h2>No Cached Pages Available</h2><p>Try visiting some pages when you\'re back online.</p>';
return;
}
// Filter for HTML pages
const htmlRequests = requests.filter(request => {
const url = new URL(request.url);
return url.pathname === '/' ||
url.pathname.endsWith('.html') ||
!url.pathname.includes('.');
});
// Sort by URL
htmlRequests.sort((a, b) => {
return new URL(a.url).pathname.localeCompare(new URL(b.url).pathname);
});
// Create list items
const listItems = htmlRequests.map(request => {
const url = new URL(request.url);
let pageName = url.pathname === '/' ? 'Home' : url.pathname
.replace(/\/$/, '')
.replace(/^\//, '')
.replace(/\.html$/, '')
.split('/')
.pop()
.replace(/-/g, ' ');
// Capitalize first letter of each word
pageName = pageName
.split(' ')
.map(word => word.charAt(0).toUpperCase() + word.slice(1))
.join(' ');
return `<li><a href="${url.pathname}">${pageName}</a></li>`;
});
if (listItems.length === 0) {
document.getElementById('cached-pages').innerHTML = '<h2>No Cached Pages Available</h2><p>Try visiting some pages when you\'re back online.</p>';
return;
}
cachedPagesList.innerHTML = listItems.join('');
document.querySelector('#cached-pages p').style.display = 'none';
});
})
.catch(error => {
console.error('Error accessing cache:', error);
document.getElementById('cached-pages').innerHTML = '<h2>Could Not Access Cache</h2><p>There was an error accessing the cached pages.</p>';
});
} else {
document.getElementById('cached-pages').innerHTML = '<h2>Cache Not Available</h2><p>Your browser does not support caching or service workers.</p>';
}
</script>
</body>
</html>