简体   繁体   English

pwa-提示添加到主屏幕未显示

[英]pwa - prompt add to home screen dosen't show

I develop a web application with symfony 4.3, then I add pwa features to it. 我使用symfony 4.3开发了一个Web应用程序,然后向其中添加了pwa功能。 I test with lighthouse extension in chrome and this is result: 我测试了铬的灯塔扩展,这是结果:

在此处输入图片说明

Now problem is prompt for add icon to home screen dosen't show and I have this error : 现在问题提示没有显示向主屏幕添加图标,并且我遇到此错误:

Uncaught TypeError: Cannot read property 'prompt' of undefined 未捕获的TypeError:无法读取未定义的属性“提示”

code js: 代码js:

var deferredPrompt ;
var btnAdd = document.getElementById('butInstall') ;

function launchPromptPwa(){
    var deferredPrompt;

    btnAdd = document.getElementById('butInstall') ;

    window.addEventListener('beforeinstallprompt',  (e) => {
        console.log('0');

        // Prevent Chrome 67 and earlier from automatically showing the prompt
        e.preventDefault();
        // Stash the event so it can be triggered later.
        deferredPrompt = e;
        btnAdd.style.display = block;
        showAddToHomeScreen();
    });

    btnAdd.addEventListener('click', (e) => {
        console.log('1');
        //btnAdd.style.display = 'none';
        //Show the prompt
        deferredPrompt.prompt();
        // Wait for the user to respond to the prompt
        deferredPrompt.userChoice
            .then((choiceResult) => {
                if (choiceResult.outcome === 'accepted') {
                    console.log('User accepted the A2HS prompt');
                } else {
                    console.log('User dismissed the A2HS prompt');
                }
                deferredPrompt = null;
            });
    });


    window.addEventListener('appinstalled', (evt) => {
        console.log('a2hs installed');
    });

    if (window.matchMedia('(display-mode: standalone)').matches) {
        console.log('display-mode is standalone');
    }

}

I test for the display prompt in chrome. 我测试了chrome的显示提示。

To avoid the error you can test first whether the deferredPrompt variable is initialised and skip the code logic if undefined: 为了避免该错误,您可以首先测试deferredPrompt变量是否已初始化,如果未定义,则跳过代码逻辑:

if (deferredPrompt) {

  deferredPrompt.prompt();

 // ... 
}

Then, is the beforeinstallprompt event triggered? 然后,会触发beforeinstallprompt事件吗?
If so, you have to proof if the event object is defined, as you use it to initialise your variable: 如果是这样,则在使用它初始化变量时,必须证明是否定义了event对象:

deferredPrompt = e;

Keep in mind that you need a running service worker in order to let the beforeinstallprompt event being triggered. 请记住,您需要一个正在运行的service worker ,以便触发beforeinstallprompt事件。 And the service worker needs a secure connection (https) or running localhost and served via web server. 并且服务工作者需要安全连接(https)或运行localhost并通过Web服务器进行服务。

You can open Chrome Dev Tools (F12) and access the "Application" tab to verify that a web manifest is correctly set and a service worker is installed. 您可以打开Chrome开发工具(F12)并访问“应用程序”标签,以验证是否正确设置了Web清单以及是否安装了Service Worker。

I wrote some articles about service workers, caching strategies and PWAs if you are interested in deepening the topic. 如果您有兴趣加深该主题我写了一些有关服务工作者,缓存策略和PWA的文章。


UPDATE UPDATE
If you want to serve content offline, you have to implement caching strategies for your service worker (eg. Stale while revalidate). 如果要脱机提供内容,则必须为服务工作者实施缓存策略(例如,重新验证时失效)。 Following the link above you can learn about different strategies and how you can implement them. 通过上面的链接,您可以了解不同的策略以及如何实施它们。

When you implement caching strategies, all the static assets (like css or js files) or data requests will be intercept by the service worker and if there is a match with the given rules, it will cache them or provide them from the cache. 当您实施缓存策略时,所有静态资产(如css或js文件)或数据请求都将被服务工作者拦截,并且如果与给定规则匹配,它将缓存它们或从缓存中提供它们。 Since the cache is on the client side, those resources are available also offline. 由于缓存位于客户端,因此这些资源也可以脱机使用。

As example, to cache static assets: 例如,要缓存静态资产:

 self.addEventListener('install', function(event) { event.waitUntil( caches.open(cacheName).then(function(cache) { return cache.addAll( [ '/css/bootstrap.css', '/css/main.css', '/js/bootstrap.min.js', '/js/jquery.min.js', '/offline.html' // Add anything else you need to be cached during the SW install ] ); }) ); }); 

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

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