javascript - 在 webpack 中注册一个 service worker
问题描述
我使用 webpack 来构建我的 React 应用程序。我不明白如何为我的服务人员指路。在index.js
文件中,我成功收到一条注册消息,但是我无法访问 service worker 的文件public/serviceWorker.js
WebPack 配置
plugins: [
new HtmlWebpackPlugin({
filename: 'index.html',
template: 'public/index.html',
favicon: 'public/favicon.ico'
}),
new MiniCssExtractPlugin({
filename: 'style.css'
}),
new WorkboxPlugin.GenerateSW({
swDest: path.join(__dirname, 'build/serviceWorker.js'),
clientsClaim: true,
skipWaiting: true
})
],
index.js
if ('serviceWorker' in navigator) {
window.addEventListener('load', () => {
navigator.serviceWorker.register('serviceWorker.js')
.then((registration) => {
registration.pushManager.subscribe({
userVisibleOnly: true,
applicationServerKey: convertedVapidKey
})
.then((subscription) => {
sendSubscriptionToServer(subscription)
})
.catch((error) => {
console.log(error)
})
})
.catch((error) => {
console.log(error);
})
})
}
解决方案
推荐阅读
- estimation - Vikings 团队中故事点估计的基线是什么?
- c# - 如何使用 IdentityUserRoles?
- javascript - Google Apps 脚本上的 google.script.run 问题
- webpack - 如何使用 npm/yarn 链接为 webpack 5 运行 HRM?
- x86-64 - AMD RAPL 功率域关系
- r - R:如何将几列分成相等等价的部分,减少列数,重新分配数据?
- python - 对进行 API 调用的函数的返回值进行 Pytest
- svg - 在交叉点减去 SVG HSLA 颜色
- c# - ICommand CanExecuteChanged 始终为空
- gitlab-ci - gitlab CI:在多个项目的管道上强制执行工作