#Running Shiki on the server with Cloudflare Workers

4 messages · Page 1 of 1 (latest)

shy violet
#

I have my app running on CF Workers, and I'm using @astro/markdown-remark to hydrate a markdown string on the server. However, this means that when I go to view that page, I get a 500 due to:

✘ [ERROR] CompileError: WebAssembly.instantiate(): Wasm code generation disallowed by embedder

What can I do about this?

proud mantleBOT
#

Hi @shy violet. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

The error you’re seeing:

✘ [ERROR] CompileError: WebAssembly.instantiate(): Wasm code generation disallowed by embedder

is due to Cloudflare Workers’ restrictions on dynamic WebAssembly (WASM) compilation. Cloudflare Workers do not allow the use of WebAssembly.instantiate() or WebAssembly.instantiateStreaming() with arbitrary code fetched at runtime for security reasons. Instead, you must use WASM modules that are statically imported and bundled at build time.

Astro’s Cloudflare integration supports importing .wasm files as modules, which are then bundled and can be instantiated safely in the Worker environment. Here’s how you can do it:

  1. Statically import your WASM module
    Instead of using WebAssembly.instantiateStreaming(fetch(...)), import the WASM file directly:

    import mod from '../util/add.wasm';
    const addModule = new WebAssembly.Instance(mod);
    

    This approach is supported in Astro’s Cloudflare adapter and is compatible with the Cloudflare Workers runtime. The WASM file is bundled and available at runtime, avoiding the dynamic instantiation restriction. See the Cloudflare Module Imports section for more details.

  2. Check your dependencies
    Some libraries (like Shiki) may attempt to instantiate WASM dynamically or fetch it at runtime, which is not allowed. If you’re using a library that does this, you may need to patch it or use a version/configuration that supports static imports.

#
  1. Summary
    • Do not use WebAssembly.instantiate() or instantiateStreaming() with runtime-fetched WASM.
    • Use static imports for WASM modules so they are bundled at build time.
    • If a dependency does not support this, you may need to look for alternatives or see if the library can be configured to use static imports.

For more information and an example, see the Cloudflare Module Imports documentation.

If you need to use a library that only supports dynamic WASM instantiation, it may not be compatible with Cloudflare Workers due to these platform restrictions. In that case, you’ll need to look for a compatible alternative or see if the library can be adapted to use static WASM imports.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: