Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add HTTPS redirect functionality to Cloudflare worker #31

Merged
merged 1 commit into from
Jun 3, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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