Loads and renders "Iron Waltz" (public/iron-waltz.gp, a Guitar Pro file of alphaTab's own demo song) and plays it back in a Next.js App Router page, bundled with Turbopack, the default bundler of Next.js 16.
Turbopack bundles alphaTab's Web Workers but not its Audio Worklet, and it cannot run the alphaTab webpack plugin. This sample copies the files Turbopack does not handle into public/. If you can use webpack (next dev --webpack, next build --webpack), prefer the nextjs-16-webpack sample: it needs no copy step and loads alphaTab once.
| Package | Version |
|---|---|
| Next.js | 16 (Turbopack) |
| React | 19 |
@coderline/alphatab |
1.9 |
npm install
npm run dev # copies the alphaTab assets, then next dev
npm run build # copies the alphaTab assets, then next build
npm run start # serve the production build- Workers: bundled. alphaTab detects Turbopack and starts its workers with
new Worker(new URL('./alphaTab.worker.mjs', import.meta.url), { type: 'module' }). Turbopack bundles them, and they share alphaTab's core chunk with the page. - Audio Worklet: not bundled. Turbopack copies the worklet entry referenced by
audioWorklet.addModule(new URL(...))as a plain file whose import of alphaTab's core does not resolve. Under Turbopack, alphaTab therefore loads the worklet fromcore.scriptFile. - No plugin. Turbopack does not run webpack plugins, so nothing copies the font and SoundFont either.
Runs before dev and build (predev, prebuild) and copies from node_modules/@coderline/alphatab/dist into public/alphatab/:
font/andsoundfont/, referenced bycore.fontDirectoryandplayer.soundFont;alphaTab.mjsandalphaTab.core.mjs, the entry the Audio Worklet loads (core.scriptFile) and the core it imports.
public/alphatab/ is generated and listed in .gitignore.
The client component imports @coderline/alphatab and points alphaTab to the copied files:
import * as alphaTab from '@coderline/alphatab';
const basePath = process.env.NEXT_PUBLIC_BASE_PATH;
const api = new alphaTab.AlphaTabApi(element, {
core: {
file: `${basePath}/iron-waltz.gp`,
fontDirectory: `${basePath}/alphatab/font/`,
scriptFile: `${basePath}/alphatab/alphaTab.mjs`
},
player: {
enablePlayer: true,
enableCursor: true,
soundFont: `${basePath}/alphatab/soundfont/sonivox.sf2`
}
});The rest is the same as with webpack: page.tsx stays a server component, ScoreViewer is a client component that creates the AlphaTabApi in useEffect (client components are also rendered on the server; effects only run in the browser) and destroys it in the cleanup.
To serve the app below a sub-path, set basePath in next.config.ts (e.g. '/sub'). Next.js applies basePath to its routes and bundles, so the workers load without changes. It does not apply it to URLs your code builds for files in public/, so the config exposes it as NEXT_PUBLIC_BASE_PATH and ScoreViewer prefixes core.file, core.fontDirectory, core.scriptFile and player.soundFont with it. Without the prefix, these requests go to the server root, fail with 404, and nothing renders.
- alphaTab is loaded twice. Page and workers share one core chunk, but the Audio Worklet loads its own unbundled, unminified copy of the core from
public/alphatab/alphaTab.core.mjs(about 2.5 MB). - The copied files come from the installed
@coderline/alphatab; they are refreshed on everydevandbuild, so they never drift from the bundled version. next devin Next.js 16.4 writes anAGENTS.mdinto the project folder (setagentRules: falseinnext.config.tsto disable it). It is unrelated to alphaTab.