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`.
111 lines
3.0 KiB
TypeScript
111 lines
3.0 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
|
|
import { reportGlobalCrashMetric, sendFrontendMetric } from './frontend-metrics'
|
|
|
|
const mocks = vi.hoisted(() => ({ isEnabled: true }))
|
|
|
|
vi.mock('./constants', () => ({
|
|
get IS_PREVIEW() {
|
|
return mocks.isEnabled
|
|
},
|
|
}))
|
|
|
|
describe('sendFrontendMetric', () => {
|
|
afterEach(() => {
|
|
vi.unstubAllGlobals()
|
|
})
|
|
|
|
it.each(['pageview', 'global_crash'] as const)(
|
|
'posts a single %s event as a CORS-simple request',
|
|
(metricName) => {
|
|
const fetchMock = vi.fn().mockResolvedValue(new Response(null, { status: 204 }))
|
|
vi.stubGlobal('fetch', fetchMock)
|
|
|
|
sendFrontendMetric(metricName)
|
|
|
|
expect(fetchMock).toHaveBeenCalledTimes(1)
|
|
const [url, init] = fetchMock.mock.calls[0]
|
|
expect(url).toBe('https://misc-functions.vercel.app/api/metrics')
|
|
expect(init).toMatchObject({
|
|
method: 'POST',
|
|
keepalive: true,
|
|
credentials: 'omit',
|
|
headers: { 'Content-Type': 'text/plain' },
|
|
})
|
|
expect(JSON.parse(init.body)).toEqual({
|
|
environment: 'staging',
|
|
events: [{ metric_name: metricName, value: 1, metadata: { app: 'www' } }],
|
|
})
|
|
}
|
|
)
|
|
|
|
it('does not throw or leave an unhandled rejection when the request fails', async () => {
|
|
const unhandled = vi.fn()
|
|
process.on('unhandledRejection', unhandled)
|
|
vi.stubGlobal('fetch', vi.fn().mockRejectedValue(new TypeError('Failed to fetch')))
|
|
|
|
expect(() => sendFrontendMetric('global_crash')).not.toThrow()
|
|
await new Promise((resolve) => setImmediate(resolve))
|
|
|
|
process.off('unhandledRejection', unhandled)
|
|
expect(unhandled).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('does not throw when fetch throws synchronously', () => {
|
|
vi.stubGlobal(
|
|
'fetch',
|
|
vi.fn(() => {
|
|
throw new Error('boom')
|
|
})
|
|
)
|
|
|
|
expect(() => sendFrontendMetric('global_crash')).not.toThrow()
|
|
})
|
|
})
|
|
|
|
describe('reportGlobalCrashMetric', () => {
|
|
const fetchMock = vi.fn()
|
|
|
|
beforeEach(() => {
|
|
mocks.isEnabled = true
|
|
fetchMock.mockReset().mockResolvedValue(new Response(null, { status: 204 }))
|
|
vi.stubGlobal('fetch', fetchMock)
|
|
})
|
|
|
|
afterEach(() => {
|
|
vi.unstubAllGlobals()
|
|
})
|
|
|
|
it('sends one global_crash metric', () => {
|
|
reportGlobalCrashMetric(new Error('boom'))
|
|
|
|
expect(fetchMock).toHaveBeenCalledTimes(1)
|
|
expect(JSON.parse(fetchMock.mock.calls[0][1].body).events[0].metric_name).toBe('global_crash')
|
|
})
|
|
|
|
it('sends nothing when the gate is off', () => {
|
|
mocks.isEnabled = false
|
|
|
|
reportGlobalCrashMetric(new Error('boom'))
|
|
|
|
expect(fetchMock).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('counts the same error object once', () => {
|
|
const error = new Error('boom')
|
|
|
|
reportGlobalCrashMetric(error)
|
|
reportGlobalCrashMetric(error)
|
|
|
|
expect(fetchMock).toHaveBeenCalledTimes(1)
|
|
})
|
|
|
|
it('counts non-object errors each time', () => {
|
|
reportGlobalCrashMetric('boom')
|
|
reportGlobalCrashMetric('boom')
|
|
reportGlobalCrashMetric(undefined)
|
|
|
|
expect(fetchMock).toHaveBeenCalledTimes(3)
|
|
})
|
|
})
|