Service Worker Sample: Mock Responses

Available in Chrome 40+

This sample demonstrates basic service worker registration, with the service worker's fetch handler not taking any special action for most requests. However, there is special behavior if a request is made to the Google URL Shortener API and a custom request header is set, X-Mock-Response. (There's no special significance to this name; any convention that both the controlled page and the service worker agree on could be used.) Instead of returning actual responses for those API requests, the service worker will return mock response that should appear identical to a real API response from the perspective of this controlled page.

Visit chrome://inspect/#service-workers and click on the "inspect" link below the registered service worker to view logging statements for the various actions the service-worker.js script is performing.