28 lines
919 B
TypeScript
28 lines
919 B
TypeScript
import { usePathname } from "next/navigation"
|
|
import { useEffect, useState } from "react"
|
|
|
|
export function removeMultipleSlashes(str: string) {
|
|
return str.replaceAll(/\/\/+/g, "/")
|
|
}
|
|
|
|
export function removeTrailingSlash(pathname: string) {
|
|
if (pathname.endsWith("/")) {
|
|
// Remove the trailing slash
|
|
return pathname.slice(0, -1)
|
|
}
|
|
return pathname
|
|
}
|
|
|
|
/*** This hook is used to get the current pathname (as reflected in window.location.href) of the page. During ssr, the value from usePathname()
|
|
* is the value return from NextResponse.rewrite() (e.g. the path from the app directory) instead of the actual pathname from the URL.
|
|
*/
|
|
export function useLazyPathname() {
|
|
const pathName = usePathname()
|
|
const [updatedPathName, setUpdatedPathName] = useState<string | null>(null)
|
|
|
|
useEffect(() => {
|
|
setUpdatedPathName(pathName)
|
|
}, [pathName])
|
|
return updatedPathName ? updatedPathName : null
|
|
}
|