Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
229 views
in Technique[技术] by (71.8m points)

angular - How to handle custom code in Ngsw - in Fetch event

I was having service worker implemented in angular.js application where we have some custom code written in self.addEventListener('fetch', function (event) . Now i am planning to migrate the application to angular, and i found in angular we have the special plugin which will do all configuration and cache cleanup. "@angular/service-worker": "~10.0.6",.

Now i am suffering to have the custom code implemented inside fetch event to integrate in angular version. Is there any way we can have the fetch method override in to a component and do the operation from there.

question from:https://stackoverflow.com/questions/65891723/how-to-handle-custom-code-in-ngsw-in-fetch-event

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Based on your message this steps were done using [email protected].

Edit service worker file.

  • Open the file <project_name>/node_modules/@angular/service-worker/ngsw-worker.js
  • Search handleFetch function Line 1121.
  • Edit adapt that function with your requirement. Check out handleFetchWithFreshness and handleFetchWithPerformance as examples.
  • Save

Patch file

  • Use patch-package to save patch.npx patch-package @angular/service-worker
  • Make a commit.
  • Add postinstall script to package.json
"scripts": {
 "postinstall": "patch-package"
}
  • Add patch-package package npm install --save-dev patch-package

Test service worker

  • ng build.
  • start a server of dist files.
  • it should works.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...