Unverified Commit b67a1182 authored by Jamiboy Mohammad's avatar Jamiboy Mohammad Committed by GitHub

fix: respect metadata streaming policy in Edge SSR (#99128)

## Summary

Edge App Router SSR always enabled streaming metadata, so requests
matching the default HTML-limited bot list could receive an initial
`<head>` without their delayed metadata. Use the same user-agent-based
metadata policy as Node SSR so matching requests receive their metadata
before `</head>`.

Add regression coverage asserting the default streaming and blocking
behavior in both Edge and Node runtimes. Twitterbot and TelegramBot
receive their metadata in the initial head; ChatGPT-User, GPTBot,
ClaudeBot, and browser requests continue to stream metadata. Also verify
that delayed metadata eventually arrives in both runtimes. The default
bot list is unchanged, and the tests do not override `htmlLimitedBots`.

Related to #98748; this addresses the Edge runtime bug without making
assumptions about how these bots consume streamed responses.

## Verification

- Production regression suite: 14 tests passed with Webpack and 14 with
Turbopack.
- Full native bootstrap failed at `cargo metadata`; JavaScript build
succeeded. Tests used the installed SWC 16.4.0-canary.38 binary with
Next.js 16.4.0-canary.40.

<!-- NEXT_JS_LLM -->
parent 2b776990
...@@ -27,6 +27,7 @@ import type { AppPageRenderResultMetadata } from '../../server/render-result' ...@@ -27,6 +27,7 @@ import type { AppPageRenderResultMetadata } from '../../server/render-result'
import type RenderResult from '../../server/render-result' import type RenderResult from '../../server/render-result'
import { getIsPossibleServerAction } from '../../server/lib/server-action-request-meta' import { getIsPossibleServerAction } from '../../server/lib/server-action-request-meta'
import { getBotType } from '../../shared/lib/router/utils/is-bot' import { getBotType } from '../../shared/lib/router/utils/is-bot'
import { shouldServeStreamingMetadata } from '../../server/lib/streaming-metadata'
import { interopDefault } from '../../lib/interop-default' import { interopDefault } from '../../lib/interop-default'
import { normalizeAppPath } from '../../shared/lib/router/utils/app-paths' import { normalizeAppPath } from '../../shared/lib/router/utils/app-paths'
import { checkIsOnDemandRevalidate } from '../../server/api-utils' import { checkIsOnDemandRevalidate } from '../../server/api-utils'
...@@ -100,7 +101,8 @@ async function requestHandler( ...@@ -100,7 +101,8 @@ async function requestHandler(
// INJECT_RAW:cacheHandlerRegistration // INJECT_RAW:cacheHandlerRegistration
const isPossibleServerAction = getIsPossibleServerAction(req) const isPossibleServerAction = getIsPossibleServerAction(req)
const botType = getBotType(req.headers.get('User-Agent') || '') const userAgent = req.headers.get('User-Agent') || ''
const botType = getBotType(userAgent)
const { isOnDemandRevalidate } = checkIsOnDemandRevalidate( const { isOnDemandRevalidate } = checkIsOnDemandRevalidate(
req.headers, req.headers,
previewProps previewProps
...@@ -131,7 +133,10 @@ async function requestHandler( ...@@ -131,7 +133,10 @@ async function requestHandler(
params, params,
page: srcPage, page: srcPage,
postponed: undefined, postponed: undefined,
serveStreamingMetadata: true, serveStreamingMetadata: shouldServeStreamingMetadata(
userAgent,
nextConfig.htmlLimitedBots
),
supportsDynamicResponse: true, supportsDynamicResponse: true,
buildManifest, buildManifest,
nextFontManifest, nextFontManifest,
......
import type { ReactNode } from 'react'
export default function Root({ children }: { children: ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
export { default, generateMetadata } from '../page'
export const runtime = 'nodejs'
export const dynamic = 'force-dynamic'
import type { Metadata } from 'next'
export const runtime = 'edge'
export const dynamic = 'force-dynamic'
export async function generateMetadata(): Promise<Metadata> {
// Simulate slow metadata so the shell can flush before it resolves.
await new Promise((resolve) => setTimeout(resolve, 1000))
return {
title: 'Edge metadata title',
description: 'Edge metadata description',
openGraph: {
title: 'Edge Open Graph title',
description: 'Edge Open Graph description',
},
}
}
export default function Page() {
return <p>Edge SSR metadata streaming</p>
}
import { nextTestSetup } from 'e2e-utils'
import cheerio from 'cheerio'
describe('metadata-streaming-edge-bots', () => {
const { next } = nextTestSetup({ files: __dirname })
async function readInitialHead(pathname: string, userAgent: string) {
const controller = new AbortController()
try {
const response = await next.fetch(pathname, {
headers: { 'user-agent': userAgent },
signal: controller.signal,
})
expect(response.status).toBe(200)
expect(response.body).not.toBeNull()
const decoder = new TextDecoder()
let html = ''
// Transport chunks can split anywhere. Stop at the first closing head
// rather than buffering the response, which could hide late metadata.
for await (const chunk of response.body! as unknown as AsyncIterable<Uint8Array>) {
html += decoder.decode(chunk, { stream: true })
const headEnd = html.indexOf('</head>')
if (headEnd !== -1) {
return cheerio.load(html.slice(0, headEnd + '</head>'.length))
}
}
throw new Error('Response ended without a closing </head>')
} finally {
controller.abort()
}
}
describe.each([
['Edge', '/'],
['Node', '/node'],
])('%s runtime', (_runtime, pathname) => {
it.each([
['ChatGPT-User', false],
['GPTBot', false],
['ClaudeBot', false],
['Twitterbot', true],
['TelegramBot (like TwitterBot)', true],
['Mozilla/5.0', false],
])(
'respects metadata streaming policy for %s',
async (userAgent, shouldBlock) => {
const $ = await readInitialHead(pathname, userAgent)
if (shouldBlock) {
expect($('head title').text()).toBe('Edge metadata title')
expect($('head meta[name="description"]').attr('content')).toBe(
'Edge metadata description'
)
expect($('head meta[property="og:title"]').attr('content')).toBe(
'Edge Open Graph title'
)
expect(
$('head meta[property="og:description"]').attr('content')
).toBe('Edge Open Graph description')
} else {
expect($('head title')).toHaveLength(0)
expect($('head meta[name="description"]')).toHaveLength(0)
expect($('head meta[property^="og:"]')).toHaveLength(0)
}
}
)
it('eventually includes the delayed metadata in the completed response', async () => {
const response = await next.fetch(pathname, {
headers: { 'user-agent': 'ChatGPT-User' },
})
expect(response.status).toBe(200)
const $ = cheerio.load(await response.text())
expect($('title').text()).toBe('Edge metadata title')
expect($('meta[name="description"]').attr('content')).toBe(
'Edge metadata description'
)
expect($('meta[property="og:title"]').attr('content')).toBe(
'Edge Open Graph title'
)
expect($('meta[property="og:description"]').attr('content')).toBe(
'Edge Open Graph description'
)
})
})
})
import type { NextConfig } from 'next'
const config: NextConfig = {
// Cache Components does not support the Edge runtime.
cacheComponents: false,
experimental: {
// CI enables this alongside Cache Components; it requires that feature.
cachedNavigations: false,
},
}
export default config
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