Unverified Commit 4d589db7 authored by Jiwon Choi's avatar Jiwon Choi Committed by GitHub

fix: allow empty string value for opengraph (#68262)

### Why?

When passing an empty string value `''` to `metadata.openGraph.authors`,
it gives an error:

```
Error: n.map is not a function
```

It is because we set the value to the `ResolvedOpenGraph` if it is
truthy.

```ts
if (value) {
 // ...
}
```

### How?

Assign the resolved value from `resolveArray` and only if not nullish.


https://github.com/vercel/next.js/blob/571597d8ee464113e24494591ed693f82b710d16/packages/next/src/lib/metadata/generate/utils.ts#L1-L6

Fixes https://github.com/vercel/next.js/issues/68259
Fixes NDX-125

---------
Co-authored-by: 's avatarJiachi Liu <inbox@huozhi.im>
parent bf0797da
import { resolveImages } from './resolve-opengraph' import { resolveImages, resolveOpenGraph } from './resolve-opengraph'
describe('resolveImages', () => { describe('resolveImages', () => {
const image1 = 'https://www.example.com/image1.jpg' const image1 = 'https://www.example.com/image1.jpg'
...@@ -36,3 +36,42 @@ describe('resolveImages', () => { ...@@ -36,3 +36,42 @@ describe('resolveImages', () => {
]) ])
}) })
}) })
describe('resolveOpenGraph', () => {
it('should return null if the value is an empty string', () => {
expect(
resolveOpenGraph(
// pass authors as empty string
{ type: 'article', authors: '' },
null,
{ isStandaloneMode: false, trailingSlash: false, pathname: '' },
''
)
).toEqual({
// if an empty string '' is passed, it'll throw: "n.map is not a function"
// x-ref: https://github.com/vercel/next.js/pull/68262
authors: null,
images: undefined,
title: { absolute: '', template: null },
type: 'article',
url: null,
})
})
it('should return null if the value is null', () => {
expect(
resolveOpenGraph(
{ type: 'article', authors: null },
null,
{ isStandaloneMode: false, trailingSlash: false, pathname: '' },
''
)
).toEqual({
authors: null,
images: undefined,
title: { absolute: '', template: null },
type: 'article',
url: null,
})
})
})
...@@ -9,7 +9,7 @@ import type { ...@@ -9,7 +9,7 @@ import type {
MetadataContext, MetadataContext,
} from '../types/resolvers' } from '../types/resolvers'
import type { ResolvedTwitterMetadata, Twitter } from '../types/twitter-types' import type { ResolvedTwitterMetadata, Twitter } from '../types/twitter-types'
import { resolveAsArrayOrUndefined } from '../generate/utils' import { resolveArray, resolveAsArrayOrUndefined } from '../generate/utils'
import { import {
getSocialImageFallbackMetadataBase, getSocialImageFallbackMetadataBase,
isStringOrURL, isStringOrURL,
...@@ -152,11 +152,8 @@ export const resolveOpenGraph: FieldResolverExtraArgs< ...@@ -152,11 +152,8 @@ export const resolveOpenGraph: FieldResolverExtraArgs<
const key = k as keyof ResolvedOpenGraph const key = k as keyof ResolvedOpenGraph
if (key in og && key !== 'url') { if (key in og && key !== 'url') {
const value = og[key] const value = og[key]
if (value) { // TODO: improve typing inferring
const arrayValue = resolveAsArrayOrUndefined(value) ;(target as any)[key] = value ? resolveArray(value) : null
/// TODO: improve typing inferring
;(target as any)[key] = arrayValue
}
} }
} }
target.images = resolveImages( target.images = resolveImages(
......
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