Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logicola is now a PWA and works offline #97

Merged
merged 2 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Footer } from '@/components/footer';
import { CSPostHogProvider } from '@/components/providers';
import thumbnail from '../public/thumbnail.jpg';
import ExerciseNavbar from '@/components/mobile/exerciseNavbar';
import RegisterSW from '@/components/providers/service-worker';

const robotoFlex = Roboto_Flex({
subsets: ['latin'],
Expand All @@ -16,6 +17,7 @@ const robotoFlex = Roboto_Flex({

export const metadata: Metadata = {
metadataBase: new URL('https://logicola.org'),
themeColor: '#ffffff',
title: 'Logicola',
description:
'LogiCola is a program to help students learn logic. This is a web version of the original software built by the late Professor Harry Gensler.',
Expand Down Expand Up @@ -58,6 +60,10 @@ export default function RootLayout({
}>) {
return (
<html lang='en'>
<head>
<meta name='theme-color' content='#FFFFFF' />
<link rel='manifest' href='/manifest.json' />
</head>
<CSPostHogProvider>
<body
className={`antialiased min-h-screen bg-white text-primaryColor ${robotoFlex.className}`}
Expand All @@ -67,6 +73,7 @@ export default function RootLayout({

<Navbar />
<main className={`flex`}>{children}</main>
<RegisterSW />
<Footer />
</body>
</CSPostHogProvider>
Expand Down
16 changes: 16 additions & 0 deletions components/providers/service-worker.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// You could create a new component like RegisterSW.tsx
'use client';
import { useEffect } from 'react';

export default function RegisterSW() {
useEffect(() => {
if ('serviceWorker' in navigator) {
navigator.serviceWorker
.register('/sw.js')
.then((registration) => console.log('SW registered:', registration))
.catch((error) => console.log('SW registration failed:', error));
}
}, []);

return null;
}
Binary file added public/icon-192.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/icon-512.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions public/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "LogiCola",
"short_name": "LogiCola",
"description": "LogiCola helps you learn logic.",
"start_url": "/",
"display": "standalone",
"background_color": "#ffffff",
"theme_color": "#ffffff",
"orientation": "portrait",
"icons": [
{
"src": "/icon-192.png",
"sizes": "192x192",
"type": "image/png",
"purpose": "any maskable"
},
{
"src": "/icon-512.png",
"sizes": "512x512",
"type": "image/png"
}
]
}
28 changes: 28 additions & 0 deletions public/sw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const CACHE_NAME = 'logicola-cache-v1';
const urlsToCache = [
'/',
'/manifest.json',
'/icon.svg',
// Add other static assets like
'/globals.css',
// Add any other pages/routes you want to cache
];

self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(CACHE_NAME)
.then((cache) => {
return cache.addAll(urlsToCache);
})
);
});

self.addEventListener('fetch', (event) => {
event.respondWith(
caches.match(event.request)
.then((response) => {
// Return cached version or fetch from network
return response || fetch(event.request);
})
);
});
Loading