diff --git a/apps/docs/components/AuthSmsProviderConfig/AuthSmsProviderConfig.tsx b/apps/docs/components/AuthSmsProviderConfig/AuthSmsProviderConfig.tsx index 2039a78854..357b37ffa7 100644 --- a/apps/docs/components/AuthSmsProviderConfig/AuthSmsProviderConfig.tsx +++ b/apps/docs/components/AuthSmsProviderConfig/AuthSmsProviderConfig.tsx @@ -8,6 +8,7 @@ import MessageBird from './MessageBirdConfig.mdx' import Twilio from './TwilioConfig.mdx' import Vonage from './VonageConfig.mdx' import TextLocal from './TextLocalConfig.mdx' +import { safeHistoryReplaceState } from '~/lib/historyUtils' const reducer = (_, action: (typeof PhoneLoginsItems)[number] | undefined) => { const url = new URL(document.location.href) @@ -16,7 +17,7 @@ const reducer = (_, action: (typeof PhoneLoginsItems)[number] | undefined) => { } else { url.searchParams.delete('showSmsProvider') } - window.history.replaceState(null, '', url) + safeHistoryReplaceState(url.toString()) return action } diff --git a/apps/docs/features/docs/Reference.ui.client.tsx b/apps/docs/features/docs/Reference.ui.client.tsx index bb4afdf2c1..0f4a8bba00 100644 --- a/apps/docs/features/docs/Reference.ui.client.tsx +++ b/apps/docs/features/docs/Reference.ui.client.tsx @@ -3,6 +3,7 @@ import type { HTMLAttributes, PropsWithChildren } from 'react' import { useContext, useEffect, useRef, useState } from 'react' import { useInView } from 'react-intersection-observer' +import { safeHistoryReplaceState } from '~/lib/historyUtils' import { cn, @@ -44,7 +45,7 @@ export function ReferenceSectionWrapper({ initialScrollHappened && window.scrollY > 0 /* Don't update on first navigation to introduction */ ) { - window.history.replaceState(null, '', link) + safeHistoryReplaceState(link) } }, }) diff --git a/apps/docs/layouts/ref/RefSubLayout.tsx b/apps/docs/layouts/ref/RefSubLayout.tsx index 284d29b341..1b72eeb5e5 100644 --- a/apps/docs/layouts/ref/RefSubLayout.tsx +++ b/apps/docs/layouts/ref/RefSubLayout.tsx @@ -6,6 +6,7 @@ import { useNavigationMenuContext } from '~/components/Navigation/NavigationMenu import { menuState } from '~/hooks/useMenuState' import Image from 'next/legacy/image' import { cn } from 'ui' +import { safeHistoryReplaceState } from '~/lib/historyUtils' interface ISectionContainer { id: string @@ -97,7 +98,7 @@ const StickyHeader: FC = ({ icon, ...props }) => { onChange: (inView, entry) => { if (inView && window) highlightSelectedNavItem(entry.target.attributes['data-ref-id'].value) if (inView && props.scrollSpyHeader) { - window.history.replaceState(null, '', entry.target.id) + safeHistoryReplaceState(entry.target.id) // if (setActiveRefItem) setActiveRefItem(entry.target.attributes['data-ref-id'].value) menuState.setMenuActiveRefId(entry.target.attributes['data-ref-id'].value) // router.push(`/reference/javascript/${entry.target.attributes['data-ref-id'].value}`, null, { diff --git a/apps/docs/layouts/ref/RefSubLayoutNonFunc.tsx b/apps/docs/layouts/ref/RefSubLayoutNonFunc.tsx index e841d71da7..c6f466e32f 100644 --- a/apps/docs/layouts/ref/RefSubLayoutNonFunc.tsx +++ b/apps/docs/layouts/ref/RefSubLayoutNonFunc.tsx @@ -4,6 +4,7 @@ import { highlightSelectedNavItem } from 'ui/src/components/CustomHTMLElements/C import { useRouter } from 'next/compat/router' import { useNavigationMenuContext } from '~/components/Navigation/NavigationMenu/NavigationMenu.Context' import { menuState } from '~/hooks/useMenuState' +import { safeHistoryReplaceState } from '~/lib/historyUtils' interface ISectionContainer { id: string @@ -60,7 +61,7 @@ const StickyHeader: FC = (props) => { onChange: (inView, entry) => { if (inView && window) highlightSelectedNavItem(entry.target.attributes['data-ref-id'].value) if (inView && props.scrollSpyHeader) { - window.history.replaceState(null, '', entry.target.id) + safeHistoryReplaceState(entry.target.id) // if (setActiveRefItem) setActiveRefItem(entry.target.attributes['data-ref-id'].value) menuState.setMenuActiveRefId(entry.target.attributes['data-ref-id'].value) // router.push(`/reference/javascript/${entry.target.attributes['data-ref-id'].value}`, null, { diff --git a/apps/docs/lib/historyUtils.ts b/apps/docs/lib/historyUtils.ts new file mode 100644 index 0000000000..ff58d8954b --- /dev/null +++ b/apps/docs/lib/historyUtils.ts @@ -0,0 +1,13 @@ +import { debounce } from 'lodash-es' + +export const safeHistoryReplaceState = debounce((url: string) => { + if (typeof window === 'undefined') return + + if (url === window.location.href) return + + try { + window.history.replaceState(null, '', url) + } catch (error) { + console.warn('Failed to call history.replaceState:', error) + } +}, 120)