kvnang / workers-og

An `og:image` (social card) generator that is fast, browser-less (no Puppeteer), and capable of running on the edge. This package is designed to be used with Cloudflare Workers (but may be used elsewhere), with the simple API inspired by `@vercel/og`.
MIT License
194 stars 18 forks source link

Error: Already initialized. The `initWasm()` function can be used only once. #9

Closed ItsWendell closed 7 months ago

ItsWendell commented 7 months ago

Currently, when a worker already initialized the wasm modules, the initialization throws the error: Error: Already initialized. TheinitWasm()function can be used only once..

This package already uses try ... catch around the initialization functions, and does a console.error when this happens, even though this indicates that it's safe to continue using the module. I would love this error not to show up when this happens, since this clutters our logs.

In my opinion, we should actually throw the error once it's not the Already initialized error, and prevent initializing twice.

Suggested approach

I'm willing to open a PR if this approach sounds good!

kvnang commented 7 months ago

Hi @ItsWendell , thanks for raising this issue. Good point, the fact that resvg throws that error can clutter the logs quickly.

I don't think setting up global variable is the best approach here as resvg is already doing that with this variable:

var initialized = false;

And so I believe there won't be any performance benefit either by checking this ourselves.

So I think a simple matching of the error message, and then skipping the console.error would suffice: https://github.com/kvnang/workers-og/commit/44e526f6eedb1fff6076c63c0c45aefb981a30e9

And also, actually throwing the error sounds like a good idea to me, so that's been implemented as well. https://github.com/kvnang/workers-og/commit/3aac7691579cf584f8d8d229b191e0a1278eaa75

Anything else feel free to re-open this. Thanks!