How to use Cloudflare Workers to implement Hreflang across multiple sites and platforms

How to use Cloudflare Workers to implement Hreflang across multiple sites and platforms

Over the past few years we’ve noticed an increasing trend of large brands being unable to implement certain elements of technical SEO that we often take for granted when aiming for best practice and technical excellence.

Notably one of the larger challenges we’ve faced was with an established, international travel brand with a multi-million-dollar turnover who were unable to implement Hreflang due to platform restrictions, and issues with their legacy code base that had a few band-aids and patches holding it together.

As a result, the company had a number of alternate versions targeting different English-speaking countries, all causing duplicate content issues and country versions ranking in the wrong countries – causing issues with attributing lead generation correctly. Historically the three options that we would have would be:

  • Do nothing
  • Re-engineer and rebuild on a new, modern tech stack and platform at a significant cost (time and money)
  • Proceed with making changes, not to best practice guidelines, and risk further issues

However, through Cloudflare Workers, the platform and codebase restrictions could be overcome.

Implementing Hreflang through service workers

We first put the website onto Cloudflare by changing the DNS, and then set about creating a serverless application to augment the current site with a Hreflang injection into the < head >.

Our process for this was:

  • Defining the required Hreflang implementation required for the URL
  • Configuration of the worker bundle to represent the necessary Hreflang implementation
  • Deployment of worker bundle to inject Hreflang before the , and removal of existing Hreflang tags implemented through other means

In order to make this process “accessible” to non-developers, the input method needs to be simple. So, with Sloth we’ve developed a simple CSV upload, and row management from the Sloth dashboard.

server responses with and without cloudflare workers

Through testing, we were able to modify the codebase through Cloudflare’s existing infrastructure, and inject Hreflang into the site in a way that is renderable to both Google and Bing.

This solution can be used for international websites utilising multiple ccTLDs, as well as websites built on different platforms - allowing management and control of Hreflang implementations in a centralised location.