Workbox with Angular 12 Error: Can't find self.__WB_MANIFEST in your SW source
Posted
技术标签:
【中文标题】Workbox with Angular 12 Error: Can\'t find self.__WB_MANIFEST in your SW source【英文标题】:Workbox with Angular 12 Error: Can't find self.__WB_MANIFEST in your SW sourceWorkbox with Angular 12 Error: Can't find self.__WB_MANIFEST in your SW source 【发布时间】:2021-11-11 04:22:38 【问题描述】:我在尝试工作箱和角度时遇到了一个奇怪的错误。
一直在关注工作箱中的指南和文档,也一直在寻找答案,但找不到任何适合我的解决方案。
无论如何,我得到了这个错误
错误:在您的软件源中找不到 self.__WB_MANIFEST。
当我使用此代码创建 service-worker.js
时
service-worker.js
import precacheAndRoute from "workbox-precaching";
import registerRoute from "workbox-routing";
import StaleWhileRevalidate from "workbox-strategies";
import BroadcastUpdatePlugin from "workbox-broadcast-update";
// Use with precache injection
precacheAndRoute(self.__WB_MANIFEST);
registerRoute(
( url ) => url.pathname.startsWith("/"),
new StaleWhileRevalidate(
plugins: [new BroadcastUpdatePlugin()],
)
);
但如果我注释掉 StaleWhileRevalidate
的导入,它会起作用。
import precacheAndRoute from "workbox-precaching";
import registerRoute from "workbox-routing";
// import StaleWhileRevalidate from "workbox-strategies";
import BroadcastUpdatePlugin from "workbox-broadcast-update";
// Use with precache injection
precacheAndRoute(self.__WB_MANIFEST);
registerRoute(
( url ) => url.pathname.startsWith("/"),
new StaleWhileRevalidate(
plugins: [new BroadcastUpdatePlugin()],
)
);
有人知道为什么会这样吗?
我将采用inject manifest
方法。
webpack.config.js
const InjectManifest = require("workbox-webpack-plugin");
module.exports =
// Other webpack config...
plugins: [
// Other plugins...
new InjectManifest(
swSrc: "./src/service-worker.js",
),
],
;
package.json
"name": "angular-main",
"version": "0.0.0",
"scripts":
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"watch": "ng build --watch --configuration development",
"test": "ng test"
,
"private": true,
"dependencies":
"@angular/animations": "~12.2.0",
"@angular/common": "~12.2.0",
"@angular/compiler": "~12.2.0",
"@angular/core": "~12.2.0",
"@angular/forms": "~12.2.0",
"@angular/platform-browser": "~12.2.0",
"@angular/platform-browser-dynamic": "~12.2.0",
"@angular/router": "~12.2.0",
"rxjs": "~6.6.0",
"tslib": "^2.3.0",
"workbox-precaching": "^6.3.0",
"workbox-window": "^6.3.0",
"zone.js": "~0.11.4"
,
"devDependencies":
"@angular-builders/custom-webpack": "^12.1.2",
"@angular-devkit/build-angular": "~12.2.5",
"@angular/cli": "~12.2.5",
"@angular/compiler-cli": "~12.2.0",
"@types/jasmine": "~3.8.0",
"@types/node": "^12.11.1",
"jasmine-core": "~3.8.0",
"karma": "~6.3.0",
"karma-chrome-launcher": "~3.1.0",
"karma-coverage": "~2.0.3",
"karma-jasmine": "~4.0.0",
"karma-jasmine-html-reporter": "~1.7.0",
"typescript": "~4.3.5",
"workbox-webpack-plugin": "^6.3.0"
angular.json
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"version": 1,
"newProjectRoot": "projects",
"projects":
"angular-main":
"projectType": "application",
"schematics":
"@schematics/angular:component":
"style": "scss"
,
"@schematics/angular:application":
"strict": true
,
"root": "",
"sourceRoot": "src",
"prefix": "app",
"architect":
"build":
"builder": "@angular-builders/custom-webpack:browser",
"options":
"outputPath": "dist/angular-main",
"index": "src/index.html",
"main": "src/main.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "tsconfig.app.json",
"inlineStyleLanguage": "scss",
"customWebpackConfig": "path": "./webpack.config.js",
"assets": [
"src/favicon.ico",
"src/assets"
],
"styles": [
"src/styles.scss"
],
"scripts": []
,
"configurations":
"production":
"budgets": [
"type": "initial",
"maximumWarning": "500kb",
"maximumError": "1mb"
,
"type": "anyComponentStyle",
"maximumWarning": "2kb",
"maximumError": "4kb"
],
"fileReplacements": [
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
],
"outputHashing": "all"
,
"development":
"buildOptimizer": false,
"optimization": false,
"vendorChunk": true,
"extractLicenses": false,
"sourceMap": true,
"namedChunks": true
,
"defaultConfiguration": "production"
,
"serve":
"builder": "@angular-builders/custom-webpack:dev-server",
"configurations":
"production":
"browserTarget": "angular-main:build:production"
,
"development":
"browserTarget": "angular-main:build:development"
,
"defaultConfiguration": "development"
,
"extract-i18n":
"builder": "@angular-devkit/build-angular:extract-i18n",
"options":
"browserTarget": "angular-main:build"
,
"test":
"builder": "@angular-devkit/build-angular:karma",
"options":
"main": "src/test.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "tsconfig.spec.json",
"karmaConfig": "karma.conf.js",
"inlineStyleLanguage": "scss",
"assets": [
"src/favicon.ico",
"src/assets"
],
"styles": [
"src/styles.scss"
],
"scripts": []
,
"defaultProject": "angular-main"
【问题讨论】:
【参考方案1】:最后我解决了,@angular-builders/custom-webpack
和 @angular-devkit/build-angular
之间的版本差异似乎破坏了构建。也许它会帮助别人。
【讨论】:
以上是关于Workbox with Angular 12 Error: Can't find self.__WB_MANIFEST in your SW source的主要内容,如果未能解决你的问题,请参考以下文章
This version of CLI is only compatible with Angular versions
workbox-webpack-plugin.InjectManifest 的 sw.js 应该包括啥?
React如何通过Webpack优雅的接入serviceWorker的成熟方案workBox && Google Analytics