Skip to content

Commit

Permalink
use file exists
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Apr 12, 2023
1 parent 299727b commit 9761bd9
Showing 1 changed file with 8 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ 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 @@ -44,9 +45,6 @@ async function nextMetadataImageLoader(this: any, content: Buffer) {
'[name].[ext]',
opts
)
const resolveAltText = this.getResolve({
extensions: ['.alt.txt'],
})

const isDynamicResource = pageExtensions.includes(extension)
const pageRoute = isDynamicResource ? fileNameBase : interpolatedName
Expand Down Expand Up @@ -114,18 +112,13 @@ async function nextMetadataImageLoader(this: any, content: Buffer) {
}),
}
if (type === 'openGraph' || type === 'twitter') {
let alt
try {
const altPath = await resolveAltText(
context,
path.join(path.dirname(resourcePath), fileNameBase + '.alt.txt')
)
alt = await fs.readFile(altPath, 'utf8')
} catch (e: any) {
if (!e?.message?.includes("Can't resolve")) throw e
}
if (alt) {
imageData.alt = alt
const altPath = path.join(
path.dirname(resourcePath),
fileNameBase + '.alt.txt'
)

if (await fileExists(altPath)) {
imageData.alt = await fs.readFile(altPath, 'utf8')
}
}

Expand Down

0 comments on commit 9761bd9

Please sign in to comment.