Skip to content

Commit

Permalink
warn once for metadataBase missing
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Apr 10, 2023
1 parent 772f3e7 commit 8e1b314
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 31 deletions.
8 changes: 8 additions & 0 deletions packages/next/src/build/output/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,11 @@ export function event(...message: any[]) {
export function trace(...message: any[]) {
console.log(prefixes.trace, ...message)
}

const uniqueWarnings = new Set()
export function warnOnce(...message: any[]) {
const key = message.join(' ')
if (uniqueWarnings.has(key)) return
uniqueWarnings.add(key)
warn(...message)
}
4 changes: 2 additions & 2 deletions packages/next/src/lib/metadata/resolvers/resolve-url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ function resolveUrl(

if (!metadataBase) {
metadataBase = new URL(`http://localhost:${process.env.PORT || 3000}`)
// Development mode warning, add new line prefix for worker output
// next build mode warning, add new line prefix for worker output
console.log()
Log.warn(
Log.warnOnce(
`metadata.metadataBase is not set for resolving url "${url}", fallbacks to "${metadataBase.origin}". See https://beta.nextjs.org/docs/api-reference/metadata#metadatabase`
)
}
Expand Down
48 changes: 19 additions & 29 deletions test/e2e/app-dir/metadata-missing-metadata-base/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,21 @@
import { createNext, FileRef } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base'
import { fetchViaHTTP } from 'next-test-utils'
import { createNextDescribe } from 'e2e-utils'

describe('app dir - metadata missing metadataBase', () => {
let next: NextInstance

if ((global as any).isNextDeploy) {
return it('should skip for deploy', () => {})
}

beforeAll(async () => {
next = await createNext({
skipStart: true,
files: new FileRef(__dirname),
createNextDescribe(
'app dir - metadata missing metadataBase',
{
files: __dirname,
skipDeployment: true,
},
({ next }) => {
it('should fallback to localhost if metadataBase is missing for absolute urls resolving', async () => {
await next.fetch('/blog')
expect(next.cliOutput).toInclude(
'metadata.metadataBase is not set for resolving url "/blog/opengraph-image?'
)
expect(next.cliOutput).toInclude(', fallbacks to "http://localhost:')
expect(next.cliOutput).toInclude(
'. See https://beta.nextjs.org/docs/api-reference/metadata#metadatabase'
)
})
})
afterAll(() => next.destroy())

it('should fallback to localhost if metadataBase is missing for absolute urls resolving', async () => {
await next.start()
await fetchViaHTTP(next.url, '/blog')
expect(next.cliOutput).toInclude(
'metadata.metadataBase is not set for resolving url "/blog/opengraph-image?'
)
expect(next.cliOutput).toInclude(', fallbacks to "http://localhost:')
expect(next.cliOutput).toInclude(
'. See https://beta.nextjs.org/docs/api-reference/metadata#metadatabase'
)
})
})
}
)

0 comments on commit 8e1b314

Please sign in to comment.