Skip to content

Commit fda75ec

Browse files
Allow setting proxyDomain to auto
1 parent 2417309 commit fda75ec

File tree

3 files changed

+13
-12
lines changed

3 files changed

+13
-12
lines changed

config.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
const env = process.env.APP__ENV_NAME || "local";
22
const isLocal = env === "local" || env === "test";
3-
const proxyDomain = process.env.PROXY_DOMAIN || "localhost";
43

54
export default {
65
isLocalEnv: isLocal,
76
httpPort: process.env.PORT || 80,
8-
proxyDomain: proxyDomain, // Your domain
7+
proxyDomain: "", // Domain to proxy calls through. Leave it empty to use the requested domain as a proxy domain
98
proxy: { // Proxy configuration is here
109
domains: [ // These domains are replaced in any proxied response (including scripts, URLs and redirects)
1110
"adservice.google.com",
@@ -48,7 +47,7 @@ export default {
4847
"www.googletagmanager.com": [
4948
{
5049
regex: /"https\:\/\/s","http:\/\/a","\.adroll\.com/,
51-
replace: `"https://${ proxyDomain }/s","http://${ proxyDomain }/a",".adroll.com`
50+
replace: ({ host }) => `"https://${ host }/s","http://${ host }/a",".adroll.com`
5251
}
5352
],
5453
"eb2.3lift.com": [

readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ Proxied: www.google-analytics.com/collect?v=1&_v=j73&a=531530768&t=pageview&_s=1
6060

6161
Check the [test-static/index.html](test-static/index.html) file's code to see how to bind the proxied analytics to your front end.
6262

63+
Later, you can containerize this repository and route the incoming traffic to `/gtm-proxy` path (for example) through this container in order to avoid analytics blocking.
64+
6365
## Configuration
6466

6567
You can configure which third-parties to proxy/replace and how to do it in the config file. Find the actual configuration in [config.js](config.js) file:

src/modules/proxy.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ const maskRegex = new RegExp(
1515
Array.from(maskPaths).join("|").replace(/\//g, "\\/"),
1616
"gi"
1717
);
18-
const replaceDomains = (match, pos, str) => {
18+
const replaceDomainsForHost = (host) => (match, pos, str) => {
1919
const escapedSlashes = str[pos - 2] === "\\" && str[pos - 2] === "/"
2020
const r = `${
2121
escapedSlashes
22-
? config.proxyDomain.replace(/\//g, "\\/") + "\\"
23-
: config.proxyDomain
24-
}/${ match }`
22+
? (config.proxyDomain || host).replace(/\//g, "\\/") + "\\"
23+
: (config.proxyDomain || host)
24+
}/${ match }`;
2525
return r;
2626
};
2727

@@ -37,7 +37,7 @@ export function createDefaultProxy (targetDomain, proxyOptionsOverride = {}) {
3737
? proxyOptionsOverride["proxyReqOptDecorator"](proxyRequest, originalRequest)
3838
: proxyRequest;
3939
},
40-
userResHeaderDecorator: (headers) => {
40+
userResHeaderDecorator: (headers, { headers: { host } }) => {
4141
if (headers.location) {
4242
if (config.proxy.specialContentReplace[servername]) { // Keep before other replacements
4343
const replacements = config.proxy.specialContentReplace[servername];
@@ -46,20 +46,20 @@ export function createDefaultProxy (targetDomain, proxyOptionsOverride = {}) {
4646
}
4747
}
4848
headers.location = headers.location
49-
.replace(replaceDomainRegex, replaceDomains)
49+
.replace(replaceDomainRegex, replaceDomainsForHost(host))
5050
.replace(maskRegex, match => mask(match));
5151
}
5252
return headers;
5353
},
54-
userResDecorator: (_, proxyResData) => {
54+
userResDecorator: (_, proxyResData, { headers: { host } }) => {
5555
if (_.req.res && _.req.res.client && _.req.res.client.servername) {
5656
servername = _.req.res.client.servername;
5757
}
58-
let pre = proxyResData.toString().replace(replaceDomainRegex, replaceDomains);
58+
let pre = proxyResData.toString().replace(replaceDomainRegex, replaceDomainsForHost(host));
5959
if (config.proxy.specialContentReplace[servername]) {
6060
const replacements = config.proxy.specialContentReplace[servername];
6161
for (const r of replacements) {
62-
pre = pre.replace(r.regex, r.replace);
62+
pre = pre.replace(r.regex, r.replace instanceof Function ? r.replace({ host }) : r.replace);
6363
}
6464
}
6565
pre = pre.replace(maskRegex, (match) => { // Mask configured URLs

0 commit comments

Comments
 (0)