Skip to content

OpenGraph-Image unable to locate static files via process.cwd() #77497

@danieldelcore

Description

@danieldelcore

Link to the code that reproduces this issue

https://nextjs.org/docs/app/api-reference/file-conventions/metadata/opengraph-image#generate-images-using-code-js-ts-tsx

To Reproduce

  1. Navigate to https://hypermod-a6gx8i6po-codeshiftcommunity.vercel.app/explore/react-19-remove-forwardref and attempt to read opengraph image (https://www.opengraph.xyz/)
  2. Vercel server will log an error stating that it is unable to access static files
[Error: ENOENT: no such file or directory, open '/var/task/public/images/og/logo.png'] {
  errno: -2,
  code: 'ENOENT',
  syscall: 'open',
  path: '/var/task/public/images/og/logo.png'
}

However in development mode this works as expected

Current vs. Expected behavior

Current behaviour appears to be that the following code does not work as expected since cwd is not the same in prod/dev environments

const interSemiBold = await readFile(join(process.cwd(), 'assets/Inter-SemiBold.ttf'))

My code is as follows:

import { readFile } from 'node:fs/promises';
import { ImageResponse } from 'next/og';
import { join } from 'node:path';

export const alt = 'Foo';
export const size = { width: 1200, height: 630 };

export const contentType = 'image/png';

// Image generation
export default async function Image({ params }: { params: { slug: string } }) {
  const cwd = process.cwd();
  const font = await readFile(join(cwd, '/assets/fonts/Inter-Bold.ttf'));
  const fontSrc = Uint8Array.from(font).buffer;
  const logoData = await readFile(join(cwd, 'public/images/og/logo.png'));
  const logoSrc = Uint8Array.from(logoData).buffer;

  return new ImageResponse((<div>...<div>),
    {
      ...size
      fonts: [
       {
          name: 'Inter',
          data: fontSrc,
          style: 'normal',
          weight: 500
        }
      ]
    }
  );
}
Image

Provide environment information

Vercel Prod
Next: 14.2.15
App router

Which area(s) are affected? (Select all that apply)

Image (next/image)

Which stage(s) are affected? (Select all that apply)

Vercel (Deployed)

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Image (next/image)Related to Next.js Image Optimization.invalid linkThe issue was auto-closed due to a missing/invalid reproduction link. A new issue should be opened.

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions