Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[sitecore-jss-nextjs] Reject SDK promise when init was rejected #1712

Merged
merged 4 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ const sdkModule: SDK<typeof Events> = {
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";
art-alexeyenko marked this conversation as resolved.
Show resolved Hide resolved
if (process.env.NODE_ENV === 'development')
throw 'Events SDK is not initialized in development environment';

await Events.init({
siteName: props.siteName,
sitecoreEdgeUrl: props.sitecoreEdgeUrl,
Expand Down
37 changes: 37 additions & 0 deletions packages/sitecore-jss-nextjs/src/context/context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ describe('Context', () => {
},
};

const errorSdk = {
Error: {
sdk: { error: 'yes' },
init: () => {
return new Promise<void>((_, reject) => {
reject('Cannot init Error');
});
},
},
};

const fooInitSpy = sinon.spy(sdks.Foo, 'init');
const barInitSpy = sinon.spy(sdks.Bar, 'init');

Expand Down Expand Up @@ -159,6 +170,32 @@ 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<typeof errorSdk>(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<typeof errorSdk>(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', () => {
Expand Down
17 changes: 11 additions & 6 deletions packages/sitecore-jss-nextjs/src/context/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,17 @@ export class Context<SDKModules extends SDKModulesType> {
* @returns {void}
*/
protected initSDK<T extends keyof SDKModules>(name: T): void {
this.sdkPromises[name] = new Promise((resolve) => {
this.props.sdks[name].init(this).then(() => {
this.sdks[name] = this.props.sdks[name].sdk;

resolve(this.sdks[name]);
});
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);
});
art-alexeyenko marked this conversation as resolved.
Show resolved Hide resolved
});
}
}