For Workes & Pages, what is the name of the domain?
a-hr.pro
What is the issue or error you’re encountering
zaraz doesn’t work when Cloudflare worker is active, but works when I disable worker
What are the steps to reproduce the issue?
whenever I disable the worker in the Workers Routes zaraz works perfectly fine
The task of this worker is to re-write the URL of the requested page for users that:
are in Ukraine and
do not have a pll_language cookie.
For the rest of the users and for static resources no URL re-write is expected
Here is the text of the worker:
addEventListener("fetch", (event) => {
event.respondWith(
handleRequest(event.request).catch(
(err) => new Response(err.stack, {status: 500})
)
);
});
async function handleRequest(request) {
const {url} = request;
if (shouldBypass(request, url)) {
return fetch(request);
}
const requestCountry = request.cf.country;
if (requestCountry != null && "ua" === requestCountry.toLowerCase()) {
return Response.redirect(buildRedirectUrl(url));
}
return fetch(request);
}
function shouldBypass(request, url) {
let flag = false;
if (request.headers.get("cf-ipcountry") === "XX") flag = true;
let cookies = request.headers.get('Cookie') || "";
if (cookies.includes('pll_language')) flag = true;
const force_bypass = [".js", ".css", ".jpeg", ".jpg", ".png", ".webp", ".avif", ".svg", ".woff2", "/wp-content/", "/wp-json/", "/zaraz/", "/ua/", "_ua/"];
force_bypass.forEach(part => {
if (url.includes(part)) flag = true;
});
return flag;
}
function buildRedirectUrl(url) {
let trimmed = url.replace(/\/$/, '');
if (trimmed.endsWith("a-hr.pro")) return "https://www.a-hr.pro/ua/home_ua/";
let parts = trimmed.split('/');
if (parts.length === 4) {
return parts[0].concat("/")
.concat(parts[1].concat("/"))
.concat(parts[2].concat("/"))
.concat("ua/")
.concat(parts[3]).concat("_ua/"); // page
}
if (parts.length === 5) {
let baseTargetUrl = parts[0].concat("/")
.concat(parts[1].concat("/"))
.concat(parts[2].concat("/"))
.concat("ua/");
if (parts[parts.length - 1].startsWith("#")) {
return baseTargetUrl
.concat(parts[3]).concat("_ua/")
.concat(parts[4]);
} else {
return baseTargetUrl
.concat(parts[3]).concat("/")
.concat(parts[4]).concat("_ua/");
}
}
let baseTargetUrl = parts[0].concat("/")
.concat(parts[1].concat("/"))
.concat(parts[2].concat("/"))
.concat("ua/");
for (let i = 3; i < parts.length - 2; i++) {
baseTargetUrl += parts[i].concat("/");
}
if (parts[parts.length - 1].startsWith("#")) {
return baseTargetUrl
.concat(parts[parts.length - 2]).concat("_ua/")
.concat(parts[parts.length - 1]);
} else {
return baseTargetUrl
.concat(parts[parts.length - 2]).concat("/")
.concat(parts[parts.length - 1]).concat("_ua/");
}
}
@yoav_zaraz is this something you can help with based on your experience?
Your worker is probably removing all original headers from the request, including the accept
header, which effectively disables Zaraz. Please keep the original headers.
1 Like
Hi @yoav_zaraz ,
Apparently, the issue was caused not by the headers, but by the fact that Zaraz auto-inject was not ejecting into the redirected page. So, something about sequence/priority for auto-inject.
Manual loading fixed the problem.
Hope that is something you might wish to reflect on.
Cheers
system
Closed
July 29, 2024, 10:10am
7
This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.