Skip to content

Commit

Permalink
feat(ilc): add tag for removing production related scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
Volodymyr Malyhin committed Oct 7, 2024
1 parent 695c75e commit f127d49
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 11 deletions.
10 changes: 4 additions & 6 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 @@ -74,7 +75,7 @@ module.exports = class ConfigsInjector {
request.styleRefs = this.#getRouteStyleRefsToPreload(registryConfig.apps, slots, template.styleRefs);

if (request.ldeRelated) {
document = this.#wrapTagsToRemove(document, /<script\b[^>]*>\s*;window\.NREUM[\s\S]*?<\/script>/g);
document = this.#removeProdTags(document);
}

return document;
Expand Down Expand Up @@ -251,10 +252,7 @@ module.exports = class ConfigsInjector {
return this.#nrCustomClientJsWrapper.replace('%CONTENT%', nrCode);
};

#wrapWithRemoveBeforeParsing = (content) =>
`<!-- TailorX: Remove before parsing START -->${content}<!-- TailorX: Remove before parsing END -->`;

#wrapTagsToRemove(content, regex) {
return content.replace(regex, (match) => this.#wrapWithRemoveBeforeParsing(match));
#removeProdTags(content) {
return content.replace(this.#markedProdTags, '');
}
};
9 changes: 4 additions & 5 deletions ilc/server/tailor/configs-injector.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ describe('configs injector', () => {
},
);
});
it('should add remove new relic wrap tags if ILC-overrideConfig cookie is present', () => {
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));

Expand All @@ -438,12 +438,11 @@ describe('configs injector', () => {
},
},
() => {
const nrHeader = `<script type="text/javascript">
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>`;
const expectedHeader = `<!-- TailorX: Remove before parsing START -->${nrHeader}<!-- TailorX: Remove before parsing END -->`;
;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 = {
Expand All @@ -464,7 +463,7 @@ describe('configs injector', () => {
};

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

0 comments on commit f127d49

Please sign in to comment.