diff --git a/ilc/package-lock.json b/ilc/package-lock.json index 19df79b8..872a2baf 100644 --- a/ilc/package-lock.json +++ b/ilc/package-lock.json @@ -10,7 +10,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", @@ -2195,9 +2195,10 @@ "integrity": "sha512-GeFZT8ntXN7N91jyHy7qEw4Kq0v66kV/7+/EQJHHsdzbko44wtrDvr+Mj6Td9soqlZGQz8VoOUa9XrHSauVcLA==" }, "node_modules/@namecheap/tailorx": { - "version": "8.1.0", - "resolved": "https://registry.npmjs.org/@namecheap/tailorx/-/tailorx-8.1.0.tgz", - "integrity": "sha512-TSCfaKfUd14QQkZrZHYIP/vj5zONxhCNtpTkpd15BN0XPl2idHwJFBDaNgKqGh/iZiHesCcXMsM6sQRIgizmKg==", + "version": "8.2.0", + "resolved": "https://registry.npmjs.org/@namecheap/tailorx/-/tailorx-8.2.0.tgz", + "integrity": "sha512-RAeOeaiw1btqowbkc5BfklXGBFUw4R4SkVCFNAVxEj9Hc/qaeo1SnDF99MI8nTiixefaZCWvyHhcp8R0/iwO3A==", + "license": "Apache-2.0", "dependencies": { "@namecheap/error-extender": "^2.0.0", "agentkeepalive": "^4.5.0", diff --git a/ilc/package.json b/ilc/package.json index 917d067b..9a1ed481 100644 --- a/ilc/package.json +++ b/ilc/package.json @@ -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", diff --git a/ilc/server/tailor/configs-injector.js b/ilc/server/tailor/configs-injector.js index 1965b967..23a7ecef 100644 --- a/ilc/server/tailor/configs-injector.js +++ b/ilc/server/tailor/configs-injector.js @@ -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), ``, this.#wrapWithAsyncScriptTag(this.#getClientjsUrl()), - newRelicScript, + this.#getNewRelicScript(), hrefLangHtml, canonicalTagHtml, ); @@ -75,6 +73,10 @@ module.exports = class ConfigsInjector { request.styleRefs = this.#getRouteStyleRefsToPreload(registryConfig.apps, slots, template.styleRefs); + if (request.ldeRelated) { + document = this.#wrapTagsToRemove(document, /]*>\s*;window\.NREUM[\s\S]*?<\/script>/g); + } + return document; } @@ -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; @@ -244,4 +250,11 @@ module.exports = class ConfigsInjector { nrCode = nrCode.replace(/(.*)<\/script\s*>/s, '$1'); return this.#nrCustomClientJsWrapper.replace('%CONTENT%', nrCode); }; + + #wrapWithRemoveBeforeParsing = (content) => + `${content}`; + + #wrapTagsToRemove(content, regex) { + return content.replace(regex, (match) => this.#wrapWithRemoveBeforeParsing(match)); + } }; diff --git a/ilc/server/tailor/configs-injector.spec.js b/ilc/server/tailor/configs-injector.spec.js index 552b8edd..af4196b6 100644 --- a/ilc/server/tailor/configs-injector.spec.js +++ b/ilc/server/tailor/configs-injector.spec.js @@ -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)); @@ -438,8 +438,12 @@ describe('configs injector', () => { }, }, () => { - const browserTimingHeader = ``; - newrelic.getBrowserTimingHeader.withArgs().returns(browserTimingHeader); + const nrHeader = ``; + const expectedHeader = `${nrHeader}` const configsInjector = new ConfigsInjector(newrelic); const request = { registryConfig, ilcState: { locale: 'en-US' }, ldeRelated: true }; const template = { @@ -447,6 +451,7 @@ describe('configs injector', () => { content: '' + '' + + nrHeader + '' + 'Configs Injector`s test' + '' + @@ -457,8 +462,9 @@ describe('configs injector', () => { '' + '', }; + 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); }, ); });