Hide internal variant values from `headers()`

parent 9de70ca0
...@@ -10,6 +10,7 @@ import { ...@@ -10,6 +10,7 @@ import {
NEXT_HTML_REQUEST_ID_HEADER, NEXT_HTML_REQUEST_ID_HEADER,
NEXT_REQUEST_ID_HEADER, NEXT_REQUEST_ID_HEADER,
} from '../../client/components/app-router-headers' } from '../../client/components/app-router-headers'
import { NEXT_VARIANTS_HEADER } from '../../lib/constants'
import { import {
HeadersAdapter, HeadersAdapter,
type ReadonlyHeaders, type ReadonlyHeaders,
...@@ -44,6 +45,7 @@ import type { OpaqueFallbackRouteParams } from '../request/fallback-params' ...@@ -44,6 +45,7 @@ import type { OpaqueFallbackRouteParams } from '../request/fallback-params'
const HIDDEN_REQUEST_HEADERS: ReadonlySet<string> = new Set( const HIDDEN_REQUEST_HEADERS: ReadonlySet<string> = new Set(
[ [
...FLIGHT_HEADERS, ...FLIGHT_HEADERS,
NEXT_VARIANTS_HEADER,
// The client sends these dev-only request IDs so the server can route debug // The client sends these dev-only request IDs so the server can route debug
// information back to the originating request. Like the flight headers, // information back to the originating request. Like the flight headers,
// they are internal plumbing. // they are internal plumbing.
......
import { connection } from 'next/server' import { connection } from 'next/server'
import { headers } from 'next/headers'
import { locale, theme } from '../variants' import { locale, theme } from '../variants'
...@@ -9,11 +10,15 @@ import { locale, theme } from '../variants' ...@@ -9,11 +10,15 @@ import { locale, theme } from '../variants'
// above a read of one. Every caller of this component provides that boundary. // above a read of one. Every caller of this component provides that boundary.
export async function VariantValues() { export async function VariantValues() {
await connection() await connection()
const requestHeaders = await headers()
return ( return (
<> <>
<p id="theme">{await theme()}</p> <p id="theme">{await theme()}</p>
<p id="locale">{await locale()}</p> <p id="locale">{await locale()}</p>
<p id="internal-variants-header">
{requestHeaders.has('x-next-internal-variants') ? 'present' : 'absent'}
</p>
</> </>
) )
} }
...@@ -67,6 +67,15 @@ import { startExternalServer } from './external-server.mjs' ...@@ -67,6 +67,15 @@ import { startExternalServer } from './external-server.mjs'
expect(response.headers.get('x-next-internal-variants')).toBeNull() expect(response.headers.get('x-next-internal-variants')).toBeNull()
}) })
it('should not expose the resolved values through `headers()`', async () => {
const $ = await next.render$(url('/'), undefined, {
headers: { cookie: 'theme=dark' },
})
expect($('#theme').text()).toBe('dark')
expect($('#internal-variants-header').text()).toBe('absent')
})
it('should resolve a variant on the route the proxy rewrote to', async () => { it('should resolve a variant on the route the proxy rewrote to', async () => {
const $ = await next.render$(url('/rewrite-source'), undefined, { const $ = await next.render$(url('/rewrite-source'), undefined, {
headers: { cookie: 'theme=dark' }, headers: { cookie: 'theme=dark' },
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment