diff --git a/apps/web/src/components/common/ClientLayout.tsx b/apps/web/src/components/common/ClientLayout.tsx index 4fe404d3..6ae250b1 100644 --- a/apps/web/src/components/common/ClientLayout.tsx +++ b/apps/web/src/components/common/ClientLayout.tsx @@ -9,6 +9,7 @@ import { reportError } from "@/utils/sentry"; import { ErrorBoundary } from "./ErrorBoundary"; import { GlobalToaster } from "./GlobalToast"; +import { InAppNavigationTracker } from "./InAppNavigationTracker"; type ClientLayoutProps = { children: ReactNode; @@ -30,6 +31,7 @@ export const ClientLayout = ({ children }: ClientLayoutProps) => { return ( + {children} ); diff --git a/apps/web/src/components/common/InAppNavigationTracker.tsx b/apps/web/src/components/common/InAppNavigationTracker.tsx new file mode 100644 index 00000000..08e1d781 --- /dev/null +++ b/apps/web/src/components/common/InAppNavigationTracker.tsx @@ -0,0 +1,21 @@ +"use client"; + +import { useEffect, useRef } from "react"; +import { usePathname } from "next/navigation"; + +import { markInAppNavigation } from "@/utils/navigation"; + +/** + * 처음 들어온 화면에서 다른 화면으로 이동하면 앱 안 이동이 있었다고 기록함 (GLB-239) + * 뒤로가기 버튼이 이 기록으로 이전 화면이 앱 안에 있는지 판단함 + */ +export const InAppNavigationTracker = () => { + const pathname = usePathname(); + const entryPathnameRef = useRef(pathname); + + useEffect(() => { + if (pathname !== entryPathnameRef.current) markInAppNavigation(); + }, [pathname]); + + return null; +}; diff --git a/apps/web/src/utils/navigation.test.ts b/apps/web/src/utils/navigation.test.ts index 1100fd63..21872b8e 100644 --- a/apps/web/src/utils/navigation.test.ts +++ b/apps/web/src/utils/navigation.test.ts @@ -1,14 +1,26 @@ import { afterEach, describe, expect, it, vi } from "vitest"; -import { goBackOrFallback } from "./navigation"; +import { goBackOrFallback, hasInAppNavigation, markInAppNavigation } from "./navigation"; const createRouter = () => ({ back: vi.fn(), push: vi.fn() }); +const stubWindow = (historyLength: number) => { + const store = new Map(); + vi.stubGlobal("window", { + history: { length: historyLength }, + sessionStorage: { + getItem: (key: string) => store.get(key) ?? null, + setItem: (key: string, value: string) => store.set(key, value), + }, + }); +}; + afterEach(() => vi.unstubAllGlobals()); describe("goBackOrFallback", () => { - it("방문 기록이 있으면 뒤로 감", () => { - vi.stubGlobal("window", { history: { length: 3 } }); + it("앱 안에서 이동해 온 화면이면 뒤로 감", () => { + stubWindow(3); + markInAppNavigation(); const router = createRouter(); goBackOrFallback(router, "/profile"); @@ -17,8 +29,8 @@ describe("goBackOrFallback", () => { expect(router.push).not.toHaveBeenCalled(); }); - it("새 탭으로 바로 들어와 방문 기록이 없으면 기본 경로로 이동함", () => { - vi.stubGlobal("window", { history: { length: 1 } }); + it("새 탭으로 바로 들어와 history.length가 2여도 앱 안 이동 기록이 없으면 기본 경로로 이동함", () => { + stubWindow(2); const router = createRouter(); goBackOrFallback(router, "/profile"); @@ -26,4 +38,25 @@ describe("goBackOrFallback", () => { expect(router.push).toHaveBeenCalledWith("/profile"); expect(router.back).not.toHaveBeenCalled(); }); + + it("sessionStorage를 쓸 수 없으면 기본 경로로 이동함", () => { + vi.stubGlobal("window", { + history: { length: 5 }, + sessionStorage: { + getItem: () => { + throw new Error("blocked"); + }, + setItem: () => { + throw new Error("blocked"); + }, + }, + }); + const router = createRouter(); + + markInAppNavigation(); + goBackOrFallback(router, "/"); + + expect(hasInAppNavigation()).toBe(false); + expect(router.push).toHaveBeenCalledWith("/"); + }); }); diff --git a/apps/web/src/utils/navigation.ts b/apps/web/src/utils/navigation.ts index fe0e22cd..fd7ba51c 100644 --- a/apps/web/src/utils/navigation.ts +++ b/apps/web/src/utils/navigation.ts @@ -3,13 +3,41 @@ type BackRouter = { push: (href: string) => void; }; +/** 이 탭에서 앱 안 화면 이동이 한 번이라도 있었는지 기록하는 sessionStorage 키 */ +const IN_APP_NAVIGATION_KEY = "globber:in-app-navigated"; + +/** + * 앱 안에서 화면을 이동했다고 기록함 + * 새 탭은 sessionStorage가 비어 있어, 바로 들어온 화면과 앱 안에서 이동해 온 화면을 구분할 수 있음 + */ +export const markInAppNavigation = () => { + try { + window.sessionStorage.setItem(IN_APP_NAVIGATION_KEY, "1"); + } catch { + // 시크릿 모드 등에서 저장소를 쓸 수 없으면 기록하지 않음 (뒤로가기는 기본 경로로 감) + } +}; + +/** + * 이 탭에서 앱 안 화면 이동이 있었는지 확인함 + * @returns 이동 기록이 있으면 true + */ +export const hasInAppNavigation = (): boolean => { + try { + return window.sessionStorage.getItem(IN_APP_NAVIGATION_KEY) === "1"; + } catch { + return false; + } +}; + /** - * 돌아갈 방문 기록이 있으면 뒤로 가고, 새 탭·링크로 바로 들어와 기록이 없으면 기본 경로로 이동함 (GLB-239) + * 앱 안에서 이동해 온 화면이면 뒤로 가고, 새 탭·링크로 바로 들어온 화면이면 기본 경로로 이동함 (GLB-239) + * 새 탭은 빈 페이지 기록이 있어 history.length로는 바로 들어온 경우를 구분할 수 없음 * @param router - next/navigation의 router - * @param fallbackHref - 방문 기록이 없을 때 이동할 경로 + * @param fallbackHref - 앱 안 이동 기록이 없을 때 이동할 경로 */ export const goBackOrFallback = (router: BackRouter, fallbackHref: string) => { - if (window.history.length > 1) { + if (hasInAppNavigation()) { router.back(); return; }