diff --git a/CHANGELOG.md b/CHANGELOG.md index d44c9183ff..49beed71e7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ Our versioning strategy is as follows: * `[templates/node-headless-ssr-proxy]` `[node-headless-ssr-proxy]` Add sc_site qs parameter to Layout Service requests by default ([#1660](https://github.com/Sitecore/jss/pull/1660)) * `[templates/nextjs-sxa]` Fixed Image component when there is using Banner variant which set property background-image when image is empty. ([#1689](https://github.com/Sitecore/jss/pull/1689)) ([#1692](https://github.com/Sitecore/jss/pull/1692)) * `[templates/nextjs-sxa]` Fix feature `show Grid column` in Experience Editor. ([#1704](https://github.com/Sitecore/jss/pull/1704)) +* `[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) [#1715](https://github.com/Sitecore/jss/pull/1715) [#1716](https://github.com/Sitecore/jss/pull/1716)) ### ๐Ÿงน Chores @@ -32,7 +33,7 @@ Our versioning strategy is as follows: ### ๐ŸŽ‰ New Features & Improvements * `[templates/react]` `[templates/angular]` `[templates/vue]` `[templates/node-headless-ssr-proxy]` `[templates/node-headless-ssr-experience-edge]` ([#1647](https://github.com/Sitecore/jss/pull/1647)) ([#1672](https://github.com/Sitecore/jss/pull/1672)) Switch from using JSS_APP_NAME to SITECORE_SITE_NAME - environment and config variables in React, Vue, Angular templates as well as ssr node proxy apps templates have been renamed. -* `[templates/nextjs]` `[sitecore-jss-nextjs]` `[sitecore-jss]` ([#1640](https://github.com/Sitecore/jss/pull/1640)) ([#1662](https://github.com/Sitecore/jss/pull/1662))([#1661](https://github.com/Sitecore/jss/pull/1661)) ([#1672](https://github.com/Sitecore/jss/pull/1672)) ([#1675](https://github.com/Sitecore/jss/pull/1675)) ([#1710](https://github.com/Sitecore/jss/pull/1710)) ([#1712](https://github.com/Sitecore/jss/pull/1712) [#1715](https://github.com/Sitecore/jss/pull/1715)) Sitecore Edge Platform and Context support: +* `[templates/nextjs]` `[sitecore-jss-nextjs]` `[sitecore-jss]` ([#1640](https://github.com/Sitecore/jss/pull/1640)) ([#1662](https://github.com/Sitecore/jss/pull/1662))([#1661](https://github.com/Sitecore/jss/pull/1661)) ([#1672](https://github.com/Sitecore/jss/pull/1672)) ([#1675](https://github.com/Sitecore/jss/pull/1675)) ([#1710](https://github.com/Sitecore/jss/pull/1710)) Sitecore Edge Platform and Context support: * Introducing the _clientFactory_ property. This property can be utilized by GraphQL-based services, the previously used _endpoint_ and _apiKey_ properties are deprecated. The _clientFactory_ serves as the central hub for executing GraphQL requests within the application. * New SITECORE_EDGE_CONTEXT_ID environment variable has been added. * The JSS_APP_NAME environment variable has been updated and is now referred to as SITECORE_SITE_NAME. diff --git a/packages/sitecore-jss-nextjs/src/context/context.test.ts b/packages/sitecore-jss-nextjs/src/context/context.test.ts index 7f1838265e..dcade0ff67 100644 --- a/packages/sitecore-jss-nextjs/src/context/context.test.ts +++ b/packages/sitecore-jss-nextjs/src/context/context.test.ts @@ -171,15 +171,16 @@ describe('Context', () => { expect(context.siteName).to.equal('website-1'); }); - it('should catch and log when SDK initialization rejects', () => { - const consoleSpy = sinon.spy(console, 'log'); + it('should not fail context init when an SDK init throws', (done) => { 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(); + try { + context.init(); + } catch (e) { + done(e); + } finally { + done(); + } }); it('should reject when getting SDK that rejected initialization', (done) => { @@ -189,7 +190,7 @@ describe('Context', () => { context .getSDK('Error') .then(() => { - throw new Error('should not resolve'); + done('should not resolve'); }) .catch((e) => { expect(e).to.be.equal('Cannot init Error'); diff --git a/packages/sitecore-jss-nextjs/src/context/context.ts b/packages/sitecore-jss-nextjs/src/context/context.ts index 93767fbef5..3e020395d2 100644 --- a/packages/sitecore-jss-nextjs/src/context/context.ts +++ b/packages/sitecore-jss-nextjs/src/context/context.ts @@ -93,6 +93,8 @@ export class Context { */ protected sdkPromises: { [module in keyof SDKModules]?: Promise } = {}; + protected sdkErrors: { [module in keyof SDKModules]?: string } = {}; + constructor(protected props: ContextConfig) { this.sitecoreEdgeUrl = props.sitecoreEdgeUrl; this.sitecoreEdgeContextId = props.sitecoreEdgeContextId; @@ -127,7 +129,15 @@ export class Context { * @returns initialized SDK */ public getSDK = (name: T): Promise => { - return this.sdkPromises[name] || Promise.reject(`Unknown SDK '${String(name)}'`); + if (!this.sdkPromises[name]) { + return Promise.reject(`Unknown SDK '${String(name)}'`); + } else { + return this.sdkPromises[name]!.then((result) => { + return ( + (this.sdkErrors[name] && Promise.reject(this.sdkErrors[name])) || Promise.resolve(result) + ); + }); + } }; /** @@ -137,7 +147,7 @@ export class Context { * @returns {void} */ protected initSDK(name: T): void { - this.sdkPromises[name] = new Promise((resolve, reject) => { + this.sdkPromises[name] = new Promise((resolve) => { this.props.sdks[name] .init(this) .then(() => { @@ -145,8 +155,9 @@ export class Context { resolve(this.sdks[name]); }) .catch((e) => { - // if init rejects, getSDK will reject too now - reject(e); + // if init rejects, we mark SDK as failed - so getSDK call would reject with a reason + this.sdkErrors[name] = e; + resolve(undefined); }); }); }