diff --git a/app/scripts/utils/use-pathname.ts b/app/scripts/utils/use-pathname.ts index 702649e04..802f3bcf9 100644 --- a/app/scripts/utils/use-pathname.ts +++ b/app/scripts/utils/use-pathname.ts @@ -1,5 +1,20 @@ import { useEffect, useState } from 'react'; +const appPathname = (() => { + try { + const publicUrl = process.env.PUBLIC_URL; + if (!publicUrl || typeof publicUrl !== 'string') return ''; + + // Use base URL fallback + const url = new URL(publicUrl, window.location.origin); + + // Remove trailing slash if present + return url.pathname.replace(/\/$/, ''); + } catch { + return ''; + } +})(); + /** * usePathname * * @@ -13,14 +28,16 @@ import { useEffect, useState } from 'react'; */ export const usePathname = () => { const [pathname, setPathname] = useState( - typeof window !== 'undefined' ? window.location.pathname : '' + typeof window !== 'undefined' + ? window.location.pathname.replace(appPathname, '') + : '' ); useEffect(() => { if (typeof window === 'undefined') return; const updatePathname = () => { - setPathname(window.location.pathname); + setPathname(window.location.pathname.replace(appPathname, '')); }; // Listen to popstate events (back/forward navigation)