Developer tooling

Web Worker Boilerplate

Describe the background task, pick classic or module worker, and get matching main-thread and worker-thread code with a clean, extensible message shape.

Main thread — app.js

Goes in your page's own script

Worker thread — my-worker.js

The separate worker file

Everything is generated locally — nothing you type is sent anywhere.

Two files, one shape

Main-thread and worker code are generated together and always agree on the same message format.

🔌

Classic or module

Toggle module workers on for native import/export syntax, or keep the classic importScripts() style.

Transferable objects

Optionally include a zero-copy ArrayBuffer transfer example for large binary payloads.

How to use the Web Worker boilerplate generator

Fill in the two fields and toggles above, then copy each block into its own file.

  1. Name the worker fileSet the filename you plan to save the worker as; the generator uses it in the Worker() constructor.
  2. Describe the taskA short description is dropped into a TODO comment so the skeleton is easy to fill in later.
  3. Copy both blocksPaste the main-thread code into your page script and the worker code into the file you named.

Why use a message shape like this

Web Workers only communicate through postMessage() and the message event, which accepts any structured-cloneable value. It's tempting to send a bare string or number, but that falls apart the moment a worker needs to report progress, return partial results or signal an error. Wrapping every message as an object with a type and a payload gives both sides of the conversation a stable contract: a switch statement on type can grow to handle new cases without breaking the ones that already work.

Classic workers

Created without options. They load additional scripts with importScripts() and cannot use ES module import statements directly.

Module workers

Created with { type: 'module' }. They support native import/export syntax, so you can share code with your main bundle more easily.

{type, payload} messages

A minimal envelope: type drives a switch statement, payload carries whatever data that message kind needs.

Transferable objects

Binary data like an ArrayBuffer can be transferred instead of copied, moving ownership between threads at near-zero cost.

Web Worker boilerplate generator FAQ

Questions about workers, message shapes and transferable objects.

What is a Web Worker and why would I use one?

A Web Worker runs JavaScript on a background thread, separate from the main thread that renders your page. It is useful for CPU-heavy work like parsing large files, image processing or complex calculations, so the user interface never freezes while that work happens.

What is the difference between a classic worker and a module worker?

A classic worker loads extra files with importScripts() and has no access to ES module import/export syntax. A module worker is created with { type: 'module' } and lets you use native import statements inside the worker file, just like a regular JavaScript module.

What is the {type, payload} message shape for?

postMessage() can send any structured-cloneable value, but sending a plain string or number quickly becomes hard to extend. Wrapping every message as { type, payload } gives both sides a predictable way to branch on a switch statement and add new message kinds later without breaking existing ones.

What are transferable objects and why do they matter?

Transferable objects, such as an ArrayBuffer, can be handed to a worker by reference instead of being copied. Passing one as the second argument to postMessage() moves ownership instantly at near-zero cost, which matters a lot for large binary data like images or audio buffers.

Does this generator run any of the code, or just create the boilerplate?

It only generates text. Nothing is executed, uploaded or sent anywhere; the two code blocks are built locally in your browser from the options you choose, ready to paste into your own project.