DawnC commited on
Commit
d87f39c
·
1 Parent(s): 95c21cd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -15
app.py CHANGED
@@ -444,26 +444,36 @@ def main():
444
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
445
  <meta name="apple-mobile-web-app-capable" content="yes">
446
  <meta name="apple-mobile-web-app-title" content="PawMatch AI">
 
 
 
 
447
  <link rel="manifest" href="manifest.json">
448
  <link rel="apple-touch-icon" href="assets/icon-192.png">
449
  """)
450
-
451
- # 使用獨立的 JavaScript 區塊,避免格式化問題
452
  gr.HTML("""
453
  <script>
454
- // 確保代碼在頁面載入後執行
455
- document.addEventListener('DOMContentLoaded', function() {
456
- // 使用標準的 JavaScript 語法
457
- if ('serviceWorker' in navigator) {
458
- navigator.serviceWorker.register('service-worker.js')
459
- .then(function(registration) {
460
- console.log('Service Worker 註冊成功');
461
- })
462
- .catch(function(error) {
463
- console.log('Service Worker 註冊失敗:', error);
464
- });
465
- }
466
- });
 
 
 
 
 
 
467
  </script>
468
  """)
469
  # Header HTML
 
444
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
445
  <meta name="apple-mobile-web-app-capable" content="yes">
446
  <meta name="apple-mobile-web-app-title" content="PawMatch AI">
447
+ """)
448
+
449
+ # 2. PWA 資源載入(使用相對路徑)
450
+ gr.HTML("""
451
  <link rel="manifest" href="manifest.json">
452
  <link rel="apple-touch-icon" href="assets/icon-192.png">
453
  """)
454
+
455
+ # 3. 安全的 Service Worker 註冊
456
  gr.HTML("""
457
  <script>
458
+ (function() {
459
+ // 確保在正確的網域上運行
460
+ var currentDomain = window.location.hostname;
461
+ if (currentDomain.includes('hf.space')) {
462
+ // 等待頁面完全載入
463
+ window.addEventListener('load', function() {
464
+ if ('serviceWorker' in navigator) {
465
+ // 使用相對路徑
466
+ navigator.serviceWorker.register('service-worker.js')
467
+ .then(function(registration) {
468
+ console.log('Service Worker 註冊成功');
469
+ })
470
+ .catch(function(err) {
471
+ console.log('Service Worker 註冊失敗:', err);
472
+ });
473
+ }
474
+ });
475
+ }
476
+ })();
477
  </script>
478
  """)
479
  # Header HTML