Next.js Discord

Discord Forum

OG Image custom font URL error

Unanswered
American posted this in #help-forum
Open in Discord
AmericanOP
Hello!

I'm trying to use a custom font in vercel/og

This is my code:
const fontPromise = fetch(
  new URL("../../../assets/fonts/CalSans-SemiBold.woff2", import.meta.url),
).then((res) => res.arrayBuffer());

as seen on https://vercel.com/docs/functions/edge-functions/og-image-generation/og-image-examples#using-a-custom-font

but I'm getting the following error:
node:internal/deps/undici/undici:11576
    Error.captureStackTrace(err, this);
          ^

TypeError: Failed to parse URL from /_next/static/media/CalSans-SemiBold.90475aac.woff2
    at Object.fetch (node:internal/deps/undici/undici:11576:11)


The file doesn't exist in that directory, how do I get it built?

24 Replies

Silky ant
@American @Filipino Venus Did any of you guys manage to fix this or work around it? I've spent some hours and still can't get it done
AmericanOP
I didn't find a solution, sorry 😩 just removed that part of the code
Silky ant
;<
@Silky ant <@131748314256244736> <@250927170325708801> Did any of you guys manage to fix this or work around it? I've spent some hours and still can't get it done
Firstly try using the complete path (without public) inside that URL. dont use import.meta.url either
Silky ant
Same issue
TypeError: Invalid URL
    at new URL (node:internal/url:775:36)
    at Image (webpack-internal:///(app-metadata-route)/./app/opengraph-image.tsx:17:5)
    at GET (webpack-internal:///(app-metadata-route)/./node_modules/next/dist/build/webpack/loaders/next-metadata-route-loader.js?page=%2Fopengraph-image%2F%5B%5B...__metadata_id__%5D%5D%2Froute&isDynamic=1!./app/opengraph-image.tsx?__next_metadata_route__:43:10)
    at /home/nicu/repos/web/trainsync/node_modules/next/dist/compiled/next-server/app-route.runtime.dev.js:6:63257
    at /home/nicu/repos/web/trainsync/node_modules/next/dist/server/lib/trace/tracer.js:133:36
    at NoopContextManager.with (/home/nicu/repos/web/trainsync/node_modules/next/dist/compiled/@opentelemetry/api/index.js:1:7062)
    at ContextAPI.with (/home/nicu/repos/web/trainsync/node_modules/next/dist/compiled/@opentelemetry/api/index.js:1:518)
    at NoopTracer.startActiveSpan (/home/nicu/repos/web/trainsync/node_modules/next/dist/compiled/@opentelemetry/api/index.js:1:18093)
    at ProxyTracer.startActiveSpan (/home/nicu/repos/web/trainsync/node_modules/next/dist/compiled/@opentelemetry/api/index.js:1:18854)
    at /home/nicu/repos/web/trainsync/node_modules/next/dist/server/lib/trace/tracer.js:122:103
    at NoopContextManager.with (/home/nicu/repos/web/trainsync/node_modules/next/dist/compiled/@opentelemetry/api/index.js:1:7062)
    at ContextAPI.with (/home/nicu/repos/web/trainsync/node_modules/next/dist/compiled/@opentelemetry/api/index.js:1:518)
    at NextTracerImpl.trace (/home/nicu/repos/web/trainsync/node_modules/next/dist/server/lib/trace/tracer.js:122:28)
    at /home/nicu/repos/web/trainsync/node_modules/next/dist/compiled/next-server/app-route.runtime.dev.js:6:56774
    at AsyncLocalStorage.run (node:async_hooks:346:14) {
  code: 'ERR_INVALID_URL',
  input: '/fonts/Geist-Bold.ttf'
}
Whats the URL you used?
Silky ant
const fontData = await fetch(new URL("/fonts/Geist-Bold.ttf")).then((res) =>
res.arrayBuffer()
);
I meant the complete url
With localhost included.
Silky ant
smh this worked
but ill have to switch for the domain in production right?
Which is why i assumed metadataBase should have worked.
Silky ant
same but it does not apparently
Did you try metadabase without the import.meta thing?
Silky ant
yeah
export const metadata: Metadata = {
  metadataBase: new URL("http://localhost:3000"),
};
  const fontData = await fetch(new URL("/fonts/Geist-Bold.ttf")).then((res) =>
    res.arrayBuffer()
  );

this doesnt work for some reason
Maybe it doesn't for og images ig since they aren't a page.
Silky ant
could be, i will test it in production. Thank you
Another example of mine that works, this time in a route https://github.com/leonlarsson/friendly-time/blob/main/app%2Fog%2Froute.tsx
Hope it's of any help
Silky ant
  const regularFontUrl = new URL("../../public/fonts/Inter-Regular.ttf", import.meta.url);

i've tried exactly this in /api/og and it did not work. its probably because of the newer next or og versions I dont know

thanks a lot regardless