Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

safely use window and document objects

Blue Mousef32b71f24c8e
diff --git a/app/pages/Auth/Socialised/index.tsx b/app/pages/Auth/Socialised/index.tsx
index cfd9f10b7..c173b125a 100644
--- a/app/pages/Auth/Socialised/index.tsx
+++ b/app/pages/Auth/Socialised/index.tsx
@@ -21,16 +21,16 @@ const Socialised = (): JSX.Element => {
const [loading, setLoading] = useState<boolean>(true)
const [isError, setIsError] = useState<boolean>(false)
- // For some reason, Google redirects to a hash URL, let's fix it
- const _location = _replace(window.location.href, `${routes.socialised}#`, `${routes.socialised}?`)
+ useEffect(() => {
+ // For some reason, Google redirects to a hash URL, let's fix it
+ const _location = _replace(window.location.href, `${routes.socialised}#`, `${routes.socialised}?`)
- const { searchParams } = new URL(_location)
- const hash = searchParams.get('state')
- const accessToken = searchParams.get('access_token')
- const code = searchParams.get('code')
- const provider = _split(hash, ':')[0]
+ const { searchParams } = new URL(_location)
+ const hash = searchParams.get('state')
+ const accessToken = searchParams.get('access_token')
+ const code = searchParams.get('code')
+ const provider = _split(hash, ':')[0]
- useEffect(() => {
const processCode = async () => {
if (!hash || !provider) {
setIsError(true)
diff --git a/app/pages/CrashHandler/index.tsx b/app/pages/CrashHandler/index.tsx
index d47760b17..7fa11b96c 100644
--- a/app/pages/CrashHandler/index.tsx
+++ b/app/pages/CrashHandler/index.tsx
@@ -1,7 +1,7 @@
import React from 'react'
import _toString from 'lodash/toString'
import { ExclamationTriangleIcon, ChevronDownIcon, ChevronUpIcon } from '@heroicons/react/24/outline'
-import { CONTACT_EMAIL, PAGE_FORCE_REFRESHED } from 'redux/constants'
+import { CONTACT_EMAIL, PAGE_FORCE_REFRESHED, isBrowser } from 'redux/constants'
interface CrashHandlerProps {
children: JSX.Element
@@ -15,6 +15,10 @@ interface CrashHandlerState {
}
const retryPageLoading = () => {
+ if (!isBrowser) {
+ return null
+ }
+
const wasPageForceRefreshed = sessionStorage.getItem(PAGE_FORCE_REFRESHED) === 'true'
if (!wasPageForceRefreshed) {
diff --git a/app/redux/reducers/ui/cache.ts b/app/redux/reducers/ui/cache.ts
index a603b7b52..52c7aacf0 100644
--- a/app/redux/reducers/ui/cache.ts
+++ b/app/redux/reducers/ui/cache.ts
@@ -3,18 +3,21 @@ import _filter from 'lodash/filter'
import _isEmpty from 'lodash/isEmpty'
import { getItem, removeItem, setItem } from 'utils/localstorage'
import {
- getProjectCacheKey, LS_VIEW_PREFS_SETTING, LS_CAPTCHA_VIEW_PREFS_SETTING, getProjectCaptchaCacheKey, getUserFlowCacheKey,
+ getProjectCacheKey, LS_VIEW_PREFS_SETTING, LS_CAPTCHA_VIEW_PREFS_SETTING, getProjectCaptchaCacheKey,
+ getUserFlowCacheKey, isBrowser,
} from 'redux/constants'
import { IUserFlow } from 'redux/models/IUserFlow'
export const getInitialViewPrefs = (LS_VIEW: string) => {
- if (typeof window !== 'undefined' && window.localStorage) {
- const storedPrefs = getItem(LS_VIEW)
- try {
- return storedPrefs
- } catch (e) {
- removeItem(LS_VIEW)
- }
+ if (!isBrowser) {
+ return {}
+ }
+
+ const storedPrefs = getItem(LS_VIEW)
+ try {
+ return storedPrefs
+ } catch (e) {
+ removeItem(LS_VIEW)
}
return {}
diff --git a/app/root.tsx b/app/root.tsx
index c61183a00..7361fbd29 100644
--- a/app/root.tsx
+++ b/app/root.tsx
@@ -9,7 +9,7 @@ import {
ScrollRestoration,
} from '@remix-run/react'
import { store } from 'redux/store'
-import { isDevelopment, whitelist } from 'redux/constants'
+import { isDevelopment, whitelist, isBrowser } from 'redux/constants'
import { Provider } from 'react-redux'
import _map from 'lodash/map'
// @ts-ignore
@@ -39,8 +39,8 @@ const options = {
transition: transitions.SCALE,
}
-if (process.env.NODE_ENV !== 'production') {
- // localStorage.debug = 'swetrix:*'
+if (isBrowser && process.env.NODE_ENV !== 'production') {
+ localStorage.debug = 'swetrix:*'
}
const FONTS_PROVIDER = 'https://fonts.bunny.net'
diff --git a/app/utils/generic.ts b/app/utils/generic.ts
index 7250b6cfd..123167580 100644
--- a/app/utils/generic.ts
+++ b/app/utils/generic.ts
@@ -4,6 +4,8 @@ import _round from 'lodash/round'
import _map from 'lodash/map'
import _reduce from 'lodash/reduce'
+import { isBrowser } from 'redux/constants'
+
const rx = /\.0+$|(\.[0-9]*[1-9])0+$/
const formatterLookup = [
@@ -80,6 +82,10 @@ export const openBrowserWindow = (url: string, width: number, height: number) =>
}
export const loadScript = (url: string) => {
+ if (!isBrowser) {
+ return
+ }
+
const script = document.createElement('script')
script.src = url
script.async = true