pwa-添加多个uri

时间:2019-09-20 14:10:17

标签: javascript symfony google-chrome progressive-web-apps

我使用Symfony并在应用程序中添加PWA ..在我的应用程序中使用webpack我有两个文件js front.js和admin.js ..其中两个文件启动服务工作程序,并创建预缓存文件js和CSS对于主页“ /”,我也在预缓存中添加了“ / admin”和“ / offline”。但是在脱机模式“ /”和“ / offline”中可以使用,但“ / admin”不可用! 这是manifest.json:

{
  "name" : "test",
  "short_name" : "test",
  "icons": [
    {
    "src": "img/icon72x72.png",
    "type": "image/png",
    "sizes": "72x72"
  },{
    "src": "img/icon96x96.png",
    "type": "image/png",
    "sizes": "96x96"
  },{
    "src": "img/icon144x144.png",
    "type": "image/png",
    "sizes": "144x144"
  },{
    "src": "img/icon-48x48.png",
    "type": "image/png",
    "sizes": "48x48"
  },{
    "src": "img/icon512x512.png",
    "type": "image/png",
    "sizes": "512x512"
  }, {
    "src": "img/icon-192x192.png",
    "type": "image/png",
    "sizes": "192x192"
  }],
  "theme_color" : "#1a1a1a",
  "background_color" : "#1a1a1a",
  "start_url" : "/",
  "display" : "standalone",
  "orientation" : "natural"
}

sw.js:

var CACHE = 'pwa';

        self.addEventListener('install', function(event) {
            event.waitUntil(
                caches.open(CACHE).then(function(cache) {
                    fetch('/get-all-files?url='+event.target.location.origin)
                        .then(
                            function(response) {
                                if (response.status !== 200) {
                                    console.log('Looks like there was a problem. Status Code: ' +
                                        response.status);
                                    return;
                                }
                                response.json().then(function(data) {
                                    data.forEach(function(element) {
                                        if(element){
                                            cache.add(element);
                                        }
                                    });
                                    cache.add('/');
                                    cache.add('/offline');
                                    cache.add('/build/front.css');
                                    cache.add('/build/images/hp-slider.ca2e5f82.jpg');
                                    cache.add('/build/images/background-commentcamarche.15892cd8.jpg');
                                    cache.add('/build/images/background-etapes.15c039e3.jpg');
                                    cache.add('/FrontOffice/images/logo-marypop-en.png');
                                    cache.add('/FrontOffice/images/logo-marypop-fr.png');
                                    cache.add('/FrontOffice/images/logo-marypop-de.png');
                                    cache.add('/admin/');
                                    return true;
                                });
                            }
                        );
                })
            );
        });

        self.addEventListener('activate', function(event) {
            event.waitUntil(
                caches.keys().then(function(CACHE) {
                    return Promise.all(
                        CACHE.filter(function(CACHE) {
                            // Return true if you want to remove this cache,
                            // but remember that caches are shared across
                            // the whole origin
                        }).map(function(CACHE) {
                            return caches.delete(CACHE);
                        })
                    );
                })
            );
        });

        self.addEventListener('fetch', function(event) {
            event.respondWith(
                caches.match(event.request)
                    .then(function(response) {
                        // Cache hit - return response
                        if (response) {
                            return response;
                        }
                        return fetch(event.request).then(
                            function(response) {
                                // Check if we received a valid response
                                if(!response || response.status !== 200 || response.type !== 'basic') {
                                    return response;
                                }
                                var responseToCache = response.clone();
                                caches.open(CACHE)
                                    .then(function(cache) {
                                        cache.put(event.request, responseToCache);
                                    });

                                return response;
                            }
                        ).catch(function(err) {
                            return caches.open(CACHE)
                                .then(function(cache) {
                                    return cache.match('/offline');
                                });
                        });
                    })
            );
        });

当我在预缓存中添加'/'时,如何添加'/admin''/register'(仪表盘的一部分到预缓存)也可以在脱机模式下工作

0 个答案:

没有答案