Could Cloudflare Workers work as a commenting solution?

Hi all – We’ve had a lot of discussion and deep sadness over the lack of a good commenting system for static sites (or just sites).

Cloudflare Workers allows you to run your own custom JavaScript at the edge, meaning Cloudflare’s edge, its CDN data centers. So for example, you could use Workers to intercept and act on all requests in some way before they hit your origin server. They have many applications.

Kenton Varda does a good job of introducing Cloudflare Workers in this post.

The official CF page is here.

What I’m thinking is that clicking on a button to comment could trigger a Worker. And that a Worker could update the page with the new comment by sending JSON over. That would require that the web server append the data to the page, and probably also to a backup comment flat file or database. Is this making sense?

Workers are incredibly fast, just a few milliseconds. They’re using the V8 JavaScript engine from Chrome, but it’s not Node – it’s much faster than Node. They explain this a bit in this post comparing Workers to Amazon’s Lambdas, and Lambdas@Edge

What do you think? I don’t know JS very well – does it sound feasible?

Cloudflare Workers are a great product! I happily use them on a few websites now, primarily for speed and security.

clicking on a button to comment could trigger a Worker.

This is possible by having the Worker monitor the URLs, and kick into action when a certain URL is requested. You could for instance have that button link to example.com/my-post/submit-comment.

I’m not sure how you’d send information from a page into a Worker, but perhaps by submitting a form?

And that a Worker could update the page with the new comment by sending JSON over.

Workers can read the content from a page that they are going to send over to the user requesting the page, so you could insert comments dynamically that way too. Or of course bundle the comments as a JSON file and have the user’s client-side code show the comments.

That would require that the web server append the data to the page, and probably also to a backup comment flat file or database.

Well the Worker can append the data to the page, so the static website doesn’t have to run on an actual server and something like S3 can do.

You mention here backup, but what you mean with backup is actually what the regular workflow probably will look like. That’s because Workers cannot store data in themselves – they run like client-side JavaScript files – so you’ll definitely need somewhere to store or send the data too.

The Worker will then later need to fetch those comments.

So that other data can be on an actual server. But you can go serverless as well, I think. If an AWS Lambda or Azure Function monitors a URL, you can have the Worker submit a request for that URL and have the serverless function fetch the comment data, and then return it to the Worker as a JSON file. And you could use another URL that a Lambda/Function runs on to act as the ‘data updater’ when a URL request is make to that Lambda/Function.

(This latter is in theory, I haven’t build that. Just thinking along here.)

(You can probably also replace what you intend to do with Workers with Azure Functions and AWS Lamdba? Since you mention you don’t have a lot of JavaScript experience, the languages that they offer might be something you’re more familiar with?)

In the Workers documentation there are examples of making requests with Workers.

Really all the worker is doing is making your front-end JavaScript more performant. Nice that Cloudflare are providing a method to host them though - certainly saves you from having to host something yourself. Though you could do that with Netlify’s AWS Lambda probably.

However, note that Cloudflare workers are not free. At $5 per month minimum, you are on your way to a low-cost VPS.

As @Jura say’s, you’d still need something like Lambda functions to process the comments and a database to store them.

If you are doing that without any kind of authentication, you are asking for loads of spam trouble. If you are asking for authentication, you need to manage the identities (and to deal with the GDPR and other local laws) or hand that off to a 3rd party which puts you right back at the heart of the problem people seem to be having with Disqus in the first place.