使用Workbox缓存外部下载

问题描述 投票:0回答:1

我正在使用gatsby-plugin-offline在一个GatsbyJS网站上工作,这个网站可以在example.com上找到,我想让我在example.com上链接到的PDF文件可以离线使用。这可能吗?

progressive-web-apps gatsby workbox
1个回答
1
投票

是的,这是可能的。我不是100%熟悉 gatsby-plugin-offline的配置,但它看起来像 https:/www.gatsbyjs.orgpackagesgatsby-plugin-offline#available-options 描述了将额外的服务工作者逻辑附加到其默认配置末尾的过程。

plugins: [{
  resolve: `gatsby-plugin-offline`,
  options: {
    appendScript: require.resolve(`src/custom-sw-code.js`),
  },
}]

然后在 src/custom-sw-code.js:

workbox.routing.registerRoute(
  ({url}) => url.pathname.endsWith('.pdf'),
  // Use StaleWhileRevalidate, CacheFirst, etc. as desired.
  new workbox.strategies.StaleWhileRevalidate({cacheName: 'pdfs'})
);
© www.soinside.com 2019 - 2024. All rights reserved.