mirror of
https://github.com/supabase/supabase.git
synced 2026-12-06 08:29:01 +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`.
106 lines
2.7 KiB
TypeScript
106 lines
2.7 KiB
TypeScript
// @vitest-environment jsdom
|
|
import { StrictMode, act } from 'react'
|
|
import { createRoot } from 'react-dom/client'
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
|
|
import { usePageviewMetric } from './usePageviewMetric'
|
|
import { sendFrontendMetric } from '@/lib/frontend-metrics'
|
|
|
|
const mocks = vi.hoisted(() => ({
|
|
isEnabled: true,
|
|
pathname: '/pricing' as string | null,
|
|
}))
|
|
|
|
vi.mock('@/lib/constants', () => ({
|
|
get IS_PREVIEW() {
|
|
return mocks.isEnabled
|
|
},
|
|
}))
|
|
|
|
vi.mock('@/lib/frontend-metrics', () => ({ sendFrontendMetric: vi.fn() }))
|
|
|
|
vi.mock('next/navigation', () => ({
|
|
usePathname: () => mocks.pathname,
|
|
}))
|
|
|
|
;(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true
|
|
|
|
function TestComponent() {
|
|
usePageviewMetric()
|
|
return null
|
|
}
|
|
|
|
describe('usePageviewMetric', () => {
|
|
let container: HTMLDivElement
|
|
let root: ReturnType<typeof createRoot>
|
|
|
|
const render = async (strict = false) => {
|
|
await act(async () =>
|
|
root.render(
|
|
strict ? (
|
|
<StrictMode>
|
|
<TestComponent />
|
|
</StrictMode>
|
|
) : (
|
|
<TestComponent />
|
|
)
|
|
)
|
|
)
|
|
}
|
|
|
|
beforeEach(() => {
|
|
vi.mocked(sendFrontendMetric).mockClear()
|
|
mocks.isEnabled = true
|
|
mocks.pathname = '/pricing'
|
|
container = document.createElement('div')
|
|
document.body.appendChild(container)
|
|
root = createRoot(container)
|
|
})
|
|
|
|
afterEach(async () => {
|
|
await act(async () => root.unmount())
|
|
container.remove()
|
|
})
|
|
|
|
it('sends once on mount and once per pathname change', async () => {
|
|
await render()
|
|
expect(sendFrontendMetric).toHaveBeenCalledTimes(1)
|
|
expect(sendFrontendMetric).toHaveBeenCalledWith('pageview')
|
|
|
|
mocks.pathname = '/blog'
|
|
await render()
|
|
expect(sendFrontendMetric).toHaveBeenCalledTimes(2)
|
|
})
|
|
|
|
it('does not send again on a same-pathname re-render', async () => {
|
|
await render()
|
|
await render()
|
|
await render()
|
|
expect(sendFrontendMetric).toHaveBeenCalledTimes(1)
|
|
})
|
|
|
|
it('sends once on mount under StrictMode', async () => {
|
|
await render(true)
|
|
expect(sendFrontendMetric).toHaveBeenCalledTimes(1)
|
|
})
|
|
|
|
it('skips a null pathname (pages router before the router is ready) and sends once it resolves', async () => {
|
|
mocks.pathname = null
|
|
await render()
|
|
expect(sendFrontendMetric).not.toHaveBeenCalled()
|
|
|
|
mocks.pathname = '/pricing'
|
|
await render()
|
|
expect(sendFrontendMetric).toHaveBeenCalledTimes(1)
|
|
})
|
|
|
|
it('sends nothing when the gate is off', async () => {
|
|
mocks.isEnabled = false
|
|
await render()
|
|
|
|
mocks.pathname = '/blog'
|
|
await render()
|
|
expect(sendFrontendMetric).not.toHaveBeenCalled()
|
|
})
|
|
})
|