Skip to content

Commit

Permalink
refactor and add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Apr 29, 2024
1 parent acfe917 commit 83ddc33
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 5 deletions.
61 changes: 60 additions & 1 deletion packages/next/src/lib/metadata/resolvers/resolve-url.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { resolveUrl, resolveAbsoluteUrlWithPathname } from './resolve-url'
import {
resolveUrl,
resolveAbsoluteUrlWithPathname,
getSocialImageFallbackMetadataBase,
} from './resolve-url'

// required to be resolved as URL with resolveUrl()
describe('metadata: resolveUrl', () => {
Expand Down Expand Up @@ -106,3 +110,58 @@ describe('resolveAbsoluteUrlWithPathname', () => {
})
})
})

describe('getSocialImageFallbackMetadataBase', () => {
describe('fallbackMetadataBase', () => {
let originalEnv: NodeJS.ProcessEnv
function getSocialImageFallbackMetadataBaseHelper(): string {
return getSocialImageFallbackMetadataBase(null).fallbackMetadataBase.href
}

beforeEach(() => {
originalEnv = process.env
})

afterEach(() => {
delete process.env.VERCEL_URL
delete process.env.VERCEL_ENV

process.env = originalEnv
})

it('should return localhost url in local dev mode', () => {
// @ts-expect-error override process env
process.env.NODE_ENV = 'development'
expect(getSocialImageFallbackMetadataBaseHelper()).toBe(
'http://localhost:3000/'
)
})

it('should return metadataBase ', () => {
// @ts-expect-error override process env
process.env.NODE_ENV = 'production'
expect(getSocialImageFallbackMetadataBaseHelper()).toBe(
'http://localhost:3000/'
)
})

it('preview deployment', () => {
// @ts-expect-error override process env
process.env.NODE_ENV = 'production'
process.env.VERCEL_ENV = 'preview'
process.env.VERCEL_BRANCH_URL = 'branch-url'
expect(getSocialImageFallbackMetadataBaseHelper()).toBe(
'https://branch-url/'
)
})

it('production deployment', () => {
// @ts-expect-error override process env
process.env.NODE_ENV = 'production'
process.env.VERCEL_PROJECT_PRODUCTION_URL = 'production-url'
expect(getSocialImageFallbackMetadataBaseHelper()).toBe(
'https://production-url/'
)
})
})
})
6 changes: 2 additions & 4 deletions packages/next/src/lib/metadata/resolvers/resolve-url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@ function createLocalMetadataBase() {

function getPreviewDeploymentUrl(): URL | undefined {
const origin = process.env.VERCEL_BRANCH_URL || process.env.VERCEL_URL
if (!origin) return undefined
return new URL(`https://${origin}`)
return origin ? new URL(`https://${origin}`) : undefined
}

function getProductionDeploymentUrl(): URL | undefined {
const origin = process.env.VERCEL_PROJECT_PRODUCTION_URL
if (!origin) return undefined
return new URL(`https://${origin}`)
return origin ? new URL(`https://${origin}`) : undefined
}

// For deployment url for metadata routes, prefer to use the deployment url if possible
Expand Down

0 comments on commit 83ddc33

Please sign in to comment.