Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cab12e0

Browse files
committed
Add og:image meta rendering
1 parent 5326e29 commit cab12e0

File tree

7 files changed

+61
-2
lines changed

7 files changed

+61
-2
lines changed

packages/next/export/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@ export default async function exportApp(
370370
domainLocales: i18n?.domains,
371371
trailingSlash: nextConfig.trailingSlash,
372372
disableOptimizedLoading: nextConfig.experimental.disableOptimizedLoading,
373+
ogImage: nextConfig.experimental.ogImage,
373374
}
374375

375376
const { serverRuntimeConfig, publicRuntimeConfig } = nextConfig

packages/next/next-server/lib/utils.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { Env } from '@next/env'
88
import { BuildManifest } from '../server/get-page-files'
99
import { DomainLocales } from '../server/config'
1010
import { PreviewData } from 'next/types'
11+
import { OgImageConfig } from '../server/og-image-config'
1112

1213
/**
1314
* Types used by both next and next-server
@@ -206,6 +207,8 @@ export type DocumentProps = DocumentInitialProps & {
206207
scriptLoader: { afterInteractive?: string[]; beforeInteractive?: any[] }
207208
locale?: string
208209
disableOptimizedLoading?: boolean
210+
ogImage: Pick<OgImageConfig, 'height' | 'width' | 'type'>
211+
ogImageUrl?: string
209212
}
210213

211214
/**

packages/next/next-server/server/incremental-cache.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,8 @@ export class IncrementalCache {
187187
pageData?: any
188188
isNotFound?: boolean
189189
isRedirect?: boolean
190+
upstreamCache?: string
191+
contentType?: string
190192
},
191193
revalidateSeconds?: number | false
192194
) {

packages/next/next-server/server/load-components.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,27 @@ export async function loadComponents(
9393
])
9494

9595
const { getServerSideProps, getStaticProps, getStaticPaths } = ComponentMod
96+
const pageConfig = ComponentMod.config || {}
97+
98+
if (!pathname.endsWith('.image')) {
99+
try {
100+
// TODO: do we want to use findPageComponent in dev mode to
101+
// render meta tags correctly? Currently we only auto-add
102+
// complimentary meta tags in production
103+
await requirePage(pathname + '.image', distDir, serverless)
104+
pageConfig.hasOgImage = true
105+
} catch (_) {
106+
console.log('idk', _)
107+
}
108+
}
96109

97110
return {
98111
App,
99112
Document,
100113
Component,
101114
buildManifest,
102115
reactLoadableManifest,
103-
pageConfig: ComponentMod.config || {},
116+
pageConfig,
104117
ComponentMod,
105118
getServerSideProps,
106119
getStaticProps,

packages/next/next-server/server/next-server.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ import escapePathDelimiters from '../lib/router/utils/escape-path-delimiters'
9494
import { getUtils } from '../../build/webpack/loaders/next-serverless-loader/utils'
9595
import { PreviewData } from 'next/types'
9696
import HotReloader from '../../server/hot-reloader'
97+
import { OgImageConfig } from './og-image-config'
9798

9899
const getCustomRouteMatcher = pathMatch(true)
99100

@@ -165,6 +166,7 @@ export default class Server {
165166
defaultLocale?: string
166167
domainLocales?: DomainLocales
167168
distDir: string
169+
ogImage: Pick<OgImageConfig, 'height' | 'width' | 'type'>
168170
}
169171
private compression?: Middleware
170172
private incrementalCache: IncrementalCache
@@ -224,6 +226,7 @@ export default class Server {
224226
.disableOptimizedLoading,
225227
domainLocales: this.nextConfig.i18n?.domains,
226228
distDir: this.distDir,
229+
ogImage: this.nextConfig.experimental.ogImage!,
227230
}
228231

229232
// Only the `publicRuntimeConfig` key is exposed to the client side
@@ -1689,6 +1692,8 @@ export default class Server {
16891692
} as UrlWithParsedQuery)
16901693
}
16911694
} else if ((cachedData as any).image) {
1695+
res.setHeader('Content-Type', (cachedData as any).contentType)
1696+
res.setHeader('Cache-Control', (cachedData as any).upstreamCache)
16921697
// TODO: set revalidate headers
16931698
;(cachedData as any).image.pipe(res)
16941699
} else {
@@ -1727,6 +1732,8 @@ export default class Server {
17271732
sprRevalidate: number | false
17281733
isNotFound?: boolean
17291734
isRedirect?: boolean
1735+
upstreamCache?: string
1736+
contentType?: string
17301737
}> => {
17311738
let pageData: any
17321739
let html: string | null
@@ -1753,6 +1760,8 @@ export default class Server {
17531760
res.end(buffer)
17541761

17551762
return {
1763+
contentType,
1764+
upstreamCache,
17561765
imageData: buffer,
17571766
html: null,
17581767
pageData: null,
@@ -1923,6 +1932,8 @@ export default class Server {
19231932
sprRevalidate,
19241933
isNotFound,
19251934
isRedirect,
1935+
upstreamCache,
1936+
contentType,
19261937
},
19271938
} = await doRender()
19281939
let resHtml = html
@@ -1963,7 +1974,15 @@ export default class Server {
19631974
if (isOrigin && ssgCacheKey) {
19641975
await this.incrementalCache.set(
19651976
ssgCacheKey,
1966-
{ imageData, html: html!, pageData, isNotFound, isRedirect },
1977+
{
1978+
imageData,
1979+
html: html!,
1980+
pageData,
1981+
isNotFound,
1982+
isRedirect,
1983+
upstreamCache,
1984+
contentType,
1985+
},
19671986
sprRevalidate
19681987
)
19691988
}

packages/next/next-server/server/render.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ import {
6161
Redirect,
6262
} from '../../lib/load-custom-routes'
6363
import { DomainLocales } from './config'
64+
import { OgImageConfig } from './og-image-config'
6465

6566
function noRouter() {
6667
const message =
@@ -191,6 +192,7 @@ export type RenderOptsPartial = {
191192
defaultLocale?: string
192193
domainLocales?: DomainLocales
193194
disableOptimizedLoading?: boolean
195+
ogImage: Pick<OgImageConfig, 'height' | 'width' | 'type'>
194196
}
195197

196198
export type RenderOpts = LoadComponentsReturnType & RenderOptsPartial
@@ -236,6 +238,8 @@ function renderDocument(
236238
domainLocales,
237239
isPreview,
238240
disableOptimizedLoading,
241+
ogImage,
242+
ogImageUrl,
239243
}: RenderOpts & {
240244
props: any
241245
docComponentsRendered: DocumentProps['docComponentsRendered']
@@ -260,6 +264,8 @@ function renderDocument(
260264
scriptLoader: any
261265
isPreview?: boolean
262266
autoExport?: boolean
267+
ogImage: Pick<OgImageConfig, 'height' | 'width' | 'type'>
268+
ogImageUrl?: string
263269
}
264270
): string {
265271
return (
@@ -308,6 +314,8 @@ function renderDocument(
308314
scriptLoader,
309315
locale,
310316
disableOptimizedLoading,
317+
ogImage,
318+
ogImageUrl,
311319
...docProps,
312320
})}
313321
</AmpStateContext.Provider>
@@ -1085,6 +1093,9 @@ export async function renderToHTML(
10851093
isPreview: isPreview === true ? true : undefined,
10861094
autoExport: isAutoExport === true ? true : undefined,
10871095
nextExport: nextExport === true ? true : undefined,
1096+
ogImageUrl: (pageConfig as any)?.hasOgImage
1097+
? `${pathname}.image.${renderOpts.ogImage.type}`
1098+
: undefined,
10881099
})
10891100

10901101
if (process.env.NODE_ENV !== 'production') {

packages/next/pages/_document.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,8 @@ export class Head extends Component<
464464
const {
465465
styles,
466466
ampPath,
467+
ogImage,
468+
ogImageUrl,
467469
inAmpMode,
468470
hybridAmp,
469471
canonicalBase,
@@ -644,6 +646,14 @@ export class Head extends Component<
644646
name="next-head-count"
645647
content={React.Children.count(head || []).toString()}
646648
/>
649+
{ogImageUrl ? (
650+
<>
651+
<meta name="og:image" content={ogImageUrl} />
652+
<meta name="og:image:type" content={ogImage.type} />
653+
<meta name="og:image:width" content={ogImage.width.toString()} />
654+
<meta name="og:image:height" content={ogImage.height.toString()} />
655+
</>
656+
) : null}
647657
{inAmpMode && (
648658
<>
649659
<meta

0 commit comments

Comments
 (0)