Merged in fix/pathname-login (pull request #530)
fix: create useLazyPathname hook Approved-by: Michael Zetterberg
This commit is contained in:
@@ -1,6 +1,5 @@
|
|||||||
"use client"
|
"use client"
|
||||||
|
|
||||||
import { usePathname } from "next/navigation"
|
|
||||||
import { PropsWithChildren, useEffect } from "react"
|
import { PropsWithChildren, useEffect } from "react"
|
||||||
|
|
||||||
import { login } from "@/constants/routes/handleAuth"
|
import { login } from "@/constants/routes/handleAuth"
|
||||||
@@ -8,6 +7,7 @@ import { login } from "@/constants/routes/handleAuth"
|
|||||||
import Link from "@/components/TempDesignSystem/Link"
|
import Link from "@/components/TempDesignSystem/Link"
|
||||||
import { LinkProps } from "@/components/TempDesignSystem/Link/link"
|
import { LinkProps } from "@/components/TempDesignSystem/Link/link"
|
||||||
import useLang from "@/hooks/useLang"
|
import useLang from "@/hooks/useLang"
|
||||||
|
import { useLazyPathname } from "@/hooks/useLazyPathname"
|
||||||
import { trackLoginClick } from "@/utils/tracking"
|
import { trackLoginClick } from "@/utils/tracking"
|
||||||
|
|
||||||
import { TrackingPosition } from "@/types/components/tracking"
|
import { TrackingPosition } from "@/types/components/tracking"
|
||||||
@@ -25,7 +25,11 @@ export default function LoginButton({
|
|||||||
color?: LinkProps["color"]
|
color?: LinkProps["color"]
|
||||||
}>) {
|
}>) {
|
||||||
const lang = useLang()
|
const lang = useLang()
|
||||||
const pathName = usePathname()
|
const pathName = useLazyPathname()
|
||||||
|
|
||||||
|
const href = pathName
|
||||||
|
? `${login[lang]}?redirectTo=${encodeURIComponent(pathName)}`
|
||||||
|
: login[lang]
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
document
|
document
|
||||||
@@ -43,7 +47,7 @@ export default function LoginButton({
|
|||||||
className={className}
|
className={className}
|
||||||
id={trackingId}
|
id={trackingId}
|
||||||
color={color}
|
color={color}
|
||||||
href={`${login[lang]}?redirectTo=${encodeURIComponent(pathName)}`}
|
href={href}
|
||||||
prefetch={false}
|
prefetch={false}
|
||||||
>
|
>
|
||||||
{children}
|
{children}
|
||||||
|
|||||||
16
hooks/useLazyPathname.ts
Normal file
16
hooks/useLazyPathname.ts
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
"use client"
|
||||||
|
import { usePathname } from "next/navigation"
|
||||||
|
import { useEffect, useState } from "react"
|
||||||
|
|
||||||
|
/*** 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
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user