简体   繁体   English

如何仅在 PWA 中而不在网站中显示更新弹出窗口?

[英]How to show an update popup only in PWA and not in website?

I have a Laravel PWA created from scratch.我有一个从头开始创建的 Laravel PWA。 I am using Laravel's Silviolleite PWA plugin.我正在使用 Laravel 的 Silviolleite PWA 插件。

I have created a service worker.我创建了一个服务人员。 Here is the code这是代码

> var staticCacheName = "pwa-v" + new Date().getTime(); var filesToCache
> = [
>     '/offline.html',
>     '/css/style.css',
>     '/js/app.js', ];
> 
> // Cache on install self.addEventListener("install", event => {
>     this.skipWaiting();
>     event.waitUntil(
>         caches.open(staticCacheName)
>             .then(cache => {
>                 return cache.addAll(filesToCache);
>             })
>     ) });
> 
> // Clear cache on activate self.addEventListener('activate', event =>
> {
>     event.waitUntil(
>         caches.keys().then(cacheNames => {
>             return Promise.all(
>                 cacheNames
>                     .filter(cacheName => (cacheName.startsWith("pwa-")))
>                     .filter(cacheName => (cacheName !== staticCacheName))
>                     .map(cacheName => caches.delete(cacheName))
>             );
>         })
>     ); });
> 
> // Serve from Cache self.addEventListener("fetch", event => {
>     event.respondWith(
>         caches.match(event.request)
>             .then(response => {
>                 return response || fetch(event.request);
>             })
>             .catch(() => {
>                 return caches.match('/offline.html');
>             })
>     ) });

Below is the code of Registering Service worker in meta.blade.php下面是在 meta.blade.php 中注册 Service Worker 的代码

<script type="text/javascript">
    // Initialize the service worker
    if ('serviceWorker' in navigator) {
        navigator.serviceWorker.register('/serviceworker.js', {
            scope: '.'
        }).then(function (registration) {
            // Registration was successful
            console.log('Laravel PWA: ServiceWorker registration successful with scope: ', registration.scope);
        }, function (err) {
            // registration failed :(
            console.log('Laravel PWA: ServiceWorker registration failed: ', err);
        });
    }
</script>

I wanted to have a popup only in a PWA (if possible).我只想在 PWA 中弹出一个窗口(如果可能的话)。 If not then on mobile website (when opened on the browser on mobile devices).如果没有,则在移动网站上(在移动设备上的浏览器上打开时)。 The pop up prompts the user to delete the current PWA and open the website as there is an update and add new PWA(Add to home screen).弹出窗口提示用户删除当前的 PWA 并打开网站,因为有更新并添加新的 PWA(添加到主屏幕)。 How can I do this?我怎样才能做到这一点?

If you only want to have a popup only in the PWA and not on desktop or mobile browser, you'll want to look in your manifest.json file.如果您只想在 PWA 中而不是在桌面或移动浏览器中显示弹出窗口,则需要查看 manifest.json 文件。

There should be a property called "display" that needs to be set equal to "standalone"应该有一个名为“display”的属性需要设置为“standalone”

"display": "standalone"

Confirm this is set correctly and now you can check if the user is using a standalone app (PWA) or not by doing the following:确认设置正确,现在您可以通过执行以下操作检查用户是否使用独立应用程序 (PWA):

if(navigator.standalone) {
    alert('Thanks for using our PWA!')
} else {
    alert('Please consider downloading our PWA.')
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM