Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion docs/platforms/javascript/common/best-practices/web-workers.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,32 @@ export default defineConfig({
plugins: () => [...sentryPlugin],
},
});
```
```

### SSR gotchas

When using SSR, make sure to only run `addIntegration` in a browser context.

Here's a Vite example :

```ts
import workerUrl from "./my-worker?worker&url"

const worker = new Worker(new URL(workerUrl, import.meta.url), { type: "module" })
if (!import.meta.env.SSR) {
Sentry.addIntegration(Sentry.webWorkerIntegration({ worker }))
}
Comment on lines +190 to +193
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potential bug: The documentation example for web workers unconditionally calls new Worker(), which will cause a server crash in SSR environments where the Worker object is not defined.
  • Description: The documentation examples for using web workers with Vite and Nuxt unconditionally instantiate a Worker via new Worker(...). In a Server-Side Rendering (SSR) environment, which runs on Node.js, the Worker global is not available. This will cause the application to throw a ReferenceError: Worker is not defined and crash the server. The conditional check if (!import.meta.env.SSR) is only applied to the Sentry integration call, not the worker creation itself, which is where the error occurs. Developers copying this example for their SSR applications will experience server crashes.

  • Suggested fix: Wrap the entire worker creation and Sentry integration logic within a conditional block that checks if the code is running in a browser environment, for example: if (!import.meta.env.SSR) { const worker = new Worker(...); Sentry.addIntegration(...); }.
    severity: 0.85, confidence: 0.95

Did we get this right? 👍 / 👎 to inform future reviews.

```

Refer to your framework documentation to know how to remove code from the server bundle.

For example, with Nuxt you can use :

```ts
import workerUrl from "./my-worker?worker&url"

const worker = new Worker(new URL(workerUrl, import.meta.url), { type: "module" })
if (import.meta.client) {
Sentry.addIntegration(Sentry.webWorkerIntegration({ worker }))
}
```
Loading