> For the complete documentation index, see [llms.txt](https://docs.aurelia.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.aurelia.io/recipes/using-webworkers.md).

# Web Workers Integration

Web Workers keep expensive computations off Aurelia’s main thread. The current Aurelia starter uses Vite, which natively supports bundling workers via the [`new Worker(new URL(...), { type: 'module' })`](https://vitejs.dev/guide/features.html#importing-workers) pattern. The following steps show how to add a TypeScript worker, exchange messages safely, and keep TypeScript aware of worker modules.

## 1. Ensure TypeScript knows about worker APIs

Update `tsconfig.json` (or `tsconfig.app.json`) so the `lib` array includes `webworker` alongside `dom`:

```json
{
  "compilerOptions": {
    "lib": ["dom", "dom.iterable", "esnext", "webworker"],
    "types": []
  }
}
```

This gives the worker file access to `self`, `postMessage`, and related types without pulling in Node globals.

## 2. Create a worker entry file

```ts
// src/workers/fibonacci.worker.ts
/// <reference lib="webworker" />

function fibonacci(n: number): number {
  if (n <= 1) return n;
  return fibonacci(n - 1) + fibonacci(n - 2);
}

self.onmessage = (event: MessageEvent<number>) => {
  const input = event.data;
  const result = fibonacci(input);
  self.postMessage(result);
};
```

## 3. Wrap worker creation for DI-friendly usage

Vite exposes worker URLs via `new URL('./file', import.meta.url)`. Export a factory that returns a strongly typed worker instance:

```ts
// src/workers/create-fibonacci-worker.ts
export function createFibonacciWorker(): Worker {
  return new Worker(new URL('./fibonacci.worker.ts', import.meta.url), {
    type: 'module',
  });
}
```

## 4. Declare a module type for worker imports (optional)

If your tooling complains when importing worker URLs, add a declaration file:

```ts
// src/types/worker.d.ts
declare module '*?worker&inline' {
  const workerConstructor: { new(): Worker };
  export default workerConstructor;
}
```

(With the `new URL('./worker', import.meta.url)` pattern this is usually unnecessary, but keep the declaration if your editor requires it.)

## 5. Use the worker inside an Aurelia component

```ts
// src/components/my-component.ts
import { ICustomElementViewModel } from 'aurelia';
import { createFibonacciWorker } from '../workers/create-fibonacci-worker';

export class MyComponent implements ICustomElementViewModel {
  result: number | null = null;
  private worker: Worker | null = null;

  startWorker(input: number) {
    this.disposeWorker();
    this.worker = createFibonacciWorker();
    this.worker.onmessage = (ev: MessageEvent<number>) => {
      this.result = ev.data;
      this.disposeWorker();
    };
    this.worker.onerror = (err) => {
      console.error('Worker error', err);
      this.disposeWorker();
    };
    this.worker.postMessage(input);
  }

  detaching() {
    this.disposeWorker();
  }

  private disposeWorker() {
    this.worker?.terminate();
    this.worker = null;
  }
}
```

```html
<!-- src/components/my-component.html -->
<template>
  <input type="number" value.bind="userInput" debounce="200">
  <button click.trigger="startWorker(+userInput || 0)">Calculate Fibonacci</button>
  <p if.bind="result !== null">Worker result: ${result}</p>
</template>
```

## 6. Sharing types between main thread and worker

Create a shared interface so both sides agree on message shapes:

```ts
// src/workers/messages.ts
export type WorkerRequest = { kind: 'fibonacci'; value: number };
export type WorkerResponse = { kind: 'fibonacci'; result: number };
```

Use those types inside the worker and component for additional safety.

## 7. Debugging tips

* Use `worker.postMessage({})` only with structured-cloneable data (objects, ArrayBuffers, typed arrays). Avoid functions or DOM nodes.
* Workers live in their own file scope—import only what you need. Vite will tree-shake unused code.
* If you need multiple workers, create helper factories (`createImageWorker`, `createSearchWorker`, etc.) so components can spin them up lazily.

With this Vite-native worker setup, you avoid legacy webpack `worker-loader` plugins and keep Aurelia’s main thread responsive during heavy computations.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.aurelia.io/recipes/using-webworkers.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
