DawnC commited on
Commit
a27c8a7
·
1 Parent(s): c19886e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -16
app.py CHANGED
@@ -443,25 +443,47 @@ def main():
443
  # Header HTML
444
 
445
  gr.HTML("""
446
- <head>
447
- <link rel="manifest" href="/manifest.json">
448
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
449
- <meta name="theme-color" content="#4299e1">
450
- <meta name="apple-mobile-web-app-capable" content="yes">
451
- <meta name="apple-mobile-web-app-status-bar-style" content="default">
452
- <meta name="apple-mobile-web-app-title" content="PawMatch">
453
- <link rel="apple-touch-icon" href="/assets/icon-192.png">
454
- </head>
455
  <script>
 
456
  if ('serviceWorker' in navigator) {
457
- window.addEventListener('load', () => {
458
- navigator.serviceWorker.register('/service-worker.js')
459
- .then((registration) => {
460
- console.log('ServiceWorker registration successful');
461
- })
462
- .catch((err) => {
463
- console.log('ServiceWorker registration failed: ', err);
 
 
 
 
 
 
 
 
 
464
  });
 
 
 
 
 
 
 
 
 
 
 
 
 
465
  });
466
  }
467
  </script>
 
443
  # Header HTML
444
 
445
  gr.HTML("""
446
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
447
+ <meta name="theme-color" content="#4299e1">
448
+ <meta name="apple-mobile-web-app-capable" content="yes">
449
+ <meta name="apple-mobile-web-app-status-bar-style" content="black">
450
+ <meta name="apple-mobile-web-app-title" content="PawMatch AI">
451
+ <link rel="manifest" href="manifest.json">
452
+ <link rel="apple-touch-icon" href="assets/icon-192.png">
453
+
 
454
  <script>
455
+ // 等待頁面完全載入後再註冊 Service Worker
456
  if ('serviceWorker' in navigator) {
457
+ // 使用相對路徑以確保在 Hugging Face Space 環境中正常運作
458
+ const swPath = 'service-worker.js';
459
+
460
+ window.addEventListener('load', async () => {
461
+ try {
462
+ const registration = await navigator.serviceWorker.register(swPath);
463
+ console.log('ServiceWorker 註冊成功!範圍是:', registration.scope);
464
+
465
+ // 監聽 Service Worker 狀態變化
466
+ registration.addEventListener('updatefound', () => {
467
+ const newWorker = registration.installing;
468
+ console.log('發現新的 Service Worker');
469
+
470
+ newWorker.addEventListener('statechange', () => {
471
+ console.log('Service Worker 狀態:', newWorker.state);
472
+ });
473
  });
474
+
475
+ } catch (error) {
476
+ console.error('ServiceWorker 註冊失敗:', error);
477
+ }
478
+ });
479
+
480
+ // 監聽離線/上線狀態
481
+ window.addEventListener('online', () => {
482
+ console.log('應用程式已連線');
483
+ });
484
+
485
+ window.addEventListener('offline', () => {
486
+ console.log('應用程式已離線');
487
  });
488
  }
489
  </script>