Skip to content

Commit 705d12c

Browse files
committed
use stat for fileSizeInMB check
1 parent 18bb847 commit 705d12c

File tree

1 file changed

+3
-7
lines changed

1 file changed

+3
-7
lines changed

packages/next/src/build/entries.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import type { LoadedEnvFiles } from '@next/env'
1313
import type { AppLoaderOptions } from './webpack/loaders/next-app-loader'
1414

1515
import { posix, join, dirname, extname, normalize, parse } from 'path'
16-
import { copyFile, mkdir, readFile } from 'fs/promises'
16+
import { copyFile, mkdir, stat } from 'fs/promises'
1717
import { stringify } from 'querystring'
1818
import fs from 'fs'
1919
import {
@@ -603,15 +603,11 @@ export async function copyMetadataStaticFiles({
603603
// x-ref: https://developer.x.com/en/docs/x-for-websites/cards/overview/summary
604604
// x-ref(facebook): https://developers.facebook.com/docs/sharing/webmasters/images
605605
const fileSizeLimit = isTwitterImage ? 5 : 8
606+
const fileSizeInMB = (await stat(filePath)).size / (1024 * 1024)
606607

607-
const buffer = Buffer.from(
608-
(await readFile(filePath)).toString('base64'),
609-
'base64'
610-
)
611-
const fileSizeInMB = buffer.byteLength / 1024 / 1024
612608
if (fileSizeInMB > fileSizeLimit) {
613609
throw new Error(
614-
`File size for ${imgName} image ${filePath} exceeds ${fileSizeLimit}MB. ` +
610+
`File size for ${imgName} image "${filePath}" exceeds ${fileSizeLimit}MB. ` +
615611
`(Current: ${fileSizeInMB.toFixed(2)}MB)\n` +
616612
'Read more: https://nextjs.org/docs/app/api-reference/file-conventions/metadata/opengraph-image#image-files-jpg-png-gif'
617613
)

0 commit comments

Comments
 (0)