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

Custom images broken with CloudFlare Workers #63

Open
harlan-zw opened this issue Jul 4, 2023 · 2 comments
Open

Custom images broken with CloudFlare Workers #63

harlan-zw opened this issue Jul 4, 2023 · 2 comments
Labels
bug Something isn't working

Comments

@harlan-zw
Copy link
Collaborator

Describe the bug

Getting the following error on CloudFlare Workers:

{
  "data": {
    "message": "Cannot perform I/O on behalf of a different request. I/O objects (such as streams, request/response bodies, and others) created in the context of one request handler cannot be accessed from a different request's handler. This is a limitation of Cloudflare Workers which allows us to improve overall performance. (https://fonts.googleapis.com/css2?family=Inter:wght@400)",
    "stack": "",
    "statusCode": 500,
    "statusMessage": "",
    "url": "/api/og-image-font?name=Inter&weight=400"
  },
  "message": " (500  (/api/og-image-font?name=Inter&weight=400))",
  "stack": "",
  "statusCode": 500,
  "statusMessage": "",
  "url": "/api/og-image-svg?path=%2F"
}

Using the config:

ogImage: {
    // @todo this is broken in cloudflare workers
    fonts: [
      'Inter:400',
      'Inter:700',
      {
        name: 'optieinstein',
        weight: 800,
        path: '/OPTIEinstein-Black.otf',
      }
    ],
  },

Reproduction

https://playground.harlanzw.workers.dev/api/og-image-svg?path=/

System / Nuxt Info

No response

@harlan-zw harlan-zw added the bug Something isn't working label Jul 4, 2023
@harlan-zw harlan-zw changed the title Custom fonts with CloudFlare Workers don't work Custom fonts and images broken with CloudFlare Workers Dec 8, 2023
@bianpratama
Copy link
Contributor

When trying out alternatives for using custom fonts, I successfully use public URLs of font files from different sites/servers.

My site https://image.prata.ma/pratama, deployed to Cloudflare Workers.

Snippets of my composable for including fonts:

export async function useFonts(families: TFontFamily[]) {
  const fonts = []

  if (families.includes('inter')) {
    fonts.push('Inter:500', 'Inter:700')
  }

  if (families.includes('doyle')) {
    fonts.push(
      {
        name: 'dyl',
        weight: 700,
        path: 'https://assets.kelola.io/fonts/dyl-bold.woff',
      },
      {
        name: 'dyl',
        weight: 500,
        path: 'https://assets.kelola.io/fonts/dyl-medium.woff',
      },
    )
  }

  return fonts
}

The usage:

const fonts = await useFonts(['inter', 'doyle'])
defineOgImageComponent('Pratama', values.value, {
  width: values.value.width,
  height: values.value.height,
  fonts,
})

I believe this workaround should be documented as a solution for the Cloudflare Workers issue. Or are there any drawbacks to using external fonts that I don't know?

@harlan-zw harlan-zw changed the title Custom fonts and images broken with CloudFlare Workers Custom images broken with CloudFlare Workers Feb 1, 2024
@harlan-zw
Copy link
Collaborator Author

Thanks for the details, the font issue should be solved with v3.0.0-rc.36

In this version we implement Nitro server storage for the fonts. The only issue with doing this is it's going to fetch the fonts on every request which is going to slow down the render.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants