mirror of
https://github.com/shadoll/sLogos.git
synced 2025-12-20 03:26:59 +00:00
feat: Add PWA support with service worker and caching
- Implemented service worker registration in main.js - Added icons for PWA in manifest.json - Created a basic service worker (sw.js) for caching static assets - Generated a list of files to cache using a Node.js script (generate-pwa-cache-list.js) - Added icon images (icon-192.png and icon-512.png) for PWA - Defined PWA manifest with app details and icon references
This commit is contained in:
45
scripts/generate-pwa-cache-list.js
Normal file
45
scripts/generate-pwa-cache-list.js
Normal file
@@ -0,0 +1,45 @@
|
||||
// Moved from project root to scripts directory for consistency
|
||||
// Node.js script to generate a list of all files in public, logos, and logos_gen for PWA caching
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
const projectRoot = path.join(__dirname, '..');
|
||||
const publicDir = path.join(projectRoot, 'public');
|
||||
const logosDir = path.join(projectRoot, 'logos');
|
||||
const logosGenDir = path.join(projectRoot, 'logos_gen');
|
||||
|
||||
// List of files to ignore
|
||||
const IGNORED_FILES = ['.DS_Store', 'CNAME', 'pwa-files-to-cache.json', '.gitignore'];
|
||||
|
||||
function walkDir(dir, baseUrl = '') {
|
||||
let results = [];
|
||||
fs.readdirSync(dir).forEach(file => {
|
||||
if (IGNORED_FILES.includes(file)) return; // Ignore listed files
|
||||
const filePath = path.join(dir, file);
|
||||
const relPath = path.join(baseUrl, file).replace(/\\/g, '/');
|
||||
if (fs.statSync(filePath).isDirectory()) {
|
||||
results = results.concat(walkDir(filePath, relPath));
|
||||
} else {
|
||||
results.push('/' + relPath);
|
||||
}
|
||||
});
|
||||
return results;
|
||||
}
|
||||
|
||||
function safeWalkDir(dir, baseUrl = '') {
|
||||
if (!fs.existsSync(dir)) return [];
|
||||
return walkDir(dir, baseUrl);
|
||||
}
|
||||
|
||||
const publicFiles = walkDir(publicDir, '').filter(f => !f.endsWith('sw.js'));
|
||||
const logosFiles = safeWalkDir(logosDir, 'logos');
|
||||
const logosGenFiles = safeWalkDir(logosGenDir, 'logos_gen');
|
||||
|
||||
const allFiles = Array.from(new Set([...publicFiles, ...logosFiles, ...logosGenFiles]));
|
||||
|
||||
fs.writeFileSync(
|
||||
path.join(publicDir, 'pwa-files-to-cache.json'),
|
||||
JSON.stringify(allFiles, null, 2)
|
||||
);
|
||||
|
||||
console.log('PWA files-to-cache list generated with', allFiles.length, 'files.');
|
||||
@@ -10,6 +10,8 @@ const srcSvgPath = path.join(__dirname, '../public/favicon.svg');
|
||||
const pngOutputPath = path.join(__dirname, '../public/apple-touch-icon.png');
|
||||
const icoOutputPath = path.join(__dirname, '../public/favicon.ico');
|
||||
const faviconPngPath = path.join(__dirname, '../public/favicon.png');
|
||||
const icon192Path = path.join(__dirname, '../public/icon-192.png');
|
||||
const icon512Path = path.join(__dirname, '../public/icon-512.png');
|
||||
|
||||
// Ensure the favicon.svg file exists
|
||||
if (!fs.existsSync(srcSvgPath)) {
|
||||
@@ -55,10 +57,20 @@ async function generateFavicons() {
|
||||
fs.copyFileSync(faviconPngPath, icoOutputPath);
|
||||
console.log(`Created ${icoOutputPath} (Note: This is actually a PNG file renamed to .ico)`);
|
||||
|
||||
// Generate icon-192.png for PWA
|
||||
const icon192 = await jimp.read(tempPngPath);
|
||||
await icon192.resize(192, 192).writeAsync(icon192Path);
|
||||
console.log(`Created ${icon192Path}`);
|
||||
|
||||
// Generate icon-512.png for PWA
|
||||
const icon512 = await jimp.read(tempPngPath);
|
||||
await icon512.resize(512, 512).writeAsync(icon512Path);
|
||||
console.log(`Created ${icon512Path}`);
|
||||
|
||||
// Clean up temporary file
|
||||
fs.unlinkSync(tempPngPath);
|
||||
|
||||
console.log('Favicon generation completed successfully!');
|
||||
console.log('Favicon and PWA icon generation completed successfully!');
|
||||
} catch (error) {
|
||||
console.error('Error generating favicons:', error);
|
||||
process.exit(1);
|
||||
|
||||
Reference in New Issue
Block a user