Overview
React ThorVG Fiber uses WebAssembly (WASM) to provide high-performance vector graphics rendering. Understanding how to properly configure WASM file loading is essential for production deployments.The locateFile Function
ThelocateFile function tells the WASM module where to find the .wasm binary file. Both SwCanvas and GlCanvas require this function.
Basic Usage
Function Signature
ThelocateFile prop accepts a function with this signature:
- path: The filename being requested (e.g.,
"thorvg-sw.wasm") - prefix: The default prefix/directory path
- returns: The full URL or path to the WASM file
Why useCallback?
Always wraplocateFile in useCallback to prevent unnecessary re-initialization of the WASM module:
WASM File Locations
React ThorVG Fiber provides two WASM files:thorvg-sw.wasm- Software renderer (CPU-based)thorvg-gl.wasm- WebGL renderer (GPU-based)
Package Exports
The package exports WASM files through these paths:Framework-Specific Configuration
Vite
Vite handles WASM imports automatically with the?url suffix:
- Copy the WASM file to the build output
- Generate a hashed filename for cache busting
- Return the public URL as a string
Next.js
Next.js requires webpack configuration (see Next.js Setup Guide) and imports without?url:
Other Bundlers
For webpack, Rollup, or other bundlers, you’ll need to configure WASM file handling:Advanced Patterns
CDN Hosting
Host WASM files on a CDN for better caching and performance:Environment-Based URLs
Use different WASM sources for development vs. production:Version Pinning
Pin WASM files to specific versions for cache control:Dynamic Renderer Selection
Switch between software and WebGL renderers dynamically:Lazy Loading
Load WASM files only when needed:Custom Path Resolution
Implement custom logic for WASM file resolution:Public Directory Setup
For static hosting without a bundler:cp node_modules/react-thorvg-fiber/dist/thorvg-sw.wasm public/
cp node_modules/react-thorvg-fiber/dist/thorvg-gl.wasm public/
const locateFile = useCallback(() => {
return "/thorvg-sw.wasm"; // Served from public directory
}, []);
MIME Type Configuration
Ensure your server sends WASM files with the correct MIME type:Vercel
Vercel usually handles this automatically. If needed, add tovercel.json:
Netlify
Add tonetlify.toml:
Apache
Add to.htaccess:
Nginx
Add to your server config:Performance Considerations
Caching
WASM files are relatively large (~2-3 MB). Implement aggressive caching:Preloading
Preload WASM files for faster initialization:Compression
Enable compression for WASM files. Most hosting providers support this automatically, but verify:- Brotli compression typically reduces WASM files by 60-70%
- Gzip compression reduces by 50-60%
Troubleshooting
WASM File Not Found
Problem:Failed to fetch WASM file
Solutions:
- Verify the URL returned by
locateFileis correct - Check browser DevTools Network tab for the actual request
- Ensure WASM files are included in your build output
- Check file paths and server configuration
MIME Type Errors
Problem:Incorrect MIME type for WASM file
Solutions:
- Configure your server to serve
.wasmfiles withapplication/wasmMIME type - Check response headers in browser DevTools
- See MIME Type Configuration section above
Module Initialization Failed
Problem:RuntimeError: abort(CompileError: WebAssembly.instantiate())
Solutions:
- Ensure the WASM file is not corrupted
- Verify the file is served with correct MIME type
- Check browser console for detailed error messages
- Try re-downloading/re-building the WASM file
CORS Errors
Problem:CORS policy blocked loading WASM from CDN
Solutions:
- Add CORS headers to your CDN/server:
- Or use same-origin hosting
- Add
crossorigin="anonymous"to preload links
Next Steps
- Learn about Vite Setup for detailed Vite configuration
- Learn about Next.js Setup for detailed Next.js configuration
- Explore Performance Tips for optimization strategies