The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
fix(www): reject Next.js 15 router state headers in proxy (#52071)
Ivan Vasilov3383f7edbd5e
diff --git a/apps/www/proxy.test.ts b/apps/www/proxy.test.tsindex 06b0c778a7f..14a17bc2b7c 100644--- a/apps/www/proxy.test.ts+++ b/apps/www/proxy.test.ts@@ -21,12 +21,20 @@ function makeRequest(hasCookie,accept,userAgent,- }: { referer?: string; hasCookie?: boolean; accept?: string; userAgent?: string } = {}+ routerStateTree,+ }: {+ referer?: string+ hasCookie?: boolean+ accept?: string+ userAgent?: string+ routerStateTree?: string+ } = {}): NextRequest {const headers: Record<string, string> = {}if (referer) headers.referer = refererif (accept) headers.accept = acceptif (userAgent) headers['user-agent'] = userAgent+ if (routerStateTree) headers['next-router-state-tree'] = routerStateTreeconst req = new NextRequest(new URL(url, 'https://supabase.com'), { headers })if (hasCookie) {req.cookies.set(FIRST_REFERRER_COOKIE_NAME, 'existing')@@ -35,6 +43,49 @@ function makeRequest(}describe('www proxy', () => {+ describe('Next.js 15 router state tree', () => {+ // Shape sent by a Next.js 15 client: the root layout segment has `isRootLayout: true`.+ const v15Tree = encodeURIComponent(+ JSON.stringify(['', { children: ['blog', { children: ['__PAGE__', {}] }] }, null, null, true])+ )+ // Shape sent by a Next.js 16 client: the fifth element is a numeric flags bitmask.+ const v16Tree = encodeURIComponent(+ JSON.stringify(['', { children: ['blog', { children: ['__PAGE__', {}] }] }, null, null, 1])+ )++ it('rejects v15-shaped state with a 400 instead of letting the renderer throw', () => {+ const res = proxy(makeRequest('/blog?_rsc=abc', { routerStateTree: v15Tree }))++ expect(res.status).toBe(400)+ expect(res.headers.get('Cache-Control')).toBe('no-store')+ })++ it.each(['/docs/guides/auth', '/docs', '/dashboard/project/x', '/kb/guides/x'])(+ 'does not touch v15-shaped state on other zones (%s)',+ (path) => {+ const res = proxy(makeRequest(`${path}?_rsc=abc`, { routerStateTree: v15Tree }))++ expect(res.status).toBe(200)+ }+ )++ it('passes v16-shaped state through', () => {+ const res = proxy(makeRequest('/blog?_rsc=abc', { routerStateTree: v16Tree }))++ expect(res.status).toBe(200)+ })++ it('passes requests without the header through', () => {+ expect(proxy(makeRequest('/blog')).status).toBe(200)+ })++ it('leaves malformed state for Next.js to handle', () => {+ const res = proxy(makeRequest('/blog?_rsc=abc', { routerStateTree: '%7Bnot-json' }))++ expect(res.status).toBe(200)+ })+ })+describe('cookie stamping on www paths', () => {it('stamps cookie for external referrer on www path', () => {const req = makeRequest('/pricing', { referer: 'https://google.com' })diff --git a/apps/www/proxy.ts b/apps/www/proxy.tsindex 879882a9860..15de02ac519 100644--- a/apps/www/proxy.ts+++ b/apps/www/proxy.ts@@ -4,9 +4,56 @@ import { NextResponse, type NextRequest } from 'next/server'import { CHANGELOG_PAGES, MD_PAGES } from './app/api-v2/md/content.generated'+const OTHER_ZONE_PREFIXES = [+ '/docs',+ '/dashboard',+ '/library',+ '/design-system',+ '/kb',+ '/evals',+ '/migrate/instantdb',+ '/new-docs',+]++/**+ * www is a Next.js 16 zone, but the docs zone (served under /docs through a rewrite) and clients+ * holding a pre-upgrade bundle still run Next.js 15. Their client-side navigations into www pages+ * carry a `Next-Router-State-Tree` header in the v15 shape, where the fifth tuple element+ * (`isRootLayout`) is a boolean. v16 expects a number there, so it throws "The router state header+ * was sent but could not be parsed" and returns a 500.+ */+function hasLegacyRouterStateTree(request: NextRequest, pathname: string) {+ // Paths that lib/rewrites.js forwards to other zones. Those zones may legitimately run Next.js 15+ // (docs), so their own client navigations must reach them untouched.+ if (+ OTHER_ZONE_PREFIXES.some((prefix) => pathname === prefix || pathname.startsWith(`${prefix}/`))+ ) {+ return false+ }++ const header = request.headers.get('next-router-state-tree')+ if (!header) return false++ try {+ const state = JSON.parse(decodeURIComponent(header))+ return Array.isArray(state) && typeof state[4] === 'boolean'+ } catch {+ return false+ }+}+export function proxy(request: NextRequest) {const { pathname } = request.nextUrl+ // The Next.js 15 router treats any non-RSC response as a cue to fall back to a full page load,+ // which is what happens today after the 500. Answer with a cheap 400 so it doesn't hit the renderer.+ if (hasLegacyRouterStateTree(request, pathname)) {+ return new NextResponse(null, {+ status: 400,+ headers: { 'Cache-Control': 'no-store' },+ })+ }+const isMarkdownSuffix = pathname.endsWith('.md')const basePathname = isMarkdownSuffix ? pathname.slice(0, -3) : pathname