javascript - Workbox 运行时缓存不适用于带有查询参数的 url?
问题描述
我有一个像这样的公共 API 端点:
https://data.weather.gov.hk/weatherAPI/opendata/weather.php?dataType=warnsum&lang=en
我想用服务工作者运行时缓存它的响应。这就是我定义路线的方式:
registerRoute(
new RegExp('.+/weatherAPI/opendata/weather.php?dataType=warnsum&lang=en'),
new StaleWhileRevalidate({
cacheName: 'weatherApi',
plugins: [
new ExpirationPlugin({
maxAgeSeconds: 60 * 60 * 24
}),
new BackgroundSyncPlugin('getWeatherAPI', {
maxRetentionTime: 24 * 60 // Retry for max of 24 Hours (specified in minutes)
})
]
})
)
但是,这不起作用,并且此 url 的响应未保存到缓存中。我想知道查询参数是否会成为这里的问题,或者我是否遗漏了什么?
解决方案
RegExpRoute
在 Workbox 中执行匹配时会包含查询参数。
我认为您更有可能需要转义正则表达式中“特殊”的字符,例如?
.
使用正则表达式以外的东西实际上可能更容易。例如,您可以使用 a matchCallback
,例如:
registerRoute(
({url}) => url.href.endsWith('/weatherAPI/opendata/weather.php?dataType=warnsum&lang=en'),
new StaleWhileRevalidate({...}),
)
推荐阅读
- tensorflow - 变分自动编码器损失未显示正确?
- javascript - 如何在 JS/TS 中将数据设置为对象?
- c# - XML 文件被其他应用程序损坏
- java - 如何使用spring boot抛出自定义异常而不是500
- python - valueerror:无法将字符串转换为浮点熊猫
- imagemagick - 复合在 imagemagick7 与 imagemagick6 中不传输颜色
- firebase - 在 Firestore SDK 代码段中找不到 databaseURL
- php - Laravel - 变量为空但不为空?
- python - 如何按类别分组,然后使用 Pandas 计算单词的频率
- android - 当适配器正在生成视图并且用户在另一个片段上时,小部件为空