PWA Service Workers on Angular level?


Harald Schneider

Hi,

I am still experimenting with PWAs under iOS. Unfortunately, the App does not cache image resources, once the network connection is lost.

I tried to install the Corodva Service Workers plugin, but it throws an error during built - looks like it is outdated and not really usable anymore.

Would it be possible to implement Service Worker's Caching on Angular level?

https://angular.io/guide/service-worker-intro


DecSoft

Hello Harald,

I have no experience with service workers, however, I think we no need to do anything related with Angular, but with the Service Worker API. Are you try with that? Note that this is a more or less "draft" API and may don't work in all the browsers...



Harald Schneider

I digged further into it an successfully implemented the following scripts:

https://css-tricks.com/serviceworker-for-offline/

After this, the app runs fluently, even with a cut network connection. Here is the complete procedure:

Add this to AppOptions/HTML/CustomMetaData:

Copy service-worker-global.js to the www folder:

Copy service-worker.js also to the www folder

Edit the offlineFundamentals array in service-worker.js. It should contain all your views and all image resources. All other dependencies are cached automatically then:

If everything is OK, you can track the service worker's cache management in the console of e.g. Firefox Dev Edition:


DecSoft

Hello Harald,

Thanks for share that information here. Can be very interesting for PWA (Progressive Web Apps). I have a question for you... why you use the "AppOptions/HTML/CustomMetaData"? Is not better to use the app's files management? If we add an script... that script is linked properly in the app's index.html file...



Harald Schneider
The scripts have to reside in the www folder to work properly. When I add them via filemanager, they are copied to www/app/...

DecSoft

Hello Harald,

Yes; certainly the scripts are copied into the app's files folder... however, what we need to put that script in the "www" folder? Is there any reason? Of course, I mention this, because, in principle, I can't see a reason to need to copy an script in an specific folder... if the script is linked... then we can use their stuff... or I am missing something...



Harald Schneider
If you put them into www/app, the the the cached URLs are not correctly assembled. Putting them into www just saved me the time to modify the original script ;-)

DecSoft

Hello Harald,

If I am not wrong... we can use relative paths... so if our scripts are placed in "www/app/scripts", maybe we can use something like this:

Note that the above is correct if the script is placed in "www/app/scripts"... in other words, if you place the above code inside the "app.js" (in the app's Ready event, or a view's Show event), when maybe you can use the URLs like before... not in a relative way than the above.

Try it, Harald, if you can do it, then you no need to place files in specific directories, etc.



Harald Schneider

Hi David

The only thing that works, is moving service-worker-global.js via AB Filemanager to app/scripts

Caching breaks, when there are double dots in the offlineFundamentals paths. Furthermore, if we would link service-worker.js vial Filemanager, it would be executed twice: By the generated link in index.html and when the script is registered by the global script:

So at least service-worker.js has to reside in the www folder.


DecSoft

Hello Harald,

What if you change the "navigator.serviceWorker.register('service-worker.js')" for the right app's path? This means that, at least, you only need to touch that line of code?



Harald Schneider
I also tried that without success.

DecSoft

Hello Harald,

Then it's a bad thing... because the objective must be to no need to touch or take care about any file... I have no time right now in order to try it, Harald. Maybe I can do it in the future... because it's quite are... certainly, if an script refers to an specific file location... the questions must be why that specific location is used... and if we can do the job in other way, maybe moving that "require" to other file... maybe edit the script to refer to our file location...

I hope at least that, if there is not another way, you can get it working with not too much efforts...



Harald Schneider
It is OK for me. It is just 1 line more in the build script, which copies the file to the www folder. Thanks for your support!

DecSoft

So at least you can use another option in order to place the "copy" command, right? ;-)



Harald Schneider
Absolutely :-)

DecSoft

Hello Harald,

So... just to conclude... :-) Are you using the App options -> Cordova -> Batch or maybe the App options -> Build options?



Harald Schneider

Not in my case. I keep data on the Mac's side. Here I use a small script which fixes permissions and does the build and copy job.

If I would be a pure Windows user, I would prefer one of the other options. That's what I like on AB - it allows flexible solutions for most problems :-)


DecSoft

Hello Harald,

[...] That's what I like on AB - it allows flexible solutions for most problems :-)

Thanks for your kindly words, sir, I really appreciate it. :-)


Everybody can read the DecSoft support forum for learning purposes, however only DecSoft customers can post new threads. Purchase one or more licenses of some DecSoft products in order to give this and other benefits.

This website uses some useful cookies to store your preferences.

I agree. Hide this note. Give me more information.