Skip to content
Notes

IDEN (current language)

Loading WebAssembly in Workers with a module import

2 min readRisqi Romadhoni
  • Cloudflare Workers
  • WebAssembly

This site builds its Open Graph cards with @resvg/resvg-wasm, a WebAssembly library that converts SVG to PNG. On Workers, the library cannot be loaded from bytes. The approach that works is to import its .wasm file as a module.

Why it cannot be compiled from bytes

The Cloudflare documentation (opens in a new tab) says Workers does not allow WebAssembly.compile and WebAssembly.instantiate when they take bytes, for security reasons. The streaming versions, WebAssembly.compileStreaming and WebAssembly.instantiateStreaming, are not allowed either.

Importing as a module

The Cloudflare WebAssembly documentation (opens in a new tab) says the Cloudflare toolchain bundles .wasm and .wasm?module files into the Worker so they are available at runtime. The import gives a WebAssembly.Module, as the Cloudflare adapter documentation for Astro (opens in a new tab) describes. The code that builds the cards on this site uses the ?module suffix:

src/shared/lib/og-image/render.ts
import { initWasm, Resvg } from "@resvg/resvg-wasm";
import rasteriserModule from "@resvg/resvg-wasm/index_bg.wasm?module";

With a module, initWasm calls WebAssembly.instantiate with that module, not with bytes. If it receives a URL or a Response, it uses instantiateStreaming, or instantiate with bytes. Workers does not allow either path. A second call to initWasm throws an error, so the code stores the promise in rasteriserReady and reuses it.

When the cards are made

The OG card route, src/pages/og/[...path].png.ts, sets prerender = true. So the cards are made at build time, not when a request arrives. The build output for this site shows this. The og folder is in the client output folder, while the server folder has no .wasm file and no resvg code.

The Cloudflare adapter for Astro runs prerender in workerd by default. The prerenderEnvironment (opens in a new tab) option sets this. The docs say the goal is to match the production environment as closely as possible. astro.config.mjs on this site does not change that option.

Spotted a mistake? Let me know