Openstatus www.openstatus.dev

fix: custom domain prefix (#1538)

authored by

Maximilian Kaske and committed by
GitHub
92961201 11999bb7

+15 -4
+15 -4
apps/status-page/src/hooks/use-pathname-prefix.ts
··· 1 1 "use client"; 2 2 3 + import { useTRPC } from "@/lib/trpc/client"; 4 + import { useQuery } from "@tanstack/react-query"; 5 + import { useParams } from "next/navigation"; 3 6 import { useEffect, useState } from "react"; 4 7 5 8 export function usePathnamePrefix() { 9 + const trpc = useTRPC(); 10 + const { domain } = useParams<{ domain: string }>(); 11 + const { data: page } = useQuery( 12 + trpc.statusPage.get.queryOptions({ slug: domain }), 13 + ); 6 14 const [prefix, setPrefix] = useState(""); 7 15 8 16 useEffect(() => { 9 17 if (typeof window !== "undefined") { 10 18 const hostnames = window.location.hostname.split("."); 11 19 const pathnames = window.location.pathname.split("/"); 20 + const isCustomDomain = window.location.hostname === page?.customDomain; 21 + 12 22 if ( 13 - hostnames.length > 2 && 14 - hostnames[0] !== "www" && 15 - !window.location.hostname.endsWith(".vercel.app") 23 + isCustomDomain || 24 + (hostnames.length > 2 && 25 + hostnames[0] !== "www" && 26 + !window.location.hostname.endsWith(".vercel.app")) 16 27 ) { 17 28 setPrefix(""); 18 29 } else { 19 30 setPrefix(pathnames[1]); 20 31 } 21 32 } 22 - }, []); 33 + }, [page?.customDomain]); 23 34 24 35 return prefix; 25 36 }