Files
Charis 34e0361708 feat(docs): send pageview and global crash metrics on preview (#54424)
## Problem

Studio sends pageview and global crash counters to the frontend metrics
endpoint on non-production deployments (#54331). Docs sends neither, so
there is no equivalent signal for docs previews.

## Solution

Port the Studio implementation to `apps/docs`:

- **Gate**: new `IS_PREVIEW` constant, true only on Vercel preview
deployments of the hosted platform. It is false in production, local
dev, self-hosted, and the Nimbus flavor. Docs has no long-running
staging environment, so there is no staging branch in the gate.
- **Sender**: `lib/frontend-metrics.ts`, the same fire-and-forget
`text/plain` POST as Studio, tagged `app: 'docs'`.
- **Pageview**: `usePageviewMetric` sends one count on hydration and on
each pathname change. It is mounted from the root layout through a small
client component.
- **Crash**: `app/error.tsx` and `app/global-error.tsx` each send one
`global_crash`. A given error reaches only one of the two boundaries,
and a `WeakSet` counts each error object once, so effect re-runs do not
double count.
- **Nimbus**: docs had no client-readable Nimbus check, so the gate
reads `NEXT_PUBLIC_IS_NIMBUS` (same name and semantics as Studio) and
the variable is added to the docs build env in `turbo.jsonc`.

No CSP change is needed because docs does not set a
Content-Security-Policy.

## Additional context

`NEXT_PUBLIC_IS_NIMBUS` must be set on the Nimbus docs project for the
Nimbus exclusion to take effect. The metrics also require
`NEXT_PUBLIC_IS_PLATFORM=true` on preview deployments.

## Review instructions

1. Open the docs preview deployment for this PR with the network tab
open.
2. Load any page and confirm one POST to the metrics endpoint with
`metric_name: "pageview"` and `app: "docs"`.
3. Navigate to another page client-side and confirm one more POST.
Changing only the hash or search params should not send one.
4. Run `pnpm -F docs exec vitest run lib/frontend-metrics.test.ts
hooks/usePageviewMetric.test.tsx`.
2026-12-01 12:01:27 -05:00

95 lines
3.1 KiB
TypeScript

import 'ui-patterns/ShimmeringLoader/index.css'
import '../styles/globals.css'
import '../styles/prism-okaidia.css'
import { GlobalProviders } from '~/features/app.providers'
import { IaNavRouteProvider } from '~/features/IaNav'
import { IA_NAV_CACHE_SCRIPT } from '~/features/IaNav/constants'
import { DOCS_CONTENT_CONTAINER_ID } from '~/features/ui/helpers.constants'
import { TopNavSkeleton } from '~/layouts/MainSkeleton'
import { BASE_PATH, IS_PRODUCTION } from '~/lib/constants'
import { getCustomContent } from '~/lib/custom-content/getCustomContent'
import { TelemetryTagManager } from 'common'
import { SIGNED_IN_HINT_SCRIPT } from 'common/gotrue'
import { LLMS_TXT_DIRECTIVE_HTML } from 'common/markdown-affordance'
import { genFaviconData } from 'common/MetaFavicons/app-router'
import type { Metadata, Viewport } from 'next'
import { SkipToContent } from 'ui-patterns/SkipToContent'
import { PageviewMetric } from '@/features/telemetry/PageviewMetric.client'
import { inter, manrope } from '@/fonts'
const { metadataApplicationName, metadataTitle } = getCustomContent([
'metadata:application_name',
'metadata:title',
])
const FAVICON_ROUTE = process.env.NODE_ENV === 'development' ? '/favicon/local' : '/favicon'
const metadata: Metadata = {
applicationName: metadataApplicationName,
title: metadataTitle,
description:
'Supabase is the Postgres development platform providing all the backend features you need to build a product.',
metadataBase: new URL('https://supabase.com'),
icons: genFaviconData(BASE_PATH, FAVICON_ROUTE),
manifest: `${BASE_PATH}${FAVICON_ROUTE}/manifest.json`,
robots: {
index: IS_PRODUCTION,
follow: IS_PRODUCTION,
},
openGraph: {
type: 'article',
authors: 'Supabase',
url: `${BASE_PATH}`,
images: `${BASE_PATH}/img/supabase-og-image.png`,
publishedTime: new Date().toISOString(),
modifiedTime: new Date().toISOString(),
},
twitter: {
card: 'summary_large_image',
site: '@supabase',
creator: '@supabase',
images: `${BASE_PATH}/img/supabase-og-image.png`,
},
}
const viewport: Viewport = {
themeColor: '#1E1E1E',
}
const RootLayout = ({ children }: { children: React.ReactNode }) => {
return (
<html
lang="en"
className={`${manrope.variable} ${inter.variable}`}
data-scroll-behavior="smooth"
suppressHydrationWarning
>
<head>
<script dangerouslySetInnerHTML={{ __html: IA_NAV_CACHE_SCRIPT }} />
<script dangerouslySetInnerHTML={{ __html: SIGNED_IN_HINT_SCRIPT }} />
</head>
<body>
{/* for agents that fetch HTML and never try the markdown path */}
<div
className="sr-only"
aria-hidden="true"
dangerouslySetInnerHTML={{ __html: LLMS_TXT_DIRECTIVE_HTML }}
/>
<SkipToContent href={`#${DOCS_CONTENT_CONTAINER_ID}`} />
<TelemetryTagManager />
<GlobalProviders>
<PageviewMetric />
<IaNavRouteProvider>
<TopNavSkeleton>{children}</TopNavSkeleton>
</IaNavRouteProvider>
</GlobalProviders>
</body>
</html>
)
}
export { metadata, viewport }
export default RootLayout