简体   繁体   English

如何在 Ngsw 中处理自定义代码 - 在 Fetch 事件中

[英]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", . 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将执行所有配置和缓存清理的特殊插件。 "@angular/service-worker": "~10.0.6", .

Now i am suffering to have the custom code implemented inside fetch event to integrate in angular version.现在我很难在 fetch 事件中实现自定义代码以集成到 angular 版本中。 Is there any way we can have the fetch method override in to a component and do the operation from there.有什么方法可以让 fetch 方法覆盖到组件中并从那里执行操作。

Based on your message this steps were done using angular-cli@10.0.6 .根据您的消息,这些步骤是使用angular-cli@10.0.6完成的。

Edit service worker file.编辑服务工作人员文件。

  • Open the file <project_name>/node_modules/@angular/service-worker/ngsw-worker.js打开文件<project_name>/node_modules/@angular/service-worker/ngsw-worker.js
  • Search handleFetch function Line 1121.搜索handleFetch function第 1121 行。
  • Edit adapt that function with your requirement.编辑根据您的要求调整 function。 Check out handleFetchWithFreshness and handleFetchWithPerformance as examples.查看handleFetchWithFreshnesshandleFetchWithPerformance作为示例。
  • Save节省

Patch file补丁文件

  • Use patch-package to save patch.使用patch-package保存补丁。 npx patch-package @angular/service-worker
  • Make a commit.做出承诺。
  • Add postinstall script to package.jsonpostinstall script添加到package.json
"scripts": {
 "postinstall": "patch-package"
}
  • Add patch-package package npm install --save-dev patch-package添加patch-package package npm install --save-dev patch-package

Test service worker测试服务人员

  • ng build.构建。
  • start a server of dist files.启动 dist 文件的服务器。
  • it should works.它应该有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何处理PrimeNG p-fullCalendar的自定义按钮单击事件? - How to handle custom button click event for PrimeNG p-fullCalendar? 如何处理事件,请单击Angular 2 Rc1中Ckeditor工具栏上的自定义按钮? - how to handle event click the custom button on toolbar of Ckeditor in Angular 2 Rc1? Angular 5 和 Service Worker:如何从 ngsw-config.json 中排除特定路径 - Angular 5 and Service Worker: How to exclude a particular path from ngsw-config.json 如何处理 Angular 应用程序中的右键单击事件? - How to handle right click event in Angular app? 如何处理 Angular 中的剑道网格复选框事件 - How to handle Kendo grid checkbox event in Angular 我如何捕获每次输入键时ctrl + f发出的事件,以处理Angular 4页面上的自定义搜索 - How can I grab a event that is emitted by ctrl + f every time an key is input , to handle custom search on page in Angular 4 如何在Fetch Api中分别处理返回不同的http状态码? - How to handle returned different http status codes separately in Fetch Api? 如何在 Angular 4 中处理窗口滚动事件? - How to handle window scroll event in Angular 4? 如何处理 angular 中的属性指令 output 事件 - How to handle attribute directive output event in angular 如何在 angular 中触发自定义事件 - how to fire custom event in angular
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM