์ž‘๊ฐ€:
    ์ƒ์„ฑ:2026-06-22๋งˆ์ง€๋ง‰ ์—…๋ฐ์ดํŠธ:2026-09-27

    Preact ํ†ตํ•ฉ: usePathname ํ›… ๋ฌธ์„œ

    usePathname ํ›…์€ ๋กœ์ผ€์ผ ์„ธ๊ทธ๋จผํŠธ๊ฐ€ ์ œ๊ฑฐ๋œ ํ˜„์žฌ ๋ธŒ๋ผ์šฐ์ €์˜ ๊ฒฝ๋กœ๋ช…(pathname)์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ด ํ›…์€ ๋กœ์ผ€์ผ ์ ‘๋‘์‚ฌ๋ฅผ ์ˆ˜๋™์œผ๋กœ ์ œ๊ฑฐํ•  ํ•„์š” ์—†์ด ์–ด๋–ค ๋‚ด๋น„๊ฒŒ์ด์…˜ ํ•ญ๋ชฉ์ด ํ™œ์„ฑํ™”๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋Š” ๋“ฑ ๋กœ์ผ€์ผ ์ธ์‹ ๋‚ด๋น„๊ฒŒ์ด์…˜์„ ๊ตฌ์ถ•ํ•  ๋•Œ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค.

    Preact์—์„œ usePathname ๊ฐ€์ ธ์˜ค๊ธฐ

    typescript
    import { usePathname } from "preact-intlayer";
    

    ๊ฐœ์š”

    usePathname์€ window.location.pathname์„ ์ฝ๊ณ  getPathWithoutLocale์„ ํ†ตํ•ด ๋กœ์ผ€์ผ ์ ‘๋‘์‚ฌ๋ฅผ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค. ๋ธŒ๋ผ์šฐ์ €๊ฐ€ popstate ์ด๋ฒคํŠธ(๋’ค๋กœ/์•ž์œผ๋กœ ๊ฐ€๊ธฐ ๋‚ด๋น„๊ฒŒ์ด์…˜)๋ฅผ ๋ฐœ์ƒ์‹œํ‚ฌ ๋•Œ๋งˆ๋‹ค ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋‹ค์‹œ ๋ Œ๋”๋งํ•ฉ๋‹ˆ๋‹ค. ์„œ๋ฒ„ ์‚ฌ์ด๋“œ ๋ Œ๋”๋ง(SSR) ์ค‘์—๋Š” ๋นˆ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

    ์‚ฌ์šฉ๋ฒ•

    src/components/NavItem.tsx
    import type { FunctionComponent } from "preact";
    import { usePathname } from "preact-intlayer";
    
    type NavItemProps = {
      href: string;
      label: string;
    };
    
    const NavItem: FunctionComponent<NavItemProps> = ({ href, label }) => {
      const pathname = usePathname();
      const isActive = pathname === href;
    
      return (
        <a href={href} aria-current={isActive ? "page" : undefined}>
          {label}
        </a>
      );
    };
    
    export default NavItem;
    

    ๋ฐ˜ํ™˜ ๊ฐ’

    ํƒ€์ž…์„ค๋ช…
    string๋กœ์ผ€์ผ ์ ‘๋‘์‚ฌ๊ฐ€ ์—†๋Š” ํ˜„์žฌ ๊ฒฝ๋กœ๋ช…. ์„œ๋ฒ„ ์‚ฌ์ด๋“œ ๋ Œ๋”๋ง(SSR) ์ค‘์—๋Š” ๋นˆ ๋ฌธ์ž์—ด์ž…๋‹ˆ๋‹ค.

    ๋™์ž‘ ๋ฐฉ์‹

    • ๋กœ์ผ€์ผ ์ œ๊ฑฐ (Locale stripping): URL ์•ž์˜ ๋กœ์ผ€์ผ ์„ธ๊ทธ๋จผํŠธ๋ฅผ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค(์˜ˆ: /ko/dashboard โ†’ /dashboard).
    • ๋ฐ˜์‘ํ˜•: popstate ์ด๋ฒคํŠธ(๋ธŒ๋ผ์šฐ์ € ๋’ค๋กœ / ์•ž์œผ๋กœ ๊ฐ€๊ธฐ ๋‚ด๋น„๊ฒŒ์ด์…˜) ์‹œ ์ž๋™์œผ๋กœ ์—…๋ฐ์ดํŠธ๋ฉ๋‹ˆ๋‹ค.
    • SSR ์•ˆ์ „์„ฑ: window๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์„ ๋•Œ ""๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
    • ์ •๋ฆฌ (Cleanup): ์ปดํฌ๋„ŒํŠธ ๋งˆ์šดํŠธ ํ•ด์ œ ์‹œ popstate ๋ฆฌ์Šค๋„ˆ๊ฐ€ ์ž๋™์œผ๋กœ ์ œ๊ฑฐ๋ฉ๋‹ˆ๋‹ค.

    ์˜ˆ์ œ

    src/components/Sidebar.tsx
    import type { FunctionComponent } from "preact";
    import { usePathname } from "preact-intlayer";
    
    const links = [
      { href: "/dashboard", label: "๋Œ€์‹œ๋ณด๋“œ" },
      { href: "/settings", label: "์„ค์ •" },
    ];
    
    const Sidebar: FunctionComponent = () => {
      const pathname = usePathname();
    
      return (
        <nav>
          {links.map(({ href, label }) => (
            <a
              key={href}
              href={href}
              style={{ fontWeight: pathname === href ? "bold" : "normal" }}
            >
              {label}
            </a>
          ))}
        </nav>
      );
    };
    
    export default Sidebar;
    

    ๊ด€๋ จ ํ•ญ๋ชฉ