Subdirectory publishing via CloudFlare Workers¶
Cloudflare Workers is a serverless application platform. As it can work with HTTP requests, it can be used instead of a complex reverse proxy setup for subdirectory publishing of the translated website.
To get started, it’s recommended to copy the settings provided in the Publishing wizard. These are partially project-specific. We’ll use a project with the following details:
- Source domain: example.com
- Project code:
redacted
- Translations exist for German
- We wish to publish them to example.com/de/
- Your white label is app.proxytranslation.com
With these options set in the Publish wizard, we get the following configuration:
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
/**
* Fetch and redirect or continue to Proxy
* @param {Request} request
*/
async function handleRequest(request) {
const redirectPaths = ['de']
const proxyUrls = {
'de': 'de-de-redacted.app.proxytranslation.com'
};
const url = new URL(request.url);
const { pathname } = url;
const [, topLevelDirectory] = pathname.split('/');
if (topLevelDirectory && redirectPaths.includes(topLevelDirectory)) {
// The request's URL must be overwritten to send it to the Proxy
request = new Request(
`${url.protocol}//${proxyUrls[topLevelDirectory]}${pathname}${url.search}`
)
request.headers.set('X-TranslationProxy-AllowRobots', 'true')
request.headers.set('X-TranslationProxy-Cache-Info', 'disable')
request.headers.set('X-TranslationProxy-EnableDeepRoot', 'true')
request.headers.set('X-TranslationProxy-ServingDomain', 'example.com')
request.headers.set('Host', proxyUrls[topLevelDirectory])
return fetch(request);
} else {
// Regular request. Forward to origin server.
return fetch(request);
}
}
With these details on hand, you can follow the following steps:
- Log in to your CloudFlare dashboard and head to the Workers section.
- If you’ve not used CloudFlare Workers before, you must choose a subdomain and a plan. For the purposes of this guide, we’ll assume that the domain chosen is
translationproxy-worker
.
- If you’ve not used CloudFlare Workers before, you must choose a subdomain and a plan. For the purposes of this guide, we’ll assume that the domain chosen is
- Click Create a Service
- Fill in the Service name field with an appropriate name.
translationproxy-domainname
would be suitable in general so in our case, we’ll just use that:translationproxy-example
. - You don’t need to change the starter, we’ll overwrite it anyway, so you can simply click Create service. You’ll be navigated to the settings section of the newly created Worker.
- Click on Quick edit. You’ll be navigated to the code editor.
- Delete all the example code that CloudFlare provides and add the snippet we provide.
- NOTE: If you wish to add more target languages, change the
redirectPaths
andproxyUrls
variables accordingly.
- NOTE: If you wish to add more target languages, change the
- Click Save and Deploy
- Test it by opening the URL of the Worker on the translated path. In our case, that would be https://translationproxy-worker.translationproxy-worker.workers.dev/de/. It should load the translated site.
- Head back to the settings section of the Worker and click on Triggers
- Click on Add route
- Enter your domain. In our example it is
*.example.com/*
.- NOTE: Only routes of hostnames configured on Cloudflare can be specified, so if your domain isn’t yet configured, you must do so via the Add site button in the top bar. Adding further DNS records is likely required to do so.
- Your environment will now respond to requests to
*.example.com/*
. You can verify that the translated site loads at, in our case, https://example.com/de/ - With that, the CloudFlare Worker configuration is complete.
Make sure that Rocket Loader is disabled when using CloudFlare!
These instructions are up-to-date as of 04/03/2022.