Skip to content

Commit

Permalink
Fallback to deployment vercel url if metadataBase is not set on prod (#…
Browse files Browse the repository at this point in the history
…48570)

x-ref:
#48556 (comment)

For production deployment, we still fallback to deployment URL if
`metadataBase` is not set.
  • Loading branch information
huozhi authored Apr 19, 2023
1 parent 6f30c91 commit 0a04ab6
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
13 changes: 8 additions & 5 deletions packages/next/src/lib/metadata/resolvers/resolve-url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@ export function getFallbackMetadataBaseIfPresent(
metadataBase: URL | null
): URL | null {
const defaultMetadataBase = createLocalMetadataBase()
const deploymentUrl =
process.env.VERCEL_URL && new URL(`https://${process.env.VERCEL_URL}`)
if (process.env.NODE_ENV === 'development') {
return defaultMetadataBase
}
return process.env.NODE_ENV === 'production' &&
process.env.VERCEL_URL &&
deploymentUrl &&
process.env.VERCEL_ENV === 'preview'
? new URL(`https://${process.env.VERCEL_URL}`)
: process.env.NODE_ENV === 'development'
? defaultMetadataBase
: metadataBase || defaultMetadataBase
? deploymentUrl
: metadataBase || deploymentUrl || defaultMetadataBase
}

function resolveUrl(url: null | undefined, metadataBase: URL | null): null
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export default function page() {
return (
<div>
<h1>Unset</h1>
</div>
)
}

export const metadata = {
metadataBase: null,
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions test/e2e/app-dir/metadata-dynamic-routes/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,21 @@ createNextDescribe(
)
})

it('should use localhost for local prod and fallback to deployment url when metadataBase is falsy', async () => {
const $ = await next.render$('/metadata-base/unset')
const twitterImage = $('meta[name="twitter:image"]').attr('content')

if (isNextDeploy) {
expect(twitterImage).toMatch(
/https:\/\/\w+.vercel.app\/metadata-base\/unset\/twitter-image\.png/
)
} else {
expect(twitterImage).toMatch(
/http:\/\/localhost:\d+\/metadata-base\/unset\/twitter-image\.png/
)
}
})

if (isNextStart) {
it('should support edge runtime of image routes', async () => {
const middlewareManifest = JSON.parse(
Expand Down

0 comments on commit 0a04ab6

Please sign in to comment.