Virtual Server

This recipe shows a service worker acting like a remote server.

Difficulty

Intermediate

Use Case

As an application developer, I want to to fully decouple UI from business logic.

Solution

With REST APIs you can decouple client from business logic. The business logic is actually a separated component placed on a remote server. With Service Workers you can do the same. Simply move your business logic to a Service Worker responding on fetch events.

Instead of implementing your own logic to distinguish between routes and request methods, use ServiceWorkerWare or sw-toolbox‘ router feature to write your worker in a declarative way.

The client code is virtually identical to that in the API analytics recipe (the report link has been removed). On the contrary, the remote Express server has been completely replaced by the ServiceWorkerWare worker.

Category

Beyond Offline


Has it been useful?

Tell us what you think of this recipe by leaving a comment!