progressive-web-apps - 带有 webpack.js 的渐进式网络应用程序
问题描述
在 webpack.js 版本 5 中,我想使用 PWA,根据他们的 webpack 文档: https ://webpack.js.org/guides/progressive-web-application/ 我被告知使用 Workbox,但 Workbox 仅用于离线模式,比如如果我想使用Notifications、IndexedDB等其他功能,我应该在webpack.config.js中添加什么代码以及如何使用manifest.json和webpack?
解决方案
假设您想要https://webpack.js.org/guides/progressive-web-application/ 中描述的预缓存行为并且您想要自定义您的服务工作者,最干净的方法是使用, 并将路径传递给您的自定义服务工作者文件作为选项。InjectManifest
workbox-webpack-plugin
swSrc
该InjectManifest
插件将负责捆绑您的swSrc
文件,并将self.__WB_MANIFEST
根据您的webpack
编译资产替换为预缓存清单。
// webpack.config.js
const path = require('path');
const {InjectManifest} = require('workbox-webpack-plugin');
module.exports = {
// Your other webpack config goes here.
plugins: [
new InjectManifest({
swSrc: './service-worker.js',
swDest: 'service-worker.js',
// Any other config if needed.
}),
],
};
// service-worker.js
import {precacheAndRoute} from 'workbox-precaching';
// Add in additional imports here as needed.
// This line of code ensures that everything in your webpack
// compilation gets precache and served.
precacheAndRoute(self.__WB_MANIFEST);
// Your custom service worker code goes here.
推荐阅读
- javascript - Chrome扩展程序:防止点击内容脚本
- python - 数组中相同列表的出现次数
- docker - How to use Docker Host Service Connection in Azure Pipelines
- python - matplotlib 条形图 - 只有大小为 1 的数组可以转换为 Python 标量
- c++ - 如何在 Qt 中合并两个 QJsonObjects?
- error-handling - 尝试从文本文件中提取特定数据行并创建新的输出文本文件
- r - 如何对数据框的所有行值执行基于向量的函数
- javascript - 如果悬停并超出边界,如何停止调整 chart.js 的大小?
- python - Visual Studio Python 项目不符合最新语法
- python - 如何使用 Altair 从 transform_regression 标记线?