Documentation
Documentation
import { posthog } from '@c15t/integrations/posthog';
import {
createRootRoute,
HeadContent,
Outlet,
Scripts,
} from '@tanstack/react-router';
import { createServerFn } from '@tanstack/react-start';
import {
ConsentBanner,
ConsentDialog,
ConsentDialogLink,
ConsentRoot,
} from 'c15t/tanstack-start';
import {
consentLoaderOptions,
createConsentStateHandler,
} from 'c15t/tanstack-start/server';
// Start's compiler keeps the handler and the bundled policy on the server.
const getConsentState = createServerFn({ method: 'GET' }).handler(
createConsentStateHandler()
);
const scripts = [
posthog({
id: 'phc_your_project_key',
initOptions: { cookieless_mode: 'never' },
loadMode: 'after-consent',
}),
];
const RootComponent = () => {
const { consent } = Route.useLoaderData();
return (
<html lang="en">
<head>
<HeadContent />
</head>
<body>
<ConsentRoot state={consent} scripts={scripts}>
<Outlet />
<ConsentBanner />
<ConsentDialog />
<footer>
<ConsentDialogLink>Privacy settings</ConsentDialogLink>
</footer>
</ConsentRoot>
<Scripts />
</body>
</html>
);
};
export const Route = createRootRoute({
...consentLoaderOptions,
component: RootComponent,
head: () => ({
meta: [
{ charSet: 'utf-8' },
{ content: 'width=device-width, initial-scale=1', name: 'viewport' },
],
}),
loader: async () => ({ consent: await getConsentState() }),
});