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 source
我在尝试 workbox 和 angular 时遇到奇怪的错误。
一直在关注 workbox 上的指南和文档,也在寻找答案,但找不到适合我的解决方案。
无论如何,我得到这个错误
Error: Can't find self.__WB_MANIFEST in your SW source.
当我使用此代码创建 service-worker.js
时
服务-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"
}
最后我解决了它,它接缝是 @angular-builders/custom-webpack
和 @angular-devkit/build-angular
之间的版本差异破坏了构建。也许它会对其他人有所帮助。
"devDependencies": {
"@angular-builders/custom-webpack": "^12.1.2",
"@angular-devkit/build-angular": "~12.2.5",
"@angular-devkit/build-angular": "~12.1.2",
...
我在尝试 workbox 和 angular 时遇到奇怪的错误。
一直在关注 workbox 上的指南和文档,也在寻找答案,但找不到适合我的解决方案。
无论如何,我得到这个错误
Error: Can't find self.__WB_MANIFEST in your SW source.
当我使用此代码创建 service-worker.js
时
服务-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"
}
最后我解决了它,它接缝是 @angular-builders/custom-webpack
和 @angular-devkit/build-angular
之间的版本差异破坏了构建。也许它会对其他人有所帮助。
"devDependencies": { "@angular-builders/custom-webpack": "^12.1.2","@angular-devkit/build-angular": "~12.2.5","@angular-devkit/build-angular": "~12.1.2", ...