Skip to content

Commit

Permalink
fix(usePathname): prevent duplication of subpath in pathname
Browse files Browse the repository at this point in the history
  • Loading branch information
vgeorge committed Dec 17, 2024
1 parent 55c04c3 commit b1a13af
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions app/scripts/utils/use-pathname.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,18 @@ 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(process.env.PUBLIC_URL ?? '', '')
: ''
);

useEffect(() => {
if (typeof window === 'undefined') return;

const updatePathname = () => {
setPathname(window.location.pathname);
setPathname(
window.location.pathname.replace(process.env.PUBLIC_URL ?? '', '')
);
};

// Listen to popstate events (back/forward navigation)
Expand Down

0 comments on commit b1a13af

Please sign in to comment.