Unverified Commit 31809e42 authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

Polish unsupported metadata warning with doc link (#58750)

* Add docs link to the warning, add codemod link to viewport docs section
* Only log the warning once after the metadata resolving process, this will avoid multiple duplicated logs showing for one page.

Closes NEXT-1723
parent 0f642dc3
...@@ -15,6 +15,7 @@ You can customize the initial viewport of the page with the static `viewport` ob ...@@ -15,6 +15,7 @@ You can customize the initial viewport of the page with the static `viewport` ob
> >
> - The `viewport` object and `generateViewport` function exports are **only supported in Server Components**. > - The `viewport` object and `generateViewport` function exports are **only supported in Server Components**.
> - You cannot export both the `viewport` object and `generateViewport` function from the same route segment. > - You cannot export both the `viewport` object and `generateViewport` function from the same route segment.
> - If you're coming from migrating `metadata` exports, you can use [metadata-to-viewport-export codemod](/docs/app/building-your-application/upgrading/codemods#metadata-to-viewport-export) to update your changes.
## The `viewport` object ## The `viewport` object
......
...@@ -63,6 +63,10 @@ type TitleTemplates = { ...@@ -63,6 +63,10 @@ type TitleTemplates = {
openGraph: string | null openGraph: string | null
} }
type BuildState = {
warnings: Set<string>
}
function hasIconsProperty( function hasIconsProperty(
icons: Metadata['icons'], icons: Metadata['icons'],
prop: 'icon' | 'apple' prop: 'icon' | 'apple'
...@@ -135,12 +139,14 @@ function mergeMetadata({ ...@@ -135,12 +139,14 @@ function mergeMetadata({
staticFilesMetadata, staticFilesMetadata,
titleTemplates, titleTemplates,
metadataContext, metadataContext,
buildState,
}: { }: {
source: Metadata | null source: Metadata | null
target: ResolvedMetadata target: ResolvedMetadata
staticFilesMetadata: StaticMetadata staticFilesMetadata: StaticMetadata
titleTemplates: TitleTemplates titleTemplates: TitleTemplates
metadataContext: MetadataContext metadataContext: MetadataContext
buildState: BuildState
}): void { }): void {
// If there's override metadata, prefer it otherwise fallback to the default metadata. // If there's override metadata, prefer it otherwise fallback to the default metadata.
const metadataBase = const metadataBase =
...@@ -244,8 +250,8 @@ function mergeMetadata({ ...@@ -244,8 +250,8 @@ function mergeMetadata({
key === 'themeColor' || key === 'themeColor' ||
key === 'colorScheme' key === 'colorScheme'
) { ) {
Log.warn( buildState.warnings.add(
`Unsupported metadata ${key} is configured in metadata export. Please move it to viewport export instead.` `Unsupported metadata ${key} is configured in metadata export in ${metadataContext.pathname}. Please move it to viewport export instead.\nRead more: https://nextjs.org/docs/app/api-reference/functions/generate-viewport`
) )
} }
break break
...@@ -685,6 +691,9 @@ export async function accumulateMetadata( ...@@ -685,6 +691,9 @@ export async function accumulateMetadata(
resolvers: [], resolvers: [],
resolvingIndex: 0, resolvingIndex: 0,
} }
const buildState = {
warnings: new Set<string>(),
}
for (let i = 0; i < metadataItems.length; i++) { for (let i = 0; i < metadataItems.length; i++) {
const staticFilesMetadata = metadataItems[i][1] const staticFilesMetadata = metadataItems[i][1]
...@@ -703,6 +712,7 @@ export async function accumulateMetadata( ...@@ -703,6 +712,7 @@ export async function accumulateMetadata(
metadataContext, metadataContext,
staticFilesMetadata, staticFilesMetadata,
titleTemplates, titleTemplates,
buildState,
}) })
// If the layout is the same layer with page, skip the leaf layout and leaf page // If the layout is the same layer with page, skip the leaf layout and leaf page
...@@ -716,6 +726,13 @@ export async function accumulateMetadata( ...@@ -716,6 +726,13 @@ export async function accumulateMetadata(
} }
} }
// Only log warnings if there are any, and only once after the metadata resolving process is finished
if (buildState.warnings.size > 0) {
for (const warning of buildState.warnings) {
Log.warn(warning)
}
}
return postProcessMetadata(resolvedMetadata, titleTemplates) return postProcessMetadata(resolvedMetadata, titleTemplates)
} }
......
export default function Page() {
return 'unsupported metadata'
}
export const metadata = {
themeColor: 'light',
}
import { createNext, FileRef } from 'e2e-utils' import { createNextDescribe } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base' import { fetchViaHTTP } from 'next-test-utils'
import { fetchViaHTTP, findPort } from 'next-test-utils'
describe('app dir - metadata missing metadataBase', () => { createNextDescribe(
let next: NextInstance 'app dir - metadata missing metadataBase',
let port: number {
files: __dirname,
skipDeployment: true,
},
({ next, isNextStart }) => {
// If it's start mode, we get the whole logs since they're from build process.
// If it's dev mode, we get the logs after request
function getCliOutput(logStartPosition: number) {
return isNextStart
? next.cliOutput
: next.cliOutput.slice(logStartPosition)
}
if ((global as any).isNextDeploy) { it('should fallback to localhost if metadataBase is missing for absolute urls resolving', async () => {
return it('should skip for deploy', () => {}) const logStartPosition = next.cliOutput.length
} await fetchViaHTTP(next.url, '/')
//
beforeAll(async () => { const output = getCliOutput(logStartPosition)
port = await findPort() expect(output).toInclude(
next = await createNext({ 'metadata.metadataBase is not set for resolving social open graph or twitter images,'
skipStart: true, )
files: new FileRef(__dirname), expect(output).toMatch(/using "http:\/\/localhost:\d+/)
forcedPort: port + '', expect(output).toInclude(
'. See https://nextjs.org/docs/app/api-reference/functions/generate-metadata#metadatabase'
)
}) })
})
afterAll(() => next.destroy())
it('should fallback to localhost if metadataBase is missing for absolute urls resolving', async () => { it('should warn for unsupported metadata properties', async () => {
await next.start() const logStartPosition = next.cliOutput.length
await fetchViaHTTP(next.url, '/') await fetchViaHTTP(next.url, '/unsupported-metadata')
expect(next.cliOutput).toInclude( const output = getCliOutput(logStartPosition)
'metadata.metadataBase is not set for resolving social open graph or twitter images, using' expect(output).toInclude(
) 'Unsupported metadata themeColor is configured in metadata export in /unsupported-metadata. Please move it to viewport'
expect(next.cliOutput).toInclude(`"http://localhost:${port}`) )
expect(next.cliOutput).toInclude( expect(output).toInclude(
'. See https://nextjs.org/docs/app/api-reference/functions/generate-metadata#metadatabase' 'Read more: https://nextjs.org/docs/app/api-reference/functions/generate-viewport'
) )
}) })
}) }
)
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