gpt4 book ai didi

android - 安装的 pwa 不会离线启动

转载 作者:搜寻专家 更新时间:2023-11-01 09:21:55 25 4
gpt4 key购买 nike

我使用 webpack 离线插件制作了一个 PWA,配置如下:

// Put it in the end to capture all the HtmlWebpackPlugin's
// assets manipulations and do leak its manipulations to HtmlWebpackPlugin
new OfflinePlugin({
ServiceWorker: {
events: true,
},
relativePaths: false,
publicPath: '/',
appShell: '/',

// No need to cache .htaccess. See http://mxs.is/googmp,
// this is applied before any match in `caches` section
excludes: ['.htaccess'], // index.html

caches: {
main: [':rest:'],

// All chunks marked as `additional`, loaded after main section
// and do not prevent SW to install. Change to `optional` if
// do not want them to be preloaded at all (cached only when first loaded)
additional: ['*.chunk.js'],
},

// Removes warning for about `additional` section usage
safeToUseOptionalCaches: true,
autoUpdate: true,
}),

new WebpackPwaManifest({
name: 'my_app_name',
short_name: 'my_app_short_name',
description: 'my_app_description',
background_color: '#364150',
theme_color: '#b1624d',
icons: [
{
src: path.resolve('app/images/icon-512x512.png'),
sizes: [72, 96, 120, 128, 144, 152, 167, 180, 192, 384, 512],
},
],
}),

所以 service worker 工作了,我可以在 chrome devtools 上看到它。pwa 被 chrome 识别,当我导航到我的 url(由 heroku 在 https 中托管)时,chrome 提示在移动设备上安装建议。然后我将应用程序安装到我的安卓手机上,登录并像往常一样使用它。当我离线时,一切仍然有效,我可以在我的应用程序中导航,我可以将它最小化并重新打开,到目前为止一切顺利。当我关闭我的应用程序(使用任务管理器)时,我会脱机,然后打开它会提示白页或无连接提示。有小费吗?在成瘾中,它实际上是如何起作用的?每次我点击已安装的 pwa 时,它都会检查我是否有连接并下载(如果存在)更新版本的应用程序?

最佳答案

基于此link ,在处理获取事件时,您需要为根请求添加一个额外的条件。

self.addEventListener('fetch', function(event) {
// … either respond with the cached object or go ahead and fetch the actual URL
event.respondWith(
caches.match(event.request).then(function(response) {
if (response) {
// retrieve from cache
return response;
}

// if not found in cache, return default offline content (only if this is a navigation request)
if (event.request.mode === 'navigate') {
return caches.match('./index.html');
}

// fetch as normal
return fetch(event.request);
})
);
});

也来自这个thread , 有一个必需的 scope连接到 serviceworker 时需要在客户端 JavaScript 中指定的参数,前提是它未在根 ( /) 路径上运行。

关于android - 安装的 pwa 不会离线启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54361453/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com