From 5b6a22775f5f9fd854a7ac33d30d6eebd72de576 Mon Sep 17 00:00:00 2001 From: illiakovalenko Date: Tue, 5 Dec 2023 10:31:33 +0200 Subject: [PATCH 1/2] [sitecore-jss] Provide contextId as a part of content styles path --- .../plugins/content-styles.ts | 6 +++++- .../src/layout/content-styles.test.ts | 19 +++++++++++-------- .../sitecore-jss/src/layout/content-styles.ts | 14 +++++++++++--- 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/packages/create-sitecore-jss/src/templates/nextjs-xmcloud/src/lib/page-props-factory/plugins/content-styles.ts b/packages/create-sitecore-jss/src/templates/nextjs-xmcloud/src/lib/page-props-factory/plugins/content-styles.ts index d980933cf6..8935e335c0 100644 --- a/packages/create-sitecore-jss/src/templates/nextjs-xmcloud/src/lib/page-props-factory/plugins/content-styles.ts +++ b/packages/create-sitecore-jss/src/templates/nextjs-xmcloud/src/lib/page-props-factory/plugins/content-styles.ts @@ -8,7 +8,11 @@ class ContentStylesPlugin implements Plugin { async exec(props: SitecorePageProps) { // Get content stylessheet link, empty if styles are not used on the page - const contentStyles = getContentStylesheetLink(props.layoutData, config.sitecoreEdgeUrl); + const contentStyles = getContentStylesheetLink( + props.layoutData, + config.sitecoreEdgeContextId, + config.sitecoreEdgeUrl + ); contentStyles && props.headLinks.push(contentStyles); diff --git a/packages/sitecore-jss/src/layout/content-styles.test.ts b/packages/sitecore-jss/src/layout/content-styles.test.ts index 5b090ed115..7cc41436e9 100644 --- a/packages/sitecore-jss/src/layout/content-styles.test.ts +++ b/packages/sitecore-jss/src/layout/content-styles.test.ts @@ -13,10 +13,13 @@ import { SITECORE_EDGE_URL_DEFAULT } from '../constants'; describe('content-styles', () => { const truthyValue = { value: '

bar

' }; const falsyValue = { value: '

ck-content

' }; + const sitecoreEdgeContextId = ' 7qwerty'; describe('getContentStylesheetLink', () => { it('should return null when route data is empty', () => { - expect(getContentStylesheetLink({ sitecore: { context: {}, route: null } })).to.be.null; + expect( + getContentStylesheetLink({ sitecore: { context: {}, route: null } }, sitecoreEdgeContextId) + ).to.be.null; }); it('should set "loadStyles: false" when layout does not have a ck-content class', () => { @@ -32,7 +35,7 @@ describe('content-styles', () => { }, }; - expect(getContentStylesheetLink(layoutData)).to.be.null; + expect(getContentStylesheetLink(layoutData, sitecoreEdgeContextId)).to.be.null; }); it('should set "loadStyles: true" when layout has a ck-content class', () => { @@ -56,8 +59,8 @@ describe('content-styles', () => { }, }; - expect(getContentStylesheetLink(layoutData)).to.deep.equal({ - href: `${SITECORE_EDGE_URL_DEFAULT}/v1/files/pages/styles/content-styles.css`, + expect(getContentStylesheetLink(layoutData, sitecoreEdgeContextId)).to.deep.equal({ + href: `${SITECORE_EDGE_URL_DEFAULT}/v1/files/pages/styles/content-styles.css?sitecoreContextId=${sitecoreEdgeContextId}`, rel: 'stylesheet', }); }); @@ -331,14 +334,14 @@ describe('content-styles', () => { describe('getContentStylesheetUrl', () => { it('should return the default url', () => { - expect(getContentStylesheetUrl()).to.equal( - `${SITECORE_EDGE_URL_DEFAULT}/v1/files/pages/styles/content-styles.css` + expect(getContentStylesheetUrl(sitecoreEdgeContextId)).to.equal( + `${SITECORE_EDGE_URL_DEFAULT}/v1/files/pages/styles/content-styles.css?sitecoreContextId=${sitecoreEdgeContextId}` ); }); it('should return the custom url', () => { - expect(getContentStylesheetUrl('https://foo.bar')).to.equal( - 'https://foo.bar/v1/files/pages/styles/content-styles.css' + expect(getContentStylesheetUrl(sitecoreEdgeContextId, 'https://foo.bar')).to.equal( + `https://foo.bar/v1/files/pages/styles/content-styles.css?sitecoreContextId=${sitecoreEdgeContextId}` ); }); }); diff --git a/packages/sitecore-jss/src/layout/content-styles.ts b/packages/sitecore-jss/src/layout/content-styles.ts index 7eca0edcad..1dc84bb450 100644 --- a/packages/sitecore-jss/src/layout/content-styles.ts +++ b/packages/sitecore-jss/src/layout/content-styles.ts @@ -19,11 +19,13 @@ type Config = { loadStyles: boolean }; /** * Get the content styles link to be loaded from the Sitecore Edge Platform * @param {LayoutServiceData} layoutData Layout service data + * @param {string} sitecoreEdgeContextId Sitecore Edge Context ID * @param {string} [sitecoreEdgeUrl] Sitecore Edge Platform URL. Default is https://edge-platform.sitecorecloud.io * @returns {HTMLLink | null} content styles link, null if no styles are used in layout */ export const getContentStylesheetLink = ( layoutData: LayoutServiceData, + sitecoreEdgeContextId: string, sitecoreEdgeUrl = SITECORE_EDGE_URL_DEFAULT ): HTMLLink | null => { if (!layoutData.sitecore.route) return null; @@ -34,11 +36,17 @@ export const getContentStylesheetLink = ( if (!config.loadStyles) return null; - return { href: getContentStylesheetUrl(sitecoreEdgeUrl), rel: 'stylesheet' }; + return { + href: getContentStylesheetUrl(sitecoreEdgeContextId, sitecoreEdgeUrl), + rel: 'stylesheet', + }; }; -export const getContentStylesheetUrl = (sitecoreEdgeUrl = SITECORE_EDGE_URL_DEFAULT): string => - `${sitecoreEdgeUrl}/v1/files/pages/styles/content-styles.css`; +export const getContentStylesheetUrl = ( + sitecoreEdgeContextId: string, + sitecoreEdgeUrl = SITECORE_EDGE_URL_DEFAULT +): string => + `${sitecoreEdgeUrl}/v1/files/pages/styles/content-styles.css?sitecoreContextId=${sitecoreEdgeContextId}`; export const traversePlaceholder = ( components: Array, From 7d117aa3b4a4eed7a396b93bac2b31848a8f083a Mon Sep 17 00:00:00 2001 From: illiakovalenko Date: Tue, 5 Dec 2023 10:33:39 +0200 Subject: [PATCH 2/2] Updated CHANGELOG --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7331d65ce4..a3684f2e49 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,7 +13,7 @@ Our versioning strategy is as follows: ### ๐ŸŽ‰ New Features & Improvements -* `[sitecore-jss]` `[templates/nextjs-xmcloud]` Load the content styles for the RichText component ([#1670](https://github.com/Sitecore/jss/pull/1670))([#1683](https://github.com/Sitecore/jss/pull/1683)) +* `[sitecore-jss]` `[templates/nextjs-xmcloud]` Load the content styles for the RichText component ([#1670](https://github.com/Sitecore/jss/pull/1670))([#1683](https://github.com/Sitecore/jss/pull/1683))([#1684](https://github.com/Sitecore/jss/pull/1684)) ### ๐Ÿงน Chores