cache versions

This commit is contained in:
Andrey Kondratev
2025-11-10 14:27:58 +05:00
parent 712c25a881
commit f6b696a5f8
7 changed files with 242 additions and 5 deletions

View File

@@ -62,12 +62,115 @@ class QuixoticApp {
private hasMoreResults: boolean = false;
private isLoadingMore: boolean = false;
private scrollObserver: IntersectionObserver | null = null;
private currentVersion: string | null = null;
constructor() {
this.tg = (window as WindowWithTelegram).Telegram?.WebApp;
this.loadRecentSearches();
this.init();
this.bindEvents();
this.checkVersion(); // Check for updates on load
}
private async checkVersion(): Promise<void> {
try {
// Get current version from localStorage
const storedVersion = localStorage.getItem('appVersion');
// Fetch latest version from server
const response = await fetch('/api/version', {
cache: 'no-cache',
headers: {
'Cache-Control': 'no-cache',
'Pragma': 'no-cache'
}
});
if (!response.ok) return;
const versionData = await response.json();
const serverVersion = versionData.version;
this.currentVersion = serverVersion;
// If versions don't match, force reload
if (storedVersion && storedVersion !== serverVersion) {
console.log('🔄 New version detected, updating...');
// Clear cache and reload
if ('caches' in window) {
const cacheNames = await caches.keys();
await Promise.all(cacheNames.map(name => caches.delete(name)));
}
// Store new version
localStorage.setItem('appVersion', serverVersion);
// Force hard reload
window.location.reload();
return;
}
// Store version for future checks
if (!storedVersion) {
localStorage.setItem('appVersion', serverVersion);
}
// Periodically check for updates (every 5 minutes)
setInterval(() => this.silentVersionCheck(), 5 * 60 * 1000);
} catch (error) {
console.warn('Version check failed:', error);
}
}
private async silentVersionCheck(): Promise<void> {
try {
const response = await fetch('/api/version', {
cache: 'no-cache',
headers: {
'Cache-Control': 'no-cache',
'Pragma': 'no-cache'
}
});
if (!response.ok) return;
const versionData = await response.json();
const serverVersion = versionData.version;
if (this.currentVersion && this.currentVersion !== serverVersion) {
console.log('🔄 Update available');
// Show update notification
this.showUpdateNotification();
}
} catch (error) {
console.warn('Silent version check failed:', error);
}
}
private showUpdateNotification(): void {
const notification = document.createElement('div');
notification.className = 'tg-update-notification';
notification.innerHTML = `
<div class="tg-update-notification__content">
<span>Доступно обновление</span>
<button class="tg-update-notification__button">Обновить</button>
</div>
`;
const button = notification.querySelector('button');
button?.addEventListener('click', () => {
window.location.reload();
});
document.body.appendChild(notification);
// Auto-dismiss after 30 seconds
setTimeout(() => {
notification.remove();
}, 30000);
}
private triggerHaptic(type: 'light' | 'medium' | 'heavy' | 'success' | 'error' = 'light'): void {