gatsby-worker is a utility package within the Gatsby ecosystem, currently at version `2.16.0`, designed for creating worker pools to offload CPU-intensive tasks into separate Node.js processes. Inspired by `jest-worker`, it enables efficient parallel execution, improving build times and overall performance for Gatsby sites. The package follows Gatsby's release cadence, typically aligning with major and minor Gatsby releases, which often see minor versions published every two weeks for the core framework. It provides a type-safe API, allowing developers to define worker modules with explicit function signatures using TypeScript. Key features include queuing tasks on single or all workers, robust parent-worker messaging capabilities, and granular control over worker lifecycle and environment variables. It requires Node.js versions `>=18.0.0 <26` for operation.
npm install gatsby-workerVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to create a worker pool, define CPU-intensive tasks in a separate worker file, execute tasks on single or all workers, and gracefully shut down the pool, while also showcasing environment variable passing and logging.
Upgrade your Node.js runtime using a version manager like `nvm` (e.g., `nvm install 20 && nvm use 20`) or `volta` to a supported LTS version.
Always `await` the promises returned by `workerPool.single` or `workerPool.all` operations before calling `workerPool.end()` to ensure all tasks have a chance to complete. Implement `.catch()` for robust error handling on individual task promises.
Reserve `gatsby-worker` for truly CPU-intensive computations that block the event loop, such as image processing, complex data transformations, or heavy computations. For I/O, rely on Node.js's native asynchronous capabilities.
Always use `import { SymbolName } from 'gatsby-worker'` for importing components like `WorkerPool`. If in a CommonJS environment, ensure your build setup correctly transpiles or bundles ES Modules. For worker paths, `require.resolve()` remains appropriate.Ensure your files are treated as ES Modules where `gatsby-worker` is imported and use `import { WorkerPool } from 'gatsby-worker';`. For `.js` files, this typically means having `"type": "module"` in your `package.json` or using `.mjs` file extensions.Update your Node.js version to one supported by Gatsby (e.g., using `nvm install --lts && nvm use --lts` or `volta install node@20`). Consult Gatsby's documentation for the most up-to-date Node.js support matrix.
Set `silent: false` in the `WorkerPool` options to allow worker stdout/stderr to be logged to the parent process's console for debugging. Add robust `try/catch` blocks around your worker function logic and ensure all promises are handled to prevent uncaught rejections.
No dependency data recorded yet.