- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在使用 Google 的 workbox library现在有一段时间,在非常基本的水平上。大部分效果很好,但由于某些原因,我的 manifest.json
中的 start_url
未在初始页面加载时缓存(由 Lighthouse 报告,在开发工具中确认) .如果您四处浏览一下,或者软刷新页面几次,它似乎确实进行了缓存,只是最初没有进行缓存。
我假设我做错了什么,但我不清楚那是什么。演示网址和源代码如下。
import { skipWaiting, clientsClaim } from "workbox-core";
import { ExpirationPlugin } from "workbox-expiration";
import { precacheAndRoute } from "workbox-precaching";
import { setCatchHandler, registerRoute } from "workbox-routing";
import { CacheFirst, NetworkFirst } from "workbox-strategies";
skipWaiting();
clientsClaim();
/**
* Cache WordPress content
*/
registerRoute(
({ url }) => {
return (url.pathname && !url.pathname.match(/^\/(wp-admin|wp-content|wp-includes|wp-json|wp-login)/) && url.pathname.match(/\/$/));
},
new NetworkFirst({
cacheName: "new_site-content-cache",
plugins: [
new ExpirationPlugin({
maxAgeSeconds: 7 * 24 * 60 * 60,
}),
],
})
);
/**
* Cache CSS files
*/
registerRoute(
({ url }) => {
return (url.pathname && url.pathname.match(/\.css$/) && !url.pathname.match(/wp-admin|wp-includes|wp-json/));
},
new CacheFirst({
cacheName: "new_site-css-cache",
plugins: [
new ExpirationPlugin({
maxAgeSeconds: 7 * 24 * 60 * 60,
}),
],
})
);
/**
* Cache JS files
*/
registerRoute(
({ url }) => {
return (url.pathname && url.pathname.match(/\.js$/) && !url.pathname.match(/wp-admin|wp-includes|wp-json/) && !url.pathname.match(/redirection/));
},
new CacheFirst({
cacheName: "new_site-js-cache",
plugins: [
new ExpirationPlugin({
maxAgeSeconds: 7 * 24 * 60 * 60,
}),
],
})
);
/**
* Cache image files
*/
registerRoute(
({ url }) => {
return (url.pathname && url.pathname.match(/\.gif|jpeg|jpg|png|svg|webp$/) && !url.pathname.match(/wp-admin|wp-includes|wp-json/));
},
new CacheFirst({
cacheName: "new_site-image-cache",
plugins: [
new ExpirationPlugin({
maxAgeSeconds: 7 * 24 * 60 * 60,
}),
],
})
);
/**
* Cache font files
*/
registerRoute(
({ url }) => {
return (url.pathname && url.pathname.match(/\.otf|ttf|woff|woff2$/) && !url.pathname.match(/wp-admin|wp-includes|wp-json/));
},
new CacheFirst({
cacheName: "new_site-font-cache",
plugins: [
new ExpirationPlugin({
maxAgeSeconds: 7 * 24 * 60 * 60,
}),
],
})
);
/**
* Return "offline" page when visiting pages offline that haven't been cached
*/
setCatchHandler(() => {
return caches.match("/offline/");
});
/**
* Precache "offline" page
*/
precacheAndRoute([
{
url: "/offline/",
revision: __VERSION__,
},
]);
更新 1: 好的,所以我找到了 this issue我认为这是问题所在。似乎初始响应未被缓存,因为服务 worker 是在请求之后安装的,因此它没有任何内容可缓存。我仍然没有找到解决此问题的方法,但如果我取得任何进展,我会发布另一个更新。
更新 2: 似乎使用 skipWaiting()
和 clientsClaim()
是 应该解决这个问题,但在添加这些之后,它仍然没有按预期工作。奇怪的是,Lighthouse 报告“当前页面在离线时响应 200”,而且“离线时 start_url 不响应 200The start_url 确实响应,但不是通过 service worker。”这没有意义,因为它是同一个页面......也许我在尝试路由 /
时做错了什么?
更新 3: 尝试将我的第一个 registerRoute
中的最后一次检查更改为 /\/?$/
,但没有任何区别。还尝试将 NetworkFirst
更改为 CacheFirst
并且实际上工作正常......但我需要它是 NetworkFirst
,所以这不是一个真正可行的选择来解决这个问题。我也尝试过预缓存 /
,但这同样没有任何区别。
更新 4: 我怀疑 this "warm the runtime cache" recipe可能接近我的需要,但我无法成功实现...如果我对 start_url
进行硬编码,它至少会缓存 HTML,但会遗漏所有其他文件。好像this method of gathering all resources可能有用,但是 location.href
只是返回 service worker URL,即使不是,我也不确定如何将所有内容拆分到它们自己的缓存中,就像我一样'目前正在做。
最佳答案
正如the method of gathering all resources表示,页面和资源未被缓存,因为它们是在安装 service worker 之前加载的。
为什么不使用 workbox-build
在构建时注入(inject)必要的 预缓存 list ? Assets 将在服务 worker 安装时预缓存。
快速示例,将以下行添加到 service-worker.js
中:
precacheAndRoute(self.__WB_MANIFEST);
构建完成后运行以下js脚本:
const {injectManifest} = require('workbox-build');
const swSrc = 'src/sw.js';
const swDest = 'build/sw.js';
injectManifest({
swSrc,
swDest,
globDirectory: 'build',
globPatterns: ['*.@(js|css)']
}).then(({count, size}) => {
console.log(`Generated ${swDest}, which will precache ${count} files, totaling ${size} bytes.`);
});
您甚至可以将 swSrc
和 swDest
设置为相同,以在适当的位置注入(inject) list 。
我认为没有必要将 Assets 拆分到不同的缓存中,但可以使用 getManifest
模式,一次glob一种 Assets ,手动注入(inject)到service-worker.js
关于caching - Workbox 在初始加载时不缓存 start_url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64702655/
我正在尝试为一个小型静态站点设置一个简单的 Service Worker,但出现 Service Worker 控制台错误:sw.js:59 Uncaught TypeError: workbox.e
我正在开发一个 pwa(带有类星体的 vue 应用程序),我真的很困惑,因为按照指示包含选项会导致我的服务 worker workbox.core.skipWaiting() 中的指令导致Servic
有人可以帮忙告诉我如何使用 workbox-build 缓存谷歌字体吗?我想在文件中包含要预缓存的 url,但这对我来说是不可能的。与普通的 service worker 不同,我所要做的就是在安装事
我正在玩工作箱,但出现以下错误: Skipping file 'app.js' due to size. [Max size supported is 2097152, this file is 47
我在 Wokbox 中遇到了一个似乎是 Sitecore 错误的问题。当“每页项目”下拉菜单设置为“全部”以外的任何内容时,我可以展开工作流程中的“草稿”和“等待批准”列表。 但是当“每页项目数”设置
我是服务人员的新手,并且正在使用Workbox来预缓存我的应用程序 shell 并缓存我的api数据。 Assets 的预缓存工作正常,正在创建和填充缓存。 在我第二次重新加载页面之前,运行时缓存不会
默认情况下 Sitecore 工作框显示项目名称,我想显示“标题”字段(自定义字段)。我怎样才能做到这一点? 最佳答案 我有一个类似的要求,客户需要 Workbox 来显示项目的“路径”而不是它的名称
在某些操作的 sitecore 工作箱中,我们可以通过关闭“禁止消息”属性来允许用户输入一些评论。是否有可能使它们成为必需? 最佳答案 我写了一篇关于为 发表评论的博客文章拒绝 命令是强制性的。您应该
我有一个 Sitecore 工作流程。当我使用项目旁边的“批准”按钮批准项目时,它会要求发表评论。当我使用“批准”(选定)或“批准”(全部)按钮批准多个项目时,它不会要求发表评论。这是为什么?我可以让
我正在使用 create-react-app 和 react-app-rewired-workbox 构建一个 React 应用程序使用工作箱。 我想使用 workbox 的 networkOnly
我想要一个数组来配置部分路径(当响应与请求的 URL 匹配时缓存响应)和过期时间(在 X 秒后使缓存过期),但我无法创建“处理程序” ' 才能正常工作。 这是配置路径和过期时间的数组: const P
我一直在使用 Google 的 workbox library现在有一段时间,在非常基本的水平上。大部分效果很好,但由于某些原因,我的 manifest.json 中的 start_url 未在初始页
我正在使用 Nuxt.js 框架,我的 WorkBox 配置如下所示 workbox: { workboxURL: 'https://cdn.jsdelivr.net/npm/workbox-
我希望使用 Workbox 缓存本地和远程图像 Assets 。目前是否支持,如果支持,如何支持? 基本上我想有以下功能: workboxBuild.injectManifest({ swSr
我已经实现了 Workbox 来使用 webpack 生成我的服务 worker 。 这很好用 - 我可以确认 revision运行时在生成的服务 worker 中更新 yarn run genera
我正在处理我的 PWA 应用程序。 所以我有一个问题,我找不到任何如何解决的信息。 我将工作箱与 webpack InjectManifest 一起使用(但也尝试过 webpack offline-p
如何在 WorkboxSW 中使用以下代码来注册所有每个缓存 URL 的路由。这个每个缓存的 url 包含 ajax,也会发送到服务器! $.ajax({ url : '/MyAPI/Recor
我正在尝试使用 Workbox 缓存整个页面,但它无法按照我的要求工作我只想缓存而不是 html 我想使用 Image、Js、Css 缓存整个页面目前它仅缓存 html workbox.routing
在 javascript webapp 的服务 worker 中使用 Workbox。想要清除所有内容的整个工作箱/应用程序缓存...基本上回到与第一次将应用程序加载到浏览器之前的状态尽可能相似的状态
通常每当我阅读有关 PWA 的博客文章时,该教程似乎只是预缓存每一个 Assets 。但这似乎有点违背应用程序外壳模式,据我了解,它是:缓存最基本的必需品(仅应用程序外壳),并在运行时缓存运行时。 (
我是一名优秀的程序员,十分优秀!