From 055f876d806bff954546c72e3abc280193607346 Mon Sep 17 00:00:00 2001 From: art-alexeyenko Date: Wed, 10 Jan 2024 17:42:26 -0500 Subject: [PATCH 1/4] [sitecore-jss-nextjs] Reject SDK promise when init was rejected --- .../src/lib/context/sdk/events.ts | 7 ++-- .../src/context/context.test.ts | 34 +++++++++++++++++++ .../src/context/context.ts | 6 ++-- 3 files changed, 43 insertions(+), 4 deletions(-) diff --git a/packages/create-sitecore-jss/src/templates/nextjs-xmcloud/src/lib/context/sdk/events.ts b/packages/create-sitecore-jss/src/templates/nextjs-xmcloud/src/lib/context/sdk/events.ts index 4d23ba17a2..56a7609d77 100644 --- a/packages/create-sitecore-jss/src/templates/nextjs-xmcloud/src/lib/context/sdk/events.ts +++ b/packages/create-sitecore-jss/src/templates/nextjs-xmcloud/src/lib/context/sdk/events.ts @@ -6,8 +6,11 @@ const sdkModule: SDK = { init: async (props) => { // Events module can't be initialized on the server side // We also don't want to initialize it in development mode - if (typeof window === 'undefined' || process.env.NODE_ENV === 'development') return; - + if (typeof window === 'undefined') + throw "Events SDK can't be initialized in server context"; + if (process.env.NODE_ENV === 'development') + throw 'Events SDK is not initialized in development environment'; + await Events.init({ siteName: props.siteName, sitecoreEdgeUrl: props.sitecoreEdgeUrl, diff --git a/packages/sitecore-jss-nextjs/src/context/context.test.ts b/packages/sitecore-jss-nextjs/src/context/context.test.ts index 912dd48323..31f74b8a53 100644 --- a/packages/sitecore-jss-nextjs/src/context/context.test.ts +++ b/packages/sitecore-jss-nextjs/src/context/context.test.ts @@ -28,6 +28,17 @@ describe('Context', () => { }, }; + const errorSdk = { + Error: { + sdk: { error: 'yes' }, + init: () => { + return new Promise((_, reject) => { + reject('Cannot init Error'); + }); + } + }, + } + const fooInitSpy = sinon.spy(sdks.Foo, 'init'); const barInitSpy = sinon.spy(sdks.Bar, 'init'); @@ -159,6 +170,29 @@ describe('Context', () => { expect(context.siteName).to.equal('website-1'); }); + + + it('should catch and log when SDK initialization rejects', () => { + const consoleSpy = sinon.spy(console, 'log'); + const localProps = {...props, sdks: errorSdk}; + const context = new Context(localProps); + context.init(); + expect(consoleSpy.calledWith('Initialization for SDK Error skipped. Reason: \n Cannot init Error')); + consoleSpy.restore(); + }); + + it('should reject when getting SDK that rejected initialization', (done) => { + const localProps = {...props, sdks: errorSdk}; + const context = new Context(localProps); + context.init(); + context.getSDK('Error').then(()=> { + throw new Error('should not resolve'); + }) + .catch((e) => { + expect(e).to.be.equal('Cannot init Error'); + done(); + }); + }); }); describe('getSDK', () => { diff --git a/packages/sitecore-jss-nextjs/src/context/context.ts b/packages/sitecore-jss-nextjs/src/context/context.ts index 61cc0f46d2..c0dd3043b2 100644 --- a/packages/sitecore-jss-nextjs/src/context/context.ts +++ b/packages/sitecore-jss-nextjs/src/context/context.ts @@ -137,11 +137,13 @@ export class Context { * @returns {void} */ protected initSDK(name: T): void { - this.sdkPromises[name] = new Promise((resolve) => { + this.sdkPromises[name] = new Promise((resolve, reject) => { this.props.sdks[name].init(this).then(() => { this.sdks[name] = this.props.sdks[name].sdk; - resolve(this.sdks[name]); + }).catch((e) => { + console.log(`Initialization for SDK ${name.toString()} skipped. Reason: \n ${e}`); + reject(e); }); }); } From 0e7cd24615491d4bf6173460ad5f5b69f4046046 Mon Sep 17 00:00:00 2001 From: art-alexeyenko Date: Wed, 10 Jan 2024 17:43:32 -0500 Subject: [PATCH 2/4] lint --- .../src/context/context.test.ts | 31 ++++++++++--------- .../src/context/context.ts | 17 +++++----- 2 files changed, 27 insertions(+), 21 deletions(-) diff --git a/packages/sitecore-jss-nextjs/src/context/context.test.ts b/packages/sitecore-jss-nextjs/src/context/context.test.ts index 31f74b8a53..7f1838265e 100644 --- a/packages/sitecore-jss-nextjs/src/context/context.test.ts +++ b/packages/sitecore-jss-nextjs/src/context/context.test.ts @@ -34,10 +34,10 @@ describe('Context', () => { init: () => { return new Promise((_, reject) => { reject('Cannot init Error'); - }); - } + }); + }, }, - } + }; const fooInitSpy = sinon.spy(sdks.Foo, 'init'); const barInitSpy = sinon.spy(sdks.Bar, 'init'); @@ -171,27 +171,30 @@ describe('Context', () => { expect(context.siteName).to.equal('website-1'); }); - it('should catch and log when SDK initialization rejects', () => { const consoleSpy = sinon.spy(console, 'log'); - const localProps = {...props, sdks: errorSdk}; + const localProps = { ...props, sdks: errorSdk }; const context = new Context(localProps); context.init(); - expect(consoleSpy.calledWith('Initialization for SDK Error skipped. Reason: \n Cannot init Error')); + expect( + consoleSpy.calledWith('Initialization for SDK Error skipped. Reason: \n Cannot init Error') + ); consoleSpy.restore(); }); it('should reject when getting SDK that rejected initialization', (done) => { - const localProps = {...props, sdks: errorSdk}; + const localProps = { ...props, sdks: errorSdk }; const context = new Context(localProps); context.init(); - context.getSDK('Error').then(()=> { - throw new Error('should not resolve'); - }) - .catch((e) => { - expect(e).to.be.equal('Cannot init Error'); - done(); - }); + context + .getSDK('Error') + .then(() => { + throw new Error('should not resolve'); + }) + .catch((e) => { + expect(e).to.be.equal('Cannot init Error'); + done(); + }); }); }); diff --git a/packages/sitecore-jss-nextjs/src/context/context.ts b/packages/sitecore-jss-nextjs/src/context/context.ts index c0dd3043b2..b934c0c1d4 100644 --- a/packages/sitecore-jss-nextjs/src/context/context.ts +++ b/packages/sitecore-jss-nextjs/src/context/context.ts @@ -138,13 +138,16 @@ export class Context { */ protected initSDK(name: T): void { this.sdkPromises[name] = new Promise((resolve, reject) => { - this.props.sdks[name].init(this).then(() => { - this.sdks[name] = this.props.sdks[name].sdk; - resolve(this.sdks[name]); - }).catch((e) => { - console.log(`Initialization for SDK ${name.toString()} skipped. Reason: \n ${e}`); - reject(e); - }); + this.props.sdks[name] + .init(this) + .then(() => { + this.sdks[name] = this.props.sdks[name].sdk; + resolve(this.sdks[name]); + }) + .catch((e) => { + console.log(`Initialization for SDK ${name.toString()} skipped. Reason: \n ${e}`); + reject(e); + }); }); } } From a399e508b9783a6412c9b3bb80aac689bb692d47 Mon Sep 17 00:00:00 2001 From: art-alexeyenko Date: Thu, 11 Jan 2024 16:39:55 -0500 Subject: [PATCH 3/4] Adjust logging and reject messages --- .../templates/nextjs-xmcloud/src/lib/context/sdk/events.ts | 4 ++-- packages/sitecore-jss-nextjs/src/context/context.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/create-sitecore-jss/src/templates/nextjs-xmcloud/src/lib/context/sdk/events.ts b/packages/create-sitecore-jss/src/templates/nextjs-xmcloud/src/lib/context/sdk/events.ts index 56a7609d77..a9faf3d12a 100644 --- a/packages/create-sitecore-jss/src/templates/nextjs-xmcloud/src/lib/context/sdk/events.ts +++ b/packages/create-sitecore-jss/src/templates/nextjs-xmcloud/src/lib/context/sdk/events.ts @@ -7,9 +7,9 @@ const sdkModule: SDK = { // Events module can't be initialized on the server side // We also don't want to initialize it in development mode if (typeof window === 'undefined') - throw "Events SDK can't be initialized in server context"; + throw 'Browser Events SDK is not initialized in server context'; if (process.env.NODE_ENV === 'development') - throw 'Events SDK is not initialized in development environment'; + throw 'Browser Events SDK is not initialized in development environment'; await Events.init({ siteName: props.siteName, diff --git a/packages/sitecore-jss-nextjs/src/context/context.ts b/packages/sitecore-jss-nextjs/src/context/context.ts index b934c0c1d4..93767fbef5 100644 --- a/packages/sitecore-jss-nextjs/src/context/context.ts +++ b/packages/sitecore-jss-nextjs/src/context/context.ts @@ -145,7 +145,7 @@ export class Context { resolve(this.sdks[name]); }) .catch((e) => { - console.log(`Initialization for SDK ${name.toString()} skipped. Reason: \n ${e}`); + // if init rejects, getSDK will reject too now reject(e); }); }); From c07eb68d8d79053af6d53d526a2534216acc4c6c Mon Sep 17 00:00:00 2001 From: art-alexeyenko Date: Fri, 12 Jan 2024 16:05:03 -0500 Subject: [PATCH 4/4] changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 11f5cfc0ab..85611bbdfb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,6 +32,7 @@ Our versioning strategy is as follows: * `[sitecore-jss-nextjs]` Fix redirects middleware when source URL contains query string. ([#1696](https://github.com/Sitecore/jss/pull/1702)) * `[templates/nextjs-sxa]` Fix feature `show Grid column` in Experience Editor. ([#1704](https://github.com/Sitecore/jss/pull/1704)) * `[templates/nextjs-xmcloud]` Fix double registration of BYOC components ([#1707](https://github.com/Sitecore/jss/pull/1707)) ([#1709](https://github.com/Sitecore/jss/pull/1709)) +* `[sitecore-jss-nextjs] [templates/nextjs-xmcloud]` SDK initialization rejections are now correctly handled. Errors should no longer occur after getSDK() promises resolve when they shouldn't (for example, getting Events SDK in development environment) ([#1712](https://github.com/Sitecore/jss/pull/1712)) ### 🛠 Breaking Changes