Files
supabase/apps/www/app/layout.tsx
Charis fd01db4691 feat(www): send pageview and global crash metrics on preview (#54425)
## Problem

Studio sends pageview and global crash counters to the frontend metrics
endpoint on non-production deployments (#54331), and docs is getting the
same in #54424. The marketing site sends neither.

## Solution

Port the same implementation to `apps/www`:

- **Gate**: the existing `IS_PREVIEW` constant (`NEXT_PUBLIC_VERCEL_ENV
=== 'preview'`). Nothing is sent in production or local dev.
- **Sender**: `lib/frontend-metrics.ts`, the same fire-and-forget
`text/plain` POST as Studio, tagged `app: 'www'`.
- **Pageview**: `usePageviewMetric` sends one count on hydration and on
each pathname change, and skips while the pathname is not yet available.
It is mounted once per router: through a small client component in
`app/layout.tsx`, and directly in `pages/_app.tsx`. A page is served by
only one router, so it is never mounted twice.
- **Crash**: `app/global-error.tsx` sends one `global_crash` per error
object. `pages/_error.tsx` sends one when the status code is unset or
5xx, so 404s and other 4xx responses are not counted.

No CSP change is needed because www does not set a
Content-Security-Policy. No turbo change is needed because the www build
env already passes `NEXT_PUBLIC_*`.

## Additional context

`app/global-error.tsx`, `pages/_error.tsx`, and
`lib/frontend-metrics.ts` use relative imports because the
`sentry-browser` vitest project, which renders the two error files, has
no path-alias resolution.

## Review instructions

1. Open the www preview deployment for this PR with the network tab
open.
2. Load an app-router page and a pages-router page. Confirm each sends
one POST to the metrics endpoint with `metric_name: "pageview"` and
`app: "www"`.
3. Navigate client-side and confirm one more POST per pathname change.
4. In `apps/www`, run `pnpm exec vitest run lib/frontend-metrics.test.ts
hooks/usePageviewMetric.test.tsx` and `pnpm exec vitest run --project
sentry-browser`.
2026-12-01 12:15:58 -05:00

69 lines
1.9 KiB
TypeScript

import '@code-hike/mdx/styles.css'
import 'config/code-hike.css'
import '../styles/globals.css'
import '../pages/launch-week/launchWeek.css'
import { inter, manrope, sourceCodePro } from '~/lib/fonts'
import { SIGNED_IN_HINT_SCRIPT } from 'common/gotrue'
import { genFaviconData } from 'common/MetaFavicons/app-router'
import type { Metadata, Viewport } from 'next'
import Providers from './providers'
import { PageviewMetric } from '@/components/PageviewMetric'
import { APP_NAME, DEFAULT_META_DESCRIPTION } from '@/lib/constants'
const site_title = `${APP_NAME} | The Postgres Development Platform`
const FAVICON_ROUTE = process.env.NODE_ENV === 'development' ? '/favicon/local' : '/favicon'
export const metadata: Metadata = {
title: site_title,
description: DEFAULT_META_DESCRIPTION,
openGraph: {
type: 'website',
url: 'https://supabase.com/',
siteName: 'Supabase',
images: [
{
url: 'https://supabase.com/images/og/supabase-og.png',
width: 800,
height: 600,
alt: 'Supabase Og Image',
},
],
},
twitter: {
creator: '@supabase',
site: '@supabase',
card: 'summary_large_image',
},
// www serves from the site root (`basePath: ''` in next.config.mjs)
icons: genFaviconData('', FAVICON_ROUTE),
manifest: `${FAVICON_ROUTE}/manifest.json`,
}
export const viewport: Viewport = {
initialScale: 1,
width: 'device-width',
}
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html
lang="en"
suppressHydrationWarning
className={`${manrope.variable} ${inter.variable} ${sourceCodePro.variable}`}
>
<head>
<script dangerouslySetInnerHTML={{ __html: SIGNED_IN_HINT_SCRIPT }} />
</head>
<body>
<Providers>
<PageviewMetric />
{children}
</Providers>
</body>
</html>
)
}