mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 15:39:13 +00:00
## 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`.
139 lines
4.9 KiB
TypeScript
139 lines
4.9 KiB
TypeScript
import '@code-hike/mdx/styles.css'
|
|
import 'config/code-hike.css'
|
|
import '../styles/globals.css'
|
|
import './launch-week/launchWeek.css'
|
|
|
|
import { inter, manrope, sourceCodePro } from '~/lib/fonts'
|
|
import {
|
|
AuthProvider,
|
|
FeatureFlagProvider,
|
|
IS_PLATFORM,
|
|
PageTelemetry,
|
|
TelemetryTagManager,
|
|
ThemeProvider,
|
|
useThemeSandbox,
|
|
} from 'common'
|
|
import { DevToolbar, DevToolbarProvider } from 'dev-tools'
|
|
import { DefaultSeo } from 'next-seo'
|
|
import type { AppProps } from 'next/app'
|
|
import Head from 'next/head'
|
|
import { useRouter } from 'next/router'
|
|
import { TooltipProvider } from 'ui'
|
|
import { CommandProvider } from 'ui-patterns/CommandMenu'
|
|
import { useConsentToast } from 'ui-patterns/consent'
|
|
|
|
import useDarkLaunchWeeks from '../hooks/useDarkLaunchWeeks'
|
|
import { usePageviewMetric } from '../hooks/usePageviewMetric'
|
|
import { useWwwCommandMenuTelemetry } from '../hooks/useWwwCommandMenuTelemetry'
|
|
import { MD_PAGES } from '@/app/api-v2/md/content.generated'
|
|
import { Toaster } from '@/app/toaster'
|
|
import { WwwCommandMenu } from '@/components/CommandMenu'
|
|
import {
|
|
DEFAULT_FAVICON_THEME_COLOR,
|
|
MetaFaviconsPagesRouter,
|
|
} from '@/components/MetaFaviconsPagesRouter'
|
|
import { WwwPagesFrameworkProvider } from '@/components/WwwPagesFrameworkProvider'
|
|
import { getPagesCanonicalHref, normalizePagesPath } from '@/lib/canonical'
|
|
import { API_URL, APP_NAME, DEFAULT_META_DESCRIPTION } from '@/lib/constants'
|
|
|
|
export default function App({ Component, pageProps }: AppProps) {
|
|
const router = useRouter()
|
|
const { hasAcceptedConsent } = useConsentToast()
|
|
const { onTelemetry } = useWwwCommandMenuTelemetry()
|
|
|
|
useThemeSandbox()
|
|
usePageviewMetric()
|
|
|
|
const site_title = `${APP_NAME} | The Postgres Development Platform.`
|
|
const { basePath } = useRouter()
|
|
|
|
const isDarkLaunchWeek = useDarkLaunchWeeks()
|
|
const forceDarkMode = isDarkLaunchWeek
|
|
|
|
const applicationName = 'Supabase'
|
|
const faviconRoute = process.env.NODE_ENV === 'development' ? '/favicon/local' : '/favicon'
|
|
const themeColor = DEFAULT_FAVICON_THEME_COLOR
|
|
|
|
// Advertise the .md version for AI agents on pages that have one.
|
|
const cleanPath = normalizePagesPath(router.asPath ?? '/') || '/'
|
|
const mdSlug = cleanPath === '/' ? 'index' : cleanPath.slice(1)
|
|
const mdAlternateHref = MD_PAGES.has(mdSlug)
|
|
? cleanPath === '/'
|
|
? '/index.md'
|
|
: `${cleanPath}.md`
|
|
: null
|
|
|
|
// Canonical for every pages-router route. Pages that set their own canonical
|
|
// via NextSeo override this default. Unresolved dynamic routes (e.g. `/[ref]`
|
|
// on a page without getStaticProps) are skipped rather than emitted literally.
|
|
const canonicalHref = getPagesCanonicalHref(router.asPath ?? '/')
|
|
|
|
return (
|
|
<>
|
|
<Head>
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
{mdAlternateHref && <link rel="alternate" type="text/markdown" href={mdAlternateHref} />}
|
|
</Head>
|
|
<MetaFaviconsPagesRouter
|
|
applicationName={applicationName}
|
|
route={faviconRoute}
|
|
themeColor={themeColor}
|
|
includeManifest
|
|
includeRssXmlFeed
|
|
/>
|
|
<DefaultSeo
|
|
title={site_title}
|
|
description={DEFAULT_META_DESCRIPTION}
|
|
canonical={canonicalHref}
|
|
openGraph={{
|
|
type: 'website',
|
|
url: 'https://supabase.com/',
|
|
site_name: 'Supabase',
|
|
images: [
|
|
{
|
|
url: `https://supabase.com${basePath}/images/og/supabase-og.png`,
|
|
width: 800,
|
|
height: 600,
|
|
alt: 'Supabase Og Image',
|
|
},
|
|
],
|
|
}}
|
|
twitter={{
|
|
handle: '@supabase',
|
|
site: '@supabase',
|
|
cardType: 'summary_large_image',
|
|
}}
|
|
/>
|
|
|
|
<div className={`${manrope.variable} ${inter.variable} ${sourceCodePro.variable}`}>
|
|
<WwwPagesFrameworkProvider>
|
|
<AuthProvider>
|
|
{/* [TODO] I think we need to deconflict with the providers in layout.tsx? */}
|
|
<FeatureFlagProvider API_URL={API_URL} enabled={{ cc: true, ph: false }}>
|
|
<DevToolbarProvider apiUrl={API_URL}>
|
|
<ThemeProvider forcedTheme={forceDarkMode ? 'dark' : undefined}>
|
|
<TooltipProvider delayDuration={0}>
|
|
<CommandProvider app="www" onTelemetry={onTelemetry}>
|
|
<Toaster />
|
|
<Component {...pageProps} />
|
|
<WwwCommandMenu />
|
|
<PageTelemetry
|
|
API_URL={API_URL}
|
|
platform="www"
|
|
hasAcceptedConsent={hasAcceptedConsent}
|
|
enabled={IS_PLATFORM}
|
|
/>
|
|
<DevToolbar />
|
|
</CommandProvider>
|
|
</TooltipProvider>
|
|
</ThemeProvider>
|
|
</DevToolbarProvider>
|
|
</FeatureFlagProvider>
|
|
</AuthProvider>
|
|
</WwwPagesFrameworkProvider>
|
|
<TelemetryTagManager />
|
|
</div>
|
|
</>
|
|
)
|
|
}
|