Web Technology Basics
Which of the following correctly describes a Service Worker?
- AA program that keeps running on a web server and handles database queries in response to requests from browsers
- BA CSS feature for speeding up drawing by rewriting the page's DOM directly, which can also be used on pages that are not HTTPS
- CA script that runs inside the browser separately from the page, can intercept the page's requests and return stored responses, and is used for offline display
Show answer and explanation
Answer: C. A script that runs inside the browser separately from the page, can intercept the page's requests and return stored responses, and is used for offline display
A Service Worker is a script registered by a page that runs inside the browser separately from the page. It receives the requests made by pages of its origin (the fetch event) and can decide in script whether to send them to the network or to return a stored response. With it, pages can be shown even offline and push notifications can be received. Because it has the powerful ability to intercept communication, it can be used only on HTTPS pages (and on localhost for development), and it cannot touch the DOM directly.
- A. Application servers and the like run on servers; a Service Worker runs inside the browser.
- B. A Service Worker is a script written in JavaScript, and it cannot touch the DOM directly.
- C. Correct. It intercepts the page's requests and is used for offline display with stored responses and similar purposes.