From the course: JavaScript: Service Workers

Unlock the full course today

Join today to access over 22,700 courses taught by industry experts or purchase this course individually.

Load external files

Load external files - JavaScript Tutorial

From the course: JavaScript: Service Workers

Start my 1-month free trial

Load external files

- [Instructor] Let's load 02_06, Begin, and open serviceworker. In this case, we have all the code that so far is pretty small and only one file. But on a real project, we know that sometimes we don't want to write all the code in only one file. Or we might want to use additional third-party libraries that are targeting service workers. Fortunately for us, the service worker global scope has an importScript function. It a synchronous function receiving one or more URLs of additional JavaScripts that we want to load. So, for example, we can do events.js. I can create a new file here, I will call that file events.js, and inside that file, I'm going to move my two event handlers, pasting that part. So now my service worker has an additional file that is going to be downloaded and parsed at line 3. This is a synchronous API, which means if I have something on line 4, like console.log, that will be after loading the events.js script. Remember this is a synchronous API. If we want to load…

Contents