Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support alt.txt for static metadata og image #48290

Merged
merged 2 commits into from
Apr 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export type CollectedMetadata = {
export type MetadataImageModule = {
url: string
type?: string
alt?: string
} & (
| { sizes?: string }
| {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import type {
MetadataImageModule,
PossibleImageFileNameConvention,
} from './metadata/types'
import fs from 'fs/promises'
import path from 'path'
import loaderUtils from 'next/dist/compiled/loader-utils3'
import { getImageSize } from '../../../server/image-optimizer'
import { imageExtMimeTypeMap } from '../../../lib/mime-type'
import { fileExists } from '../../../lib/file-exists'

interface Options {
route: string
Expand Down Expand Up @@ -109,6 +111,16 @@ async function nextMetadataImageLoader(this: any, content: Buffer) {
: `${imageSize.width}x${imageSize.height}`,
}),
}
if (type === 'openGraph' || type === 'twitter') {
const altPath = path.join(
path.dirname(resourcePath),
fileNameBase + '.alt.txt'
)

if (await fileExists(altPath)) {
imageData.alt = await fs.readFile(altPath, 'utf8')
}
Comment on lines +120 to +122
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe more of a question for @sokra - possible to have this added as a dependency, and is there a way to cache it?

}

return `\
import path from 'next/dist/shared/lib/isomorphic/path'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
A alt txt for og
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
A alt txt for twitter
30 changes: 16 additions & 14 deletions test/e2e/app-dir/metadata/metadata.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -478,21 +478,23 @@ createNextDescribe(

it('should pick up opengraph-image and twitter-image as static metadata files', async () => {
const $ = await next.render$('/opengraph/static')
expect($('[property="og:image"]').attr('content')).toBe(
'https://example.com/opengraph/static/opengraph-image.png?b76e8f0282c93c8e'
)
expect($('[property="og:image:type"]').attr('content')).toBe(
'image/png'
)
expect($('[property="og:image:width"]').attr('content')).toBe('114')
expect($('[property="og:image:height"]').attr('content')).toBe('114')

expect($('[name="twitter:image"]').attr('content')).toBe(
'https://example.com/opengraph/static/twitter-image.png?b76e8f0282c93c8e'
)
expect($('[name="twitter:card"]').attr('content')).toBe(
'summary_large_image'
)
const match = createMultiHtmlMatcher($)
await match('meta', 'property', 'content', {
'og:image:width': '114',
'og:image:height': '114',
'og:image:type': 'image/png',
'og:image:alt': 'A alt txt for og',
'og:image':
'https://example.com/opengraph/static/opengraph-image.png?b76e8f0282c93c8e',
})

await match('meta', 'name', 'content', {
'twitter:image':
'https://example.com/opengraph/static/twitter-image.png?b76e8f0282c93c8e',
'twitter:image:alt': 'A alt txt for twitter',
'twitter:card': 'summary_large_image',
})

// favicon shouldn't be overridden
const $icon = $('link[rel="icon"]')
Expand Down