Unverified Commit d1f96581 authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

[metadata] only serve block streaming metadata for html bots (#80272)

parent a8ed882c
...@@ -12,10 +12,11 @@ export function shouldServeStreamingMetadata( ...@@ -12,10 +12,11 @@ export function shouldServeStreamingMetadata(
htmlLimitedBots || HTML_LIMITED_BOT_UA_RE_STRING, htmlLimitedBots || HTML_LIMITED_BOT_UA_RE_STRING,
'i' 'i'
) )
return ( // Only block metadata for HTML-limited bots
// When it's static generation, userAgents are not available - do not serve streaming metadata if (userAgent && blockingMetadataUARegex.test(userAgent)) {
!!userAgent && !blockingMetadataUARegex.test(userAgent) return false
) }
return true
} }
// When the request UA is a html-limited bot, we should do a dynamic render. // When the request UA is a html-limited bot, we should do a dynamic render.
......
import Head from 'next/head'
import Image from 'next/image' import Image from 'next/image'
const Page = () => { const Page = () => {
return ( return (
<div> <div>
<Head>
<title>Title</title>
<meta name="description" content="Description..." />
<link rel="icon" type="image/jpeg" href="/test.jpg" />
</Head>
<main> <main>
<Image src="/test.jpg" width="400" height="400" alt="basic image" /> <Image src="/test.jpg" width="400" height="400" alt="basic image" />
</main> </main>
...@@ -18,3 +11,11 @@ const Page = () => { ...@@ -18,3 +11,11 @@ const Page = () => {
} }
export default Page export default Page
export const metadata = {
title: 'Title',
description: 'Description...',
icons: {
icon: '/test.jpg',
},
}
...@@ -1568,25 +1568,26 @@ function runTests(mode: 'dev' | 'server') { ...@@ -1568,25 +1568,26 @@ function runTests(mode: 'dev' | 'server') {
}) })
it('should be valid HTML', async () => { it('should be valid HTML', async () => {
let browser // `html-validator` underlying API (W3C validator or Nu HTML Checker) ignores or overrides custom headers.
try { // So we need to use a custom User-Agent to avoid streaming metadata.
browser = await webdriver(appPort, '/valid-html-w3c') // Otherwise the HTML will failed with validation which requires title to be rendered under the `head` tag.
await waitFor(1000) const html = await renderViaHTTP(appPort, '/valid-html-w3c', null, {
expect(await browser.hasElementByCssSelector('img')).toBeTruthy() headers: {
const url = await browser.url() 'User-Agent': 'Discordbot',
const result = (await validateHTML({ },
url, })
format: 'json',
isLocal: true, const result = (await validateHTML({
validator: 'whatwg', data: html,
})) as any format: 'json',
expect(result.isValid).toBe(true) isLocal: true,
expect(result.errors).toEqual([]) validator: 'whatwg',
} finally { })) as any
if (browser) { expect(result.isValid).toBe(true)
await browser.close() expect(result.errors).toEqual([])
}
} const $ = cheerio.load(html)
expect($('img').length).toBeGreaterThan(0)
}) })
it('should call callback ref cleanups when unmounting', async () => { it('should call callback ref cleanups when unmounting', async () => {
......
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