What I want is to convert my Aurelia app to PWA but not want to use PWA service worker to implement offline access because it will depend too much on PWA functionality but to implement offline access myself in Aurelia.
What I need is service worker in Aurelia or way to intercept all my networking requests/responses.
Dirty way is to call my custom class that will be one level of indirection between service calls and responses.
Is there some recommended approach for this in Aurelia or finished solution?
1 Like
The Aurelia http fetch client has an interceptor option see: https://aurelia.io/docs/plugins/http-services#aurelia-fetch-client
My knowledge on pwa config and service workers is limited but I thought you just use the API to write code for caching for example. Completely standalone, isolated, just plain js without the need of Aurelia.
1 Like
Thanks. I totally forgot about interceptors. Great advice. I want to avoid use PWA for caching or offline.
This way I am sure my app would work for long time.
PWA specifications might change, support for that might change however if I implement it directly in Aurelia I am sure I will not get into troubles later.
1 Like
Hi @radenkozec, if you truly want to control offline capability with a Service Worker, then you need to follow the specification for what makes a PWA. You would need to have a manifest that describes all of your dependencies. As far as writing a Service Worker, once, installed, you can intercept all fetch
requests simply by implementing your Service Worker. I have used it extensively in several apps where I have written my own Service Worker by hand to facilitate what I wanted it to do. I think writing your own Service Worker is an excellent exercise of understanding what happens and how it works with the rest of your application.
Hopefully, that helps.
1 Like
Hi Matt. I actually wanted to intercept fetch requests using Aurelia interceptors and write code that will behave like service worker to implement offline functionality.
However the problem I have is to detect offline/online which can be tricky but doable and to replay all offline requests.
Service workers do this in different thread from APP and I think I too complicated to build similar for my project.
I was hoping that there is some library that have all that.
I would probably just implement service worker as in standard PWA as you suggested.
UPDATE: I think I have found solution:
Workbox Webpack from Google
https://developers.google.com/web/tools/workbox/
1 Like
Hi @radenkozec, yes work box will work just fine. It utilizes Service Workers so you will still need to use them. I think that using Service Workers is actually a good thing and I think once you use it more, you will agree. Workbox is a very nice tool from Google and it should help you tremendously!
1 Like
Mind sharing a small example? I’ve never worked with it before
1 Like
Hi @zewa666, I now write all of my Service Worker code by hand but here is a link to an introductory video on Workbox. (It comes from the main page)…
3 Likes