#Running Shiki on the server with Cloudflare Workers
4 messages · Page 1 of 1 (latest)
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:
-
Statically import your WASM module
Instead of usingWebAssembly.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.
-
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.
- Summary
- Do not use
WebAssembly.instantiate()orinstantiateStreaming()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.
- Do not use
For more information and an example, see the Cloudflare Module Imports documentation.