Skip to content
This repository has been archived by the owner on Nov 11, 2022. It is now read-only.

Commit

Permalink
neylify functions requests cache
Browse files Browse the repository at this point in the history
  • Loading branch information
mayneyao committed Jun 24, 2019
1 parent b4fbb37 commit 54ab729
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
35 changes: 35 additions & 0 deletions config.js
Expand Up @@ -135,6 +135,41 @@ module.exports = {
theme_color: `#ffffff`,
display: `standalone`,
icon: `src/static/41546411364_.pic.jpg`, // This path is relative to the root of the site.
},
// 配置参见 gatsby-plugin-offline 插件文档
swConf: {
importWorkboxFrom: `local`,
globDirectory: rootDir,
globPatterns,
modifyUrlPrefix: {
// If `pathPrefix` is configured by user, we should replace
// the default prefix with `pathPrefix`.
"/": `${pathPrefix}/`,
},
cacheId: `gatsby-plugin-offline`,
// Don't cache-bust JS or CSS files, and anything in the static directory,
// since these files have unique URLs and their contents will never change
dontCacheBustUrlsMatching: /(\.js$|\.css$|static\/)/,
runtimeCaching: [
{
// Use cacheFirst since these don't need to be revalidated (same RegExp
// and same reason as above)
urlPattern: /(\.js$|\.css$|static\/)/,
handler: `cacheFirst`,
},
{
// Add runtime caching of various other page resources
urlPattern: /^https?:.*\.(png|jpg|jpeg|webp|svg|gif|tiff|js|woff|woff2|json|css)$/,
handler: `staleWhileRevalidate`,
},
{
// Google Fonts CSS (doesn't end in .css so we need to specify it)
urlPattern: /^https?:\/\/fonts\.googleapis\.com\/css/,
handler: `staleWhileRevalidate`,
},
],
skipWaiting: true,
clientsClaim: true,
}
},
rss: {
Expand Down
6 changes: 5 additions & 1 deletion gatsby-browser.js
Expand Up @@ -18,4 +18,8 @@ export const onServiceWorkerUpdateFound = () => {
if (answer === true) {
window.location.reload()
}
}
}

// 注册自定义 sw
// 参见:https://www.gatsbyjs.org/docs/add-offline-support-with-a-service-worker/#using-a-custom-service-worker-in-gatsby
// export const registerServiceWorker = () => true
19 changes: 18 additions & 1 deletion gatsby-config.js
@@ -1,10 +1,10 @@

const config = require('./config.js')


conf = {
siteMetadata: config.blogMeta,
plugins: [
`gatsby-plugin-offline`,
`gatsby-plugin-react-helmet`,
],
}
Expand Down Expand Up @@ -63,10 +63,27 @@ if (config.sitemap.open) {
}

if (config.pwa.open) {

// 安装应用
conf.plugins.push({
resolve: `gatsby-plugin-manifest`,
options: config.pwa.conf,
})

// sw 重载
let swConf = config.pwa.swConf

// 对基于 nelify functions 动态页面,缓存其请求
swConf.runtimeCaching.push({
// Add runtime caching of various other page resources
urlPattern: /^https?:.*\/.netlify\/functions\/notion/,
handler: `staleWhileRevalidate`,
})

conf.plugins.push({
resolve: `gatsby-plugin-offline`,
options: swConf,
})
}

if (config.rss.open) {
Expand Down

0 comments on commit 54ab729

Please sign in to comment.