ASP.NET Core 2.1-PWA(横幅提示被取消)

时间:2018-11-13 12:38:12

标签: javascript asp.net asp.net-core progressive-web-apps web-essentials

尝试使用ASP.NET Core 2.1和Nuget:WebEssentials.AspNetCore.PWA制作渐进式Web应用。

我的服务人员和清单显示在Chrome开发工具中,但是当我点击“添加到主屏幕”时,除了在计算机上显示的错误外,什么都没有发生,并且在手机上,顶部的加载横幅卡住了。

错误:

  

无法安装网站:该页面已要求将横幅提示   已取消

对于这个错误,我似乎找不到任何东西,因此希望你们能帮帮我。预先感谢。

ServiceWorker:

self.addEventListener('install', async event => {
    const cache = await caches.open(CACHE_NAME);
    cache.addAll(urlsToCache).catch(err => console.log('An error occured: ', err));
});

self.addEventListener('fetch', event => {
    const request = event.request;
    const url = new URL(request.URL);

    if (url.orgin === location.orgin) {
        event.respondWith(cacheFirst(request));
    } else {
        event.responseWith(networkFirst(request));
    }
    
});

async function cacheFirst(request) {
    const cachedResponse = await caches.match(request);
    return cachedResponse || fetch(request);
}

async function networkFirst(request) {
    const cache = await caches.open('wportal-dynamic-v1');

    try {
        const res = await fetch(request);
        cache.put(request, res.clone());
        return res;
    } catch (exception) {
        console.log('An error occured in networkFirst: ', exception);
        return await cache.match(request);
    }
}

1 个答案:

答案 0 :(得分:2)

找到了解决方案。

var deferredPrompt = null;                

window.addEventListener('beforeinstallprompt', (e) => {
    e.preventDefault(); // Prevent Chrome 67 and earlier from automatically showing the prompt
    deferredPrompt = e;
});
<button onclick="deferredPrompt.prompt();">Click me to install pwa</button>