Skip to content

Commit

Permalink
feat: facebook metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
Netail committed May 13, 2024
1 parent 47769d1 commit 46b205c
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 1 deletion.
2 changes: 1 addition & 1 deletion contributing/repository/linting.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ We recommend installing the [Prettier plugin for VS Code](https://marketplace.vi

You can find the format configuration in the [Prettier config](../../.prettierrc.json).

## alex
## Alex

We recommend installing the [AlexJS Linter extension for VSCode](https://marketplace.visualstudio.com/items?itemName=TLahmann.alex-linter)

Expand Down
14 changes: 14 additions & 0 deletions docs/02-app/02-api-reference/04-functions/generate-metadata.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -927,6 +927,20 @@ export const metadata = {
<meta name="category" content="technology" />
```

### `facebook`

```jsx filename="layout.js | page.js"
export const metadata = {
facebook: {
appId: '12345678',
},
}
```

```html filename="<head> output" hideLineNumbers
<meta property="fb:app_id" content="12345678" />
```

### `other`

All metadata options should be covered using the built-in support. However, there may be custom metadata tags specific to your site, or brand new metadata tags just released. You can use the `other` option to render any custom metadata tag.
Expand Down
1 change: 1 addition & 0 deletions packages/next/src/lib/metadata/default-metadata.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export function createDefaultMetadata(): ResolvedMetadata {
appleWebApp: null,
formatDetection: null,
itunes: null,
facebook: null,
abstract: null,
appLinks: null,
archives: null,
Expand Down
10 changes: 10 additions & 0 deletions packages/next/src/lib/metadata/generate/basic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,16 @@ export function ItunesMeta({ itunes }: { itunes: ResolvedMetadata['itunes'] }) {
return <meta name="apple-itunes-app" content={content} />
}

export function FacebookMeta({
facebook,
}: {
facebook: ResolvedMetadata['facebook']
}) {
if (!facebook) return null
const { appId } = facebook
return <meta property="fb:app_id" content={appId} />
}

const formatDetectionKeys = [
'telephone',
'date',
Expand Down
2 changes: 2 additions & 0 deletions packages/next/src/lib/metadata/metadata.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
BasicMeta,
ViewportMeta,
VerificationMeta,
FacebookMeta,
} from './generate/basic'
import { AlternatesMetadata } from './generate/alternate'
import {
Expand Down Expand Up @@ -124,6 +125,7 @@ export function createMetadataComponents({
BasicMeta({ metadata }),
AlternatesMetadata({ alternates: metadata.alternates }),
ItunesMeta({ itunes: metadata.itunes }),
FacebookMeta({ facebook: metadata.facebook }),
FormatDetectionMeta({ formatDetection: metadata.formatDetection }),
VerificationMeta({ verification: metadata.verification }),
AppleWebAppMeta({ appleWebApp: metadata.appleWebApp }),
Expand Down
5 changes: 5 additions & 0 deletions packages/next/src/lib/metadata/resolve-metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {
resolveThemeColor,
resolveVerification,
resolveItunes,
resolveFacebook,
} from './resolvers/resolve-basics'
import { resolveIcons } from './resolvers/resolve-icons'
import { getTracer } from '../../server/lib/trace/tracer'
Expand Down Expand Up @@ -196,6 +197,10 @@ function mergeMetadata({
)
break
}
case 'facebook':
target.facebook = resolveFacebook(source.facebook)
break

case 'verification':
target.verification = resolveVerification(source.verification)
break
Expand Down
7 changes: 7 additions & 0 deletions packages/next/src/lib/metadata/resolvers/resolve-basics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,3 +255,10 @@ export const resolveItunes: FieldResolverExtraArgs<
: undefined,
}
}

export const resolveFacebook: FieldResolver<'facebook'> = (facebook) => {
if (!facebook) return null
return {
appId: facebook.appId,
}
}
4 changes: 4 additions & 0 deletions packages/next/src/lib/metadata/types/extra-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ export type ResolvedAppleWebApp = {
statusBarStyle?: 'default' | 'black' | 'black-translucent'
}

export type Facebook = {
appId?: string
}

// Format Detection
// This is a poorly specified metadata export type that is supposed to
// control whether the device attempts to conver text that matches
Expand Down
15 changes: 15 additions & 0 deletions packages/next/src/lib/metadata/types/metadata-interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
import type {
AppleWebApp,
AppLinks,
Facebook,
FormatDetection,
ItunesApp,
ResolvedAppleWebApp,
Expand Down Expand Up @@ -320,6 +321,18 @@ interface Metadata extends DeprecatedMetadataFields {
*/
twitter?: null | Twitter

/**
* The Facebook metadata for the document.
* @example
* ```tsx
* { appId: "12345678" }
*
* <meta name="fb:app_id" content="12345678" />
* ```
*
*/
facebook?: null | Facebook

/**
* The common verification tokens for the document.
* @example
Expand Down Expand Up @@ -508,6 +521,8 @@ interface ResolvedMetadata extends DeprecatedMetadataFields {

twitter: null | ResolvedTwitterMetadata

facebook: null | Facebook

// common verification tokens
verification: null | ResolvedVerification

Expand Down
12 changes: 12 additions & 0 deletions test/e2e/app-dir/metadata/app/facebook/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export default function page() {
return 'facebook'
}

export async function generateMetadata() {
const metadata = {
facebook: {
appId: '12345678',
},
}
return metadata
}
9 changes: 9 additions & 0 deletions test/e2e/app-dir/metadata/metadata.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,15 @@ describe('app dir - metadata', () => {
)
})

it('should support facebook related tags', async () => {
const browser = await next.browser('/facebook')
const matchDom = createDomMatcher(browser)

await matchDom('meta', 'property="fb:app_id"', {
content: '12345678',
})
})

it('should support alternate tags', async () => {
const browser = await next.browser('/alternates')
const matchDom = createDomMatcher(browser)
Expand Down

0 comments on commit 46b205c

Please sign in to comment.