diff --git a/example/app/demo/page.tsx b/example/app/demo/page.tsx index c48531e..4ce0443 100644 --- a/example/app/demo/page.tsx +++ b/example/app/demo/page.tsx @@ -5,7 +5,13 @@ import { Reveal } from "@/components/reveal"; import demoImage from "@/assets/image.jpg"; import Link from "next/link"; -export default function DemoPage() { +export default async function DemoPage({ + searchParams, +}: { + searchParams: Promise<{ page: string }>; +}) { + const { page = 0 } = await searchParams; + return ( <>
@@ -20,10 +26,16 @@ export default function DemoPage() {
-
+
Back + +
+ Current: {page} + Page 1 + Page 2 +
diff --git a/src/context.tsx b/src/context.tsx index 8afa885..2f5f971 100644 --- a/src/context.tsx +++ b/src/context.tsx @@ -9,7 +9,7 @@ import { useState, } from "react"; import delegate, { DelegateEvent } from "delegate-it"; -import { usePathname, useRouter } from "next/navigation"; +import { usePathname, useRouter, useSearchParams } from "next/navigation"; import { NavigateOptions } from "next/dist/shared/lib/app-router-context.shared-runtime"; import { isModifiedEvent } from "./utils"; @@ -53,6 +53,7 @@ export function TransitionRouter({ }: TransitionRouterProps) { const router = useRouter(); const pathname = usePathname(); + const searchParams = useSearchParams(); const [stage, setStage] = useState("none"); @@ -70,6 +71,10 @@ export function TransitionRouter({ [leave, router] ); + const fullPath = useMemo(() => { + return pathname + searchParams.toString(); + }, [pathname, searchParams]); + const handleClick = useCallback( (event: DelegateEvent) => { const anchor = event.delegateTarget as HTMLAnchorElement; @@ -77,15 +82,19 @@ export function TransitionRouter({ const ignore = anchor?.getAttribute("data-transition-ignore"); const url = href ? new URL(href, window.location.origin) : null; - const targetPathname = url?.pathname; + const currentUrl = new URL(window.location.href); + + const isSamePage = + url?.pathname === currentUrl.pathname && + url?.search === currentUrl.search; if ( !ignore && href?.startsWith("/") && - targetPathname !== pathname && + !isSamePage && anchor.target !== "_blank" && !isModifiedEvent(event) && - !(href.includes("#") && targetPathname === pathname) + !(href.includes("#") && url?.pathname === pathname) ) { event.preventDefault(); navigate(href, pathname); @@ -127,7 +136,7 @@ export function TransitionRouter({ setStage("entering"); } }; - }, [stage, pathname]); + }, [stage, fullPath]); const value = useMemo( () => ({ stage, navigate, isReady: stage !== "entering" }),