use-host-routing.ts1.4 KBView on GitHub import {
appUrl,
hasSplitHosts,
isAppHost,
isMarketingHost,
isMarketingPath,
leaveForOrigin,
marketingUrl,
} from '@/lib/marketing-host';
import { useLocation } from 'react-router';
import { useEffect } from 'react';
/**
* Keeps each origin to its own half of the site once the marketing site and the app
* have separate hostnames (see lib/marketing-host.ts).
*
* Marketing paths that land on the app host — typically old inbound links and stale
* search results for mail.cedarcopilot.com/pricing — move to the marketing origin, and
* app paths that land on the marketing host move to the app origin (where the session
* cookie lives, so the user stays signed in).
*
* Crossing origins is a full-page navigation — see leaveForOrigin.
*
* No-ops entirely until VITE_PUBLIC_MARKETING_URL names a host distinct from the app's.
*/
export function useHostRouting() {
const location = useLocation();
useEffect(() => {
if (!hasSplitHosts()) return;
const path = `${location.pathname}${location.search}${location.hash}`;
const marketing = isMarketingPath(location.pathname);
if (isAppHost() && marketing) {
const target = marketingUrl(path);
if (target) leaveForOrigin(target);
return;
}
if (isMarketingHost() && !marketing) {
const target = appUrl(path);
if (target) leaveForOrigin(target);
}
}, [location.pathname, location.search, location.hash]);
}