feat: add PWA support with service worker and caching

This commit is contained in:
Ashis Kumar Naik 2025-07-02 08:38:21 +05:30
parent 5a1ddd43ad
commit c4fc84f66c
4 changed files with 4595 additions and 53 deletions

4574
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -84,6 +84,7 @@
"tailwindcss": "^3.4.11",
"typescript": "^5.5.3",
"typescript-eslint": "^8.0.1",
"vite": "^5.4.1"
"vite": "^5.4.1",
"vite-plugin-pwa": "^0.21.1"
}
}

View File

@ -2,4 +2,17 @@ import { createRoot } from 'react-dom/client'
import App from './App.tsx'
import './index.css'
// Register service worker for PWA functionality
if ('serviceWorker' in navigator) {
window.addEventListener('load', () => {
navigator.serviceWorker.register('/sw.js', { scope: '/' })
.then((registration) => {
console.log('Service Worker registered successfully:', registration.scope);
})
.catch((error) => {
console.log('Service Worker registration failed:', error);
});
});
}
createRoot(document.getElementById("root")!).render(<App />);

View File

@ -2,6 +2,7 @@ import { defineConfig } from "vite";
import react from "@vitejs/plugin-react-swc";
import path from "path";
import { componentTagger } from "lovable-tagger";
import { VitePWA } from 'vite-plugin-pwa';
// https://vitejs.dev/config/
export default defineConfig(({ mode }) => ({
@ -13,6 +14,63 @@ export default defineConfig(({ mode }) => ({
react(),
mode === 'development' &&
componentTagger(),
VitePWA({
registerType: 'autoUpdate',
includeAssets: ['favicon.ico', 'robots.txt', 'placeholder.svg'],
manifest: {
name: 'OpChan - Ordinal Echo Chamber',
short_name: 'OpChan',
description: 'A decentralized forum powered by Waku with offline support',
theme_color: '#000000',
background_color: '#ffffff',
display: 'standalone',
scope: '/',
start_url: '/',
icons: [
{
src: 'favicon.ico',
sizes: '48x48',
type: 'image/x-icon'
},
{
src: 'placeholder.svg',
sizes: 'any',
type: 'image/svg+xml',
purpose: 'any maskable'
}
]
},
workbox: {
globPatterns: ['**/*.{js,css,html,ico,png,svg}'],
runtimeCaching: [
{
urlPattern: /^https:\/\/fonts\.googleapis\.com\/.*/i,
handler: 'CacheFirst',
options: {
cacheName: 'google-fonts-cache',
expiration: {
maxEntries: 10,
maxAgeSeconds: 60 * 60 * 24 * 365
}
}
},
{
urlPattern: /^https:\/\/fonts\.gstatic\.com\/.*/i,
handler: 'CacheFirst',
options: {
cacheName: 'gstatic-fonts-cache',
expiration: {
maxEntries: 10,
maxAgeSeconds: 60 * 60 * 24 * 365
}
}
}
]
},
devOptions: {
enabled: true
}
})
].filter(Boolean),
resolve: {
alias: {