From 88026b71bbb9a12b6ece7ab5e9f4e36b2499a5b7 Mon Sep 17 00:00:00 2001 From: Hossein Mehrabi Date: Tue, 26 Sep 2023 15:28:09 +0330 Subject: [PATCH] fix: ensure only one instance of UnbodyService is created on the production env --- src/services/unbody/unbody.service.ts | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/services/unbody/unbody.service.ts b/src/services/unbody/unbody.service.ts index a34a3a7..bb33dc2 100644 --- a/src/services/unbody/unbody.service.ts +++ b/src/services/unbody/unbody.service.ts @@ -1576,20 +1576,22 @@ export class UnbodyService { }, []) } -const _globalThis = globalThis as any -if (!_globalThis.unbodyApi) - _globalThis.unbodyApi = new UnbodyService( - process.env.UNBODY_API_KEY || '', - process.env.UNBODY_PROJECT_ID || '', - ) - const unbodyApi: UnbodyService = process.env.NODE_ENV === 'development' ? new UnbodyService( process.env.UNBODY_API_KEY || '', process.env.UNBODY_PROJECT_ID || '', ) - : _globalThis.unbodyApi + : (() => { + const _globalThis = globalThis as any + if (!_globalThis.unbodyApi) + _globalThis.unbodyApi = new UnbodyService( + process.env.UNBODY_API_KEY || '', + process.env.UNBODY_PROJECT_ID || '', + ) + + return _globalThis.unbodyApi + })() unbodyApi.onChange(async (oldData, data, changes, firstLoad) => { if (firstLoad || isBuildTime() || !sendDiscordNotifications) return