DEV Community

Cover image for Service Workers at Cloudflare
K
K

Posted on

Service Workers at Cloudflare

Cover image by Christopher on Flickr

Cloudflare has a new product, called Workers, they allow you to run JavaScript code on the Edge which is basically the outta rim of the Cloud. Cool thing is, it's based on a standard: Service Workers!

Why

Sometimes you want to do things with your Web application that isn't directly part of it. Like modifying requests or responses or caching. And sometimes you want to do this and prevent clients from accessing these modifying code.

What

A service worker is mostly transparent. It can for example intercept a fetch event and modify it somehow. Setting different headers, getting some of the content from a cache or different servers, etc.

But they run on the client.

Cloudflare Workers run on the Edge of the Cloudflare infrastructure, right between your clients and your servers, but they have the same API as Service Workers.

This means, you can write a service Worker that runs on your dev client, but won't run on your users clients, because you have a clean interface between the worker and the app.

This can be used to write static front-ends and route the requests through the Worker, for example to add API secrets that would normally need to set up a whole server. Since it runs on the Edge the code is in geographical proximity to the client and not the server, which reduces latency.

Conclusion

Cloudflare Workers seem to be a nice alternative to solutions like AWS Lambda. While Lambda@Edge is a similar offering, the Workers adhere to the Service Worker standard, which could bring benefits on porting it to another provider or moving code between client and Edge when needed.

All in all, they give more power to the front-end and allow for less infrastructure.

Top comments (8)

Collapse
 
ben profile image
Ben Halpern

Conceptually this is similar to how we use Fastly, using Varnish Configuration Language. Though I do appreciate the concept of using the same API at the edge compute node as well as on the browser.

Collapse
 
kayis profile image
K

Interesting.

Reading about Lambda@Edge and Cloudflare Workers just introdruced me to this concept :D

But, well, guess I'm a front-end dev by heart

Collapse
 
ben profile image
Ben Halpern

I don't think these topics are super well understood, but dev.to is served from the edge, with some logic at that point, and then some more once the request hits the browser (service workers + local storage etc. to ultimately show the specific user their content as quickly as possible.)

Collapse
 
rhymes profile image
rhymes

Wow super interesting! I need to get more familiar with these concepts. Thanks

Collapse
 
jonbosanac profile image
Jonathan Bosanac

This is serverless compute or Function as a Service. Edge compute is AR, VR and compute at the actual edge. The CDN industry really confuses things.

Collapse
 
kayis profile image
K

Guess people have different opinions about where the edge is, haha.

Some say it's the edge of their infrastructure.

Some say it's mobile devices.

Some say it's embedded devices.

Everybody wants to claim the edge :D

Collapse
 
jonbosanac profile image
Jonathan Bosanac • Edited

That is my point, it is confusing.

The "edge" is just the modern equivalent to DMARC or MPOE. There is a whole lot of network between your nearest proxy and the device.

As your company acquired Neumob, your network edge is not the proxy nearest to the device but the device itself.

I face the edge term challenge in presenting my company nuu:bit. We are software platform focused on content delivery and service chaining. We enable infrastructure providers to offer similar services (white label, etc). I have found that using edge compute to describe FaaS or serverless causes too much confusion.

-Jon

Thread Thread
 
kayis profile image
K

Oh, I'm in no way associated with Cloudflare :)

But thank's for the insight, I think clear naming is important too.