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