0

I need to include some scripts in to my service worker. Normally we are appending scripts to DOM using document object. But in the case of service workers there is no DOM. So how can we do this using JS codes?

Thanks..

Saidh
  • 1,131
  • 1
  • 12
  • 21
  • Follow this URL to get your answer: http://stackoverflow.com/questions/950087/how-do-i-include-a-javascript-file-in-another-javascript-file – Sahadev Dec 16 '16 at 04:50

2 Answers2

1

Use the importScripts function. you can host your js files on an https domain and do something like this:

importScripts("//cdn.jsdelivr.net/pouchdb/5.3.1/pouchdb.min.js");
Dhruv Batheja
  • 2,140
  • 1
  • 18
  • 16
  • OK so I have done this but my service worker is still falling over when offline because these scripts are not being cached :( – Yvonne Aburrow Jun 13 '17 at 10:34
  • Your serviceworker shouldn't fail as these scripts are also cached. Can you share a public URL of your setup? – Dhruv Batheja Jun 14 '17 at 08:51
  • I have created a new question (which has ended up being a bit rambly) https://stackoverflow.com/questions/44496941/service-worker-to-save-form-data-when-browser-is-offline (my application is login only - but I will put some more detail in my question over there) - thank you – Yvonne Aburrow Jun 14 '17 at 09:56
0

There is a importScripts() method for that.

Xzandro
  • 957
  • 6
  • 14