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

feat(ilc): disable newrelic for lde #614

Merged
merged 5 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
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
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;
m2broth marked this conversation as resolved.
Show resolved Hide resolved

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);
},
);
});
});
});
Loading