0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-03-24 23:21:57 -05:00
astro/examples/snowpack/astro/pages/guides/web-worker.md
Matthew Phillips 54ba9f5ee1
Fix complex MDX parsing ()
* Fix complex MDX parsing

This allows fully MDX support using the micromark MDX extension. One caveat is that if you do something like use the less than sign, you need to escape it because the parser expects these to be tags otherwise.

* Move micromark definition
2021-04-01 16:34:11 -04:00

1.6 KiB

layout title tags published description
../../layouts/content.astro Web Workers communityGuide true How to use Web Workers in your Snowpack project.

Web Workers are a simple means for web content to run scripts in background threads.

To use Web Workers with Snowpack: Use the browser's native Web Worker API directly:

// Example: Load a Web Worker in your project
const myWorker = new Worker(new URL('./worker.js', import.meta.url));

Passing a URL to the Worker constructor (instead of a string literal) is recommended, but not required. Using a string literal (ex: new Worker('./worker.js')) may prevent some optimizations when you build your site for production.

Also note that the URL passed to the Worker must match the final URL which may differ from the path on disk. For example, ./worker.js would still be used even if the original file on disk was worker.ts. mount destinations should also be used here, if needed.

ESM Web Worker Support

ESM syntax (import/export) in Web Workers is still not supported in all modern browsers. Snowpack v3.0.0 and the Snowpack Webpack v5 plugin will both support automatic bundling once released. Until then, you'll need to write self-contained, single-file Web Workers (no ESM import/export statements) or pre-bundle your web workers yourself.

const worker = new Worker(new URL('./esm-worker.js', import.meta.url), {
  name: 'my-worker',
  type: 'module',
});