fix: ensure only one instance of UnbodyService is created on the production env

This commit is contained in:
Hossein Mehrabi 2023-09-26 15:28:09 +03:30
parent 3a645be467
commit 88026b71bb
No known key found for this signature in database
GPG Key ID: 45C04964191AFAA1
1 changed files with 10 additions and 8 deletions

View File

@ -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