Make notifications SSR ready (#341)

Properly check if window exists
This commit is contained in:
Andrew Prifer 2022-11-22 14:24:23 +01:00 committed by GitHub
parent 8c325c901a
commit 7ef016f3f0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -64,8 +64,10 @@ const createHandler =
}
}
// @ts-ignore
return window?.[globalVariableNames.notifications]?.notify[type](...args)
return typeof window !== 'undefined'
? // @ts-ignore
window[globalVariableNames.notifications]?.notify[type](...args)
: undefined
}
export const notify: Notifiers = {
@ -75,16 +77,18 @@ export const notify: Notifiers = {
error: createHandler('error'),
}
window?.addEventListener('error', (e) => {
if (typeof window !== 'undefined') {
window.addEventListener('error', (e) => {
notify.error(
`An error occurred`,
`<pre>${e.message}</pre>\n\nSee **console** for details.`,
)
})
window?.addEventListener('unhandledrejection', (e) => {
window.addEventListener('unhandledrejection', (e) => {
notify.error(
`An error occurred`,
`<pre>${e.reason}</pre>\n\nSee **console** for details.`,
)
})
}