Skip to content

Commit

Permalink
Merge pull request #614 from namecheap/feat/disable_newrelic_lde
Browse files Browse the repository at this point in the history
feat(ilc): disable newrelic for lde
  • Loading branch information
m2broth authored Oct 8, 2024
2 parents 1f7440e + f127d49 commit 9dd5960
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 5 deletions.
9 changes: 5 additions & 4 deletions ilc/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ilc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"license": "Apache-2.0",
"dependencies": {
"@namecheap/error-extender": "^2.2.1",
"@namecheap/tailorx": "^8.1.0",
"@namecheap/tailorx": "^8.2.0",
"@newrelic/native-metrics": "^11.0.0",
"agentkeepalive": "^4.5.0",
"axios": "^1.7.7",
Expand Down
9 changes: 9 additions & 0 deletions ilc/server/tailor/configs-injector.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module.exports = class ConfigsInjector {
#cdnUrl;
#jsInjectionPlaceholder = '<!-- ILC_JS -->';
#cssInjectionPlaceholder = '<!-- ILC_CSS -->';
#markedProdTags = /<!-- Prod only start -->.*?<!-- Prod only end -->/gims;

constructor(newrelic, cdnUrl = null, nrCustomClientJsWrapper = null, nrAutomaticallyInjectClientScript = true) {
this.#newrelic = newrelic;
Expand Down Expand Up @@ -73,6 +74,10 @@ module.exports = class ConfigsInjector {

request.styleRefs = this.#getRouteStyleRefsToPreload(registryConfig.apps, slots, template.styleRefs);

if (request.ldeRelated) {
document = this.#removeProdTags(document);
}

return document;
}

Expand Down Expand Up @@ -246,4 +251,8 @@ module.exports = class ConfigsInjector {
nrCode = nrCode.replace(/<script.*?>(.*)<\/script\s*>/s, '$1');
return this.#nrCustomClientJsWrapper.replace('%CONTENT%', nrCode);
};

#removeProdTags(content) {
return content.replace(this.#markedProdTags, '');
}
};
50 changes: 50 additions & 0 deletions ilc/server/tailor/configs-injector.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const chai = require('chai');
const sinon = require('sinon');
const _ = require('lodash');
const _fp = require('lodash/fp');
const LZUTF8 = require('lzutf8');
const { context } = require('../context/context');

const ConfigsInjector = require('./configs-injector');
Expand Down Expand Up @@ -417,5 +418,54 @@ describe('configs injector', () => {
},
);
});
it('should remove marked tags for LDE if cookie is present', () => {
const overrideConfig = JSON.stringify({ someKey: 'someValue' });
const encodedOverrideConfig = 'LZUTF8:' + LZUTF8.encodeBase64(LZUTF8.compress(overrideConfig));

context.run(
{
request: {
raw: {
url: 'test/a?test=15',
connection: {
encrypted: true,
},
},
hostname: 'test.com',
headers: {
cookie: `ILC-overrideConfig=${encodedOverrideConfig}`,
},
},
},
() => {
const nrHeader = `<!-- Prod only start --><script type="text/javascript">
;window.NREUM||(NREUM={});NREUM.init={distributed_tracing:{enabled:true},privacy:{cookies_enabled:true},ajax:{deny_list:["bam.nr-data.net"]}};
;NREUM.loader_config={accountID:"1111",trustKey:"1111",agentID:"1111",licenseKey:"1111",applicationID:"1111"};
;NREUM.info={beacon:"bam.nr-data.net",errorBeacon:"bam.nr-data.net",licenseKey:"1111",applicationID:"1111",sa:1};</script><!-- Prod only end -->`;
const configsInjector = new ConfigsInjector(newrelic);
const request = { registryConfig, ilcState: { locale: 'en-US' }, ldeRelated: true };
const template = {
styleRefs: [],
content:
'<html>' +
'<head>' +
nrHeader +
'<!-- ILC_JS -->' +
'<title>Configs Injector`s test</title>' +
'<!-- ILC_CSS -->' +
'<link rel="stylesheet" href="https://somewhere.com/style.css">' +
'</head>' +
'<body>' +
'<div>Hi there! I am content.</div>' +
'</body>' +
'</html>',
};

const result = configsInjector.inject(request, template, { slots, reqUrl: '/test/route?a=15' });
chai.expect(result).to.not.include(nrHeader);
},
);
});
});
});

0 comments on commit 9dd5960

Please sign in to comment.