Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions apps/web/src/components/common/ClientLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -30,6 +31,7 @@ export const ClientLayout = ({ children }: ClientLayoutProps) => {
return (
<QueryClientProvider client={queryClient}>
<GlobalToaster />
<InAppNavigationTracker />
<ErrorBoundary>{children}</ErrorBoundary>
</QueryClientProvider>
);
Expand Down
21 changes: 21 additions & 0 deletions apps/web/src/components/common/InAppNavigationTracker.tsx
Original file line number Diff line number Diff line change
@@ -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;
};
43 changes: 38 additions & 5 deletions apps/web/src/utils/navigation.test.ts
Original file line number Diff line number Diff line change
@@ -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<string, string>();
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");
Expand All @@ -17,13 +29,34 @@ 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");

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("/");
});
});
34 changes: 31 additions & 3 deletions apps/web/src/utils/navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Loading