Skip to content

Commit

Permalink
feat: add tags for removing parts from template
Browse files Browse the repository at this point in the history
  • Loading branch information
Volodymyr Malyhin committed Oct 7, 2024
1 parent cc757d2 commit 7f76e85
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 12 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
19 changes: 16 additions & 3 deletions ilc/server/tailor/configs-injector.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,13 @@ module.exports = class ConfigsInjector {
registryConfig.settings?.i18n,
);

const newRelicScript =
!this.#nrAutomaticallyInjectClientScript || request.ldeRelated ? '' : this.#getNewRelicScript();
const headHtmlContent = this.#wrapWithIgnoreDuringParsing(
//...routeAssets.scriptLinks,
this.#getIlcState(request),
this.#getSPAConfig(registryConfig),
`<script>window.ilcApps = [];</script>`,
this.#wrapWithAsyncScriptTag(this.#getClientjsUrl()),
newRelicScript,
this.#getNewRelicScript(),
hrefLangHtml,
canonicalTagHtml,
);
Expand All @@ -75,6 +73,10 @@ 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);
}

return document;
}

Expand Down Expand Up @@ -236,6 +238,10 @@ module.exports = class ConfigsInjector {
// So it is much easier to achieve needed outcome by modifying template.
// Right now gently backward compatibility is maintained, so everyone who expects NR to be injected in browser will get it.
#getNewRelicScript = () => {
if (!this.#nrAutomaticallyInjectClientScript) {
return '';
}

let nrCode = this.#newrelic.getBrowserTimingHeader();
if (this.#nrCustomClientJsWrapper === null || !nrCode) {
return nrCode;
Expand All @@ -244,4 +250,11 @@ module.exports = class ConfigsInjector {
nrCode = nrCode.replace(/<script.*?>(.*)<\/script\s*>/s, '$1');
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));
}
};
14 changes: 10 additions & 4 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 exclude New Relic script if ILC-overrideConfig cookie is present', () => {
it('should add remove new relic wrap tags if ILC-overrideConfig cookie is present', () => {
const overrideConfig = JSON.stringify({ someKey: 'someValue' });
const encodedOverrideConfig = 'LZUTF8:' + LZUTF8.encodeBase64(LZUTF8.compress(overrideConfig));

Expand All @@ -438,15 +438,20 @@ describe('configs injector', () => {
},
},
() => {
const browserTimingHeader = `<script defer type="text/javascript">window.browserTimingHeader = 'Hi there! I should add a timing header.';</script>`;
newrelic.getBrowserTimingHeader.withArgs().returns(browserTimingHeader);
const nrHeader = `<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 -->`
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 -->' +
Expand All @@ -457,8 +462,9 @@ describe('configs injector', () => {
'</body>' +
'</html>',
};

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

0 comments on commit 7f76e85

Please sign in to comment.