# Bundlers and CSP

> Vite, webpack, Next and esbuild: how the SDK's worker gets to the page, what a Content-Security-Policy needs, and what the integrity hash covers.

The SDK runs its mesh in a Web Worker, so using it from a bundled app has one extra question: where does `worker.js` come from, and may the page start it? This page answers both for the common setups. The basics of the CDN, npm and TypeScript are in [Loading the SDK](https://jsflow.com/docs/guides/loading-the-sdk.md).

## What each bundler does

| Setup | The worker | What you do |
|---|---|---|
| **Vite** | bundled automatically | Nothing. |
| **webpack** | bundled automatically | Nothing. |
| **Next** | bundled automatically | Nothing (import the SDK in a client component). |
| **esbuild** | not bundled | Copy `worker.js` beside your output, or pass its URL. |
| **The CDN** | next to `jsflow.js` | Nothing: both files are in the version's folder. |

The SDK starts its worker with the standard `new Worker(new URL('./worker.js', import.meta.url), { type: 'module' })`, which Vite, webpack and Next recognise and bundle. jsflow's own tests build a page with plain ES modules, Vite, webpack and Next. esbuild is **not in those tests yet**: what follows for it comes from how the worker is started, not from a test run.

## esbuild

esbuild has no support for workers started that way. With `format: 'esm'` it leaves the `new URL(…, import.meta.url)` as it is, so the worker is loaded from beside your output file, and the only job is to put it there:

```js title="build.mjs"
import { build } from 'esbuild';
import { copyFile } from 'node:fs/promises';

await build({
  entryPoints: ['src/main.ts'],
  bundle: true,
  format: 'esm',
  outdir: 'dist',
});
await copyFile('node_modules/jsflow-sdk/worker.js', 'dist/worker.js');
```

With another output format (`iife`, `cjs`), where `import.meta.url` is empty, or when you serve the file from somewhere else, say where it is. The URL is resolved against the page:

```ts check title="main.ts"
import { join } from 'jsflow-sdk';

const room = await join({
  app: 'pk_…',
  room: 'lobby',
  worker: '/assets/worker.js',
});
room.on('state', (s) => console.log(s));
```

Two rules for a copied worker:

- **Copy it from the same version of the package as `jsflow.js`.** The two halves talk to each other, and a mismatch fails in ways that are hard to read. When you update the SDK, copy the worker again (a build step, as above, does that for you).
- **Copy it as it is.** `worker.js` is self-contained: it has no imports to resolve and nothing to bundle.

## Content-Security-Policy

A page with a Content-Security-Policy has to let the SDK do three things: load its code, start the worker, and reach the signalling service.

```
script-src https://cdn.jsflow.com;
worker-src blob:;
connect-src https://signal.jsflow.com https://*.signal.jsflow.com
```

It is one header; the directives are on separate lines here to fit the page.

- **`worker-src blob:`**: the SDK starts the worker from a `blob:` URL that imports the real `worker.js`, because browsers refuse a worker script from another origin. This is needed whether the SDK comes from the CDN or from your bundle.
- **`script-src`**: with the CDN, it names `https://cdn.jsflow.com`. With npm, drop that entry: the SDK is part of your own scripts, and the origin that serves your `worker.js` is already one of yours.
- **`connect-src`**: both entries. The first request goes to `signal.jsflow.com`, and the service may send the SDK on to a regional host such as `eu.signal.jsflow.com`, which the wildcard covers (a wildcard does not match the bare name).

If the console shows a CSP error naming `worker-src` or `connect-src`, a directive above is missing; [Troubleshooting](https://jsflow.com/docs/guides/troubleshooting.md) lists the symptoms.

## Integrity (SRI)

A subresource-integrity hash lets the browser refuse a file that differs from the one you pinned. The SDK's `sri.json` carries the SHA-384 of `jsflow.js` and `worker.js`, and it is the same in the npm package and in the CDN's version folder, because both are made by one build.

- **From the CDN**, use the hash in an import map, as [Loading the SDK](https://jsflow.com/docs/guides/loading-the-sdk.md#integrity-sri) shows.
- **From npm**, the hash does not apply to your output: a bundler rewrites the files, so what the browser loads is no longer the file the hash describes. What protects an npm install is your lockfile, which pins the package's own hash.

Source: https://jsflow.com/docs/guides/bundlers-and-csp/
