init()
Initialize the HTVG WASM module. This function must be called once before using any compilation functions (compileDocument(), compile()).
Optional WASM module input. Behavior varies by runtime:
- Browser: Omit to auto-fetch, or pass a URL/string path to the
.wasmfile - Node.js: Pass a
Bufferfromfs.readFileSync() - Cloudflare Workers: Pass the imported
WebAssembly.Module - Deno: Omit to auto-fetch, or pass a
URL
InitInput Type
Usage by Runtime
Browser
In browsers,init() automatically fetches the WASM file from the same directory as the JS module.
Custom WASM path
If you need to specify a custom path:Node.js
In Node.js, you must manually load the WASM binary and pass it toinit().
Express.js server
Cloudflare Workers
Cloudflare Workers require importing the WASM module directly.Wrangler configuration
Add this to yourwrangler.toml:
Optimized initialization
Initialize once and reuse across requests:Deno
Deno supports auto-fetching like browsers:Error Handling
init() may throw errors if:
- WASM file cannot be fetched or loaded
- Invalid WASM binary provided
- WebAssembly is not supported in the runtime
Best Practices
Initialize once
Callinit() only once per application lifecycle:
Lazy initialization
Defer initialization until first use:Serverless optimization
In serverless environments, initialize outside the handler when possible:Internal Behavior
Wheninit() is called:
- Sets up panic hooks for better error messages (in debug builds)
- Loads the WebAssembly module into memory
- Instantiates the WASM module with JavaScript bindings
- Makes compilation functions (
compileDocument,compile) available
Troubleshooting
”HTVG WASM not initialized” error
This error occurs when callingcompileDocument() or compile() before init():
WASM file not found (Browser)
Ensure the.wasm file is served from the correct path: