Skip to main content

Blog Post

Cloudflare Workers to Extend Drupal

February 10, 2020

Drupal provides a rich ecosystem of tools and features to turn your content system into an effective and performant platform. But it's not the only technology you want to rely on to reach your audience. Integrating with third-party systems can provide high-value experiences, such as:

  • External search technologies like Solr and ElasticSearch are fast, flexible and scalable to satisfy even the largest workloads
  • Messaging services like SendGrid or Twilio can manage delivery and tracking for important notifications

Another technology that websites depend on to be fast and reliable is a content-delivery network, or CDN. These systems help manage user traffic and deliver content as fast as possible while limiting how much work is required of the servers that actually host your website. 

Cloudflare CDN

Cloudflare is one of the big names in the CDN world and they're showing that their global network can do much more with the introduction of Cloudflare Workers. This is a new feature using the V8 engine that is also used by Chromium and Node.js. 

Workers operate on Cloudflare's network to process every inbound request. While many integrations, like Solr search, affect the user experience after reaching the site, Workers kick in before the user reaches the site. Similar to cloud services that are marketed as "serverless," Cloudflare takes care of managing the deployment and workload concerns.

Rather than running on an individual’s machine (e.g a browser application or on a centralized server), Workers functions run on Cloudflare’s Edge Network - a growing global network of thousands of machines distributed across hundreds of locations. Each of these machines hosts an instance of the Workers runtime, and each of those runtimes is capable of running thousands of user-defined apps.

- Source: Cloudflare Workers - How it Works

Written in Javascript, Workers can be created and edited in the Cloudflare web console or through CLI tools. There is also a very handy selection of templates to get started: https://developers.cloudflare.com/workers/templates/

What does this mean if you're operating a Drupal site? Workers provide a new outlet to expand the functionality of your application, help replace or simplify existing features, or get access to tools that are otherwise limited to large-scale hosting plans. In some cases, a Worker can replace a module that you need to install and maintain -- or even build yourself. In other cases, using Workers can help reduce the load on your application database, by handling redirects for example. Workers also expand on Cloudflare's security benefits, by blocking malicious traffic before it reaches your application server.

Use Cases

A/B testing

A/B testing, or split testing, is one common way to compare two different pages, to measure the effectiveness of the content or design. Users to a specific URL are shown version A or version B, based on random assignment or a cookie assigned to certain users. Cloudflare Workers provide an easy-to-use template to get this working on your site.

Use this instead of a Drupal module: A/B Test JS

Bulk Redirects

The Redirect module provides a great way for editors to manage redirects from one URL to another. Workers can accomplish the same thing with even more flexibility. Managing these redirects at the edge also can save the load on your application server. For a site migration, this can be especially useful to route traffic between the new application and the old one.

Use this instead of a Drupal module: Redirect

Respond to Requests based on Geolocation or IP

For marketing or commerce needs, it's more effective to know something about a user when they first reach the site. Where is the user located in the world? Is the visitor using the organization's internal IP address? By knowing this kind of information, you can make smart decisions about how to route the request or what language to show the user. Workers have access to the incoming request in order to manage these choices, by setting a cookie or routing to different versions of the application. For sites with a global audience and varying needs, this type of flexibility is critical.

Use this with Drupal's Multilingual features. 

Prepopulate a Contextual Filter

The Views module is the lifeblood of any dynamic Drupal site, and integrating with contextual filters helps manage the complex mix of content and taxonomies. What's better than showing these filters in a dropdown? Anticipating the user's needs and prepopulating the contextual filter. Similar to above, we can make certain assumptions about our user based on their geolocation, for example, and set a cookie that is read by Views. This kind of efficiency can improve the user experience and make it easier for visitors to accomplish their task on the site.

Use this with Drupal module: Views Extras

Sanitize Query String for Email Campaigns, etc.

Email marketing campaigns rely on specialized URL parameters to track which visitors reach your landing pages. Some of these parameters are useful to Drupal, to provide a custom experience to the user. But when these URLs become unwieldy and very long, the user may be confused or suspicious. Using a Worker, we can read the query parameters and respond accordingly -- maybe to send a confirmation message to a logging service. Then the user is directed to a simplified URL without all the extra parameters. Similarly, these types of rules can help detect malicious requests and block them from reaching the site.

Use this with email campaigns.

Conclusion

Building web applications can be complicated, more so today than ever before. Users have high expectations when they visit our sites. Adding a tool like Cloudflare Workers should make that process easier to manage and maintain. Whether a site is running Drupal or some other technology (or both!), Workers may help deliver the kind of rich experiences that users are looking for.

Related Insights