Overview
Next.js requires additional webpack configuration to properly handle WebAssembly files. This guide provides complete, copy-paste ready configuration for both the App Router and Pages Router.Installation
Advanced Configuration
TypeScript Configuration
Add type declarations for WASM imports. Create or updatenext-env.d.ts:
Dynamic Imports
For better code splitting, you can dynamically import the canvas component:Custom WASM Path
Serve WASM files from a CDN or custom path:Static Export
If usingoutput: 'export' for static HTML export, ensure WASM files are in the public folder:
public/:
Troubleshooting
Webpack Configuration Not Applied
Problem: Changes tonext.config.mjs are not taking effect
Solution:
- Stop the dev server completely
- Delete
.nextfolder:rm -rf .next - Restart dev server:
npm run dev
WASM Import Errors
Problem:Module parse failed: Unexpected character when importing WASM
Solution: Ensure you’ve added the webpack configuration to next.config.mjs. Do not use the ?url suffix (that’s Vite-specific):
“use client” Missing
Problem:Error: Cannot read properties of undefined or hydration errors
Solution: Add "use client" directive at the top of files using ThorVG:
Server-Side Rendering Errors
Problem:ReferenceError: HTMLCanvasElement is not defined
Solution: Use dynamic imports with ssr: false:
Production Build Failures
Problem: Build fails with WASM-related errors Solution:- Verify webpack configuration is correct
- Clear build cache:
rm -rf .next - Try building again:
npm run build - Check that WASM files exist in
.next/static/chunks/
MIME Type Errors
Problem:Failed to load WASM: Incorrect response MIME type
Solution: Configure your hosting provider to serve .wasm files with application/wasm MIME type.
For Vercel (usually automatic), add to vercel.json if needed:
TypeScript Module Not Found
Problem: TypeScript error:Cannot find module 'react-thorvg-fiber/thorvg-sw.wasm'
Solution: Add the module declaration to next-env.d.ts as shown in the TypeScript Configuration section.
Next Steps
- Learn about WASM Configuration for advanced setup
- Explore Performance Tips to optimize rendering
- Check out Examples to see what’s possible