Files
supabase/apps/www/proxy.ts
T
Alaister YoungandAlaister Young 42bf16cdce fix(www): skip proxy for Studio assets and API requests (#52743)
Stops the www proxy from running on Studio's non-document traffic. Each
chunk, server function and API request under `/dashboard` currently
invokes it, and a cold Studio load preloads a few hundred chunks through
this path.

**Changed:**
- The proxy matcher now skips `/dashboard/_vercel/`,
`/dashboard/_serverFn/` and `/dashboard/api/`
- Studio documents still match, so direct `/dashboard` landings keep
getting the `_sb_first_referrer` cookie and the paid-traffic refresh

**Added:**
- Matcher tests using Next's `unstable_doesMiddlewareMatch`. The three
skip cases fail against the old matcher

A quick single-region TTFB probe on production showed a www static file
that goes through the proxy about 15ms slower than one that skips it.
Rerun that comparison on this preview if you want a before/after.

## To test

On the www preview, replacing `<chunk>` with a real chunk path from the
page source:

- `curl -sI -H 'Referer: https://www.google.com/'
<preview>/dashboard/_vercel/immutable/<chunk>`: 200 with no `set-cookie:
_sb_first_referrer`
- `curl -sI -H 'Referer: https://www.google.com/'
<preview>/dashboard/sign-in`: still has `set-cookie: _sb_first_referrer`
- Load `/dashboard` in a browser and check Studio renders with no failed
chunk or API requests
- `curl -sI -H 'Accept: text/markdown' <preview>/pricing`: markdown
negotiation on www pages is unchanged

Co-authored-by: Alaister Young <10985857+alaister@users.noreply.github.com>
2026-11-04 11:31:25 +08:00

55 lines
2.0 KiB
TypeScript

import { stampFirstReferrerCookie } from 'common/first-referrer-cookie'
import { negotiateMarkdown } from 'common/markdown-negotiation'
import { NextResponse, type NextRequest } from 'next/server'
import { CHANGELOG_PAGES, MD_PAGES } from './app/api-v2/md/content.generated'
export function proxy(request: NextRequest) {
const { pathname } = request.nextUrl
const isMarkdownSuffix = pathname.endsWith('.md')
const basePathname = isMarkdownSuffix ? pathname.slice(0, -3) : pathname
// Strip trailing slash so /auth/ and /auth resolve to the same allowlist
// entry — NextURL preserves trailing-slash style on rewrite targets.
const slug = (basePathname === '/' ? 'index' : basePathname.slice(1)).replace(/\/$/, '')
const isMdEligible = MD_PAGES.has(slug)
const isChangelogEntry = CHANGELOG_PAGES.has(slug)
const decision = negotiateMarkdown(
{ acceptHeader: request.headers.get('accept') ?? '' },
{ hasMarkdownVariant: isMdEligible || isChangelogEntry, isMarkdownSuffix }
)
if (decision === 'not-acceptable') {
return new NextResponse('Not Acceptable', {
status: 406,
headers: { 'Cache-Control': 'no-store', Vary: 'Accept' },
})
}
if (decision === 'markdown') {
if (isMdEligible) {
return NextResponse.rewrite(new URL(`/api-v2/md/${slug}`, request.nextUrl))
}
// Changelog entries are static .md files in public/, not API routes.
if (isChangelogEntry) {
return NextResponse.rewrite(new URL(`/${slug}.md`, request.nextUrl))
}
}
const response = NextResponse.next()
stampFirstReferrerCookie(request, response)
return response
}
export const config = {
matcher: [
// MUST exclude _next/data to prevent full page reloads in multi-zone apps.
// Studio's assets, server functions and API routes are excluded because they
// don't need this proxy. Studio documents must still match: they are where
// direct /dashboard landings get the first-referrer cookie.
'/((?!api|_next/static|_next/image|_next/data|favicon.ico|__nextjs|dashboard/(?:_vercel|_serverFn|api)/).*)',
],
}