【发布时间】:2020-12-08 03:26:23
【问题描述】:
我刚刚开始使用 next-offline 并找到有关工作箱集成及其配方的部分。
根据文档:
如果您不熟悉 Workbox,我建议您阅读此快速指南 -- workboxOpts 中的任何内容都将传递给 workbox-webpack-plugin.
在你的 next.config.js 中定义一个 workboxOpts 对象,它会得到 传递给 workbox-webpack-plugin。 Workbox 是 next-offline 使用的 在幕后生成服务工作者,您可以了解更多信息 在这里。
在四处挖掘之后,我发现了这个很棒的section。
基本上它给出了使用两种不同选项的建议:
我想使用 InjectManifest,但是当我尝试在我的 next.config.js 文件中实现它时。我收到此错误:
"runtimeCaching" is not a supported parameter.
这是我的 next.config.js:
const withCSS = require('@zeit/next-css');
const withSass = require('@zeit/next-sass');
const withImages = require('next-images');
const optimizedImages = require('next-optimized-images');
const withOffline = require('next-offline');
module.exports = withOffline(
withImages(
optimizedImages(
withCSS(
withSass({
// useFileSystemPublicRoutes: false,
// generateSw: false, // this allows all your workboxOpts to be passed in injectManifest
generateInDevMode: true,
workboxOpts: {
swDest: './service-worker.js', // this is the important part,
exclude: [/.+error\.js$/, /\.map$/, /\.(?:png|jpg|jpeg|svg)$/],
runtimeCaching: [
{
urlPattern: /\.(?:png|jpg|jpeg|svg)$/,
handler: 'CacheFirst',
options: {
cacheName: 'hillfinder-images'
}
},
{
urlPattern: /^https?.*/,
handler: 'NetworkFirst',
options: {
cacheName: 'hillfinder-https-calls',
networkTimeoutSeconds: 15,
expiration: {
maxEntries: 150,
maxAgeSeconds: 30 * 24 * 60 * 60 // 1 month
},
cacheableResponse: {
statuses: [0, 200]
}
}
}
]
},
dontAutoRegisterSw: false,
env: {
MAPBOX_ACCESS_TOKEN: process.env.MAPBOX_ACCESS_TOKEN,
useFileSystemPublicRoutes: false
},
webpack(config, options) {
config.module.rules.push({
test: /\.(png|jpg|gif|svg|eot|ttf|woff|woff2)$/,
use: {
loader: 'url-loader',
options: {
limit: 100000,
target: 'serverless'
}
}
});
return config;
}
})
)
)
)
);
当我检查 Application 窗格时,在 devTools 中我看到了这个:
您会注意到在我看来重复的字段,即 https-calls 和 hillfinder-https-calls 和 images 和 hillfinder-images。
我认为每个 options: {} 中的 cacheName 字段允许包含自定义名称?
只是想知道是否有人有过设置的经验?
提前谢谢你!
【问题讨论】:
标签: next.js workbox workbox-webpack-plugin