Skip to content

Commit

Permalink
Add HTTPS redirect functionality to Cloudflare worker
Browse files Browse the repository at this point in the history
  • Loading branch information
tomusher authored and kaedroho committed Jun 3, 2021
1 parent 92ad470 commit e33aeaa
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,9 @@ Finally, set up a Cloudflare Worker based on the following JavaScript. Don't for
// Set this to the domain name of your backend server
const WAGTAIL_DOMAIN = "mysite.herokuapp.com";

// Set to false if Cloudflare shouldn't automatically redirect requests to use HTTPS
const ENFORCE_HTTPS = true;

// Set this to the URL of the A/B testing API on your backend server. Don't forget the / at the end!
const API_BASE = `https://${WAGTAIL_DOMAIN}/abtestingapi/`;

Expand Down Expand Up @@ -313,11 +316,17 @@ async function handleVisitPageGoal(request, response, tests) {
}

async function handleRequest(request) {
const url = new URL(request.url)

if(url.protocol == "http:" && ENFORCE_HTTPS) {
url.protocol == "https:";
return Response.redirect(url, 301);
}

const tests = await getRunningTests();

// Check if there is a running test on the visited page
const getRunningTest = () => {
const url = new URL(request.url);
for (const test of tests) {
if (test.site.hostname === url.hostname && test.page.path === url.pathname) {
return test;
Expand Down

0 comments on commit e33aeaa

Please sign in to comment.