Giter Site home page Giter Site logo

gatsby-plugin-workerize-loader's Introduction

gatsby-plugin-workerize-loader

A Gatsby plugin to integrate web workers into your GatsbyJS app via workerize-loader.

Install

Latest 2.0.0 version is compatible with Gatsby 3 and Webpack 5.

With Yarn:

yarn add gatsby-plugin-workerize-loader -D

Or with npm:

npm install --save-dev gatsby-plugin-workerize-loader

For Gatsby 2, please use version 1.5.0.

yarn add [email protected] -D

Or with npm:

npm install --save-dev [email protected]

Usage

Add plugin to gatsby-config.js

plugins: ['gatsby-plugin-workerize-loader']

Create a worker file with suffix .worker.js e.g. search.worker.js.

// search.worker.js
export async function search(searchInput) {
  // expensive search procedure...

  return searchResults
}

Import and instantiate the web worker in your source file. One caveat is web worker only works in browser environment.

// searchWorker.js
import SearchWorker from 'path/to/search.worker.js'

const searchWorker = typeof window === 'object' && new SearchWorker()

export default searchWorker

Use it in your component

// SearchComponent.js
import searchWorker from "path/to/seachWorker.js";

function SearchComponent() {
  useEffect(() => {
    searchWorker.search(searchInput).then(
      searchResults => // do something with search results
    );
  }, [searchInput]);
}

Recipes

Lazy initialization

Worker code is only downloaded and parsed when you first instantiate Worker i.e. new Worker(). Automatic code-splitting ๐Ÿ”ฅ๐Ÿ”ฅ๐Ÿ”ฅ.

With useRef within a component.

// SearchComponent.js
import SearchWorker from "path/to/search.worker.js";

function SearchComponent() {
  const searchWorkerRef = useRef(null);

  function getSearchWorker() {
    if (!searchWorkerRef.current) {
      searchWorkerRef.current = new SearchWorker();
    }
    return searchWorkerRef.current;
  }

  useEffect(() => {
    getSearchWorker().search(searchInput).then(
      searchResults => // do something with search results
    );
  }, [searchInput]);
}

Or anywhere in your app.

// getSearchWorker.js
import SearchWorker from 'path/to/search.worker.js'

let searchWorker

export default function getSearchWorker() {
  if (!searchWorker) {
    searchWorker = new SearchWorker()
  }
  return searchWorker
}
// anywhere.js
import getSearchWorker from "path/to/getSearchWorker.js";

function someOperation (searchInput) {
  getSearchWorker().search(searchInput).then(
    searchResults => // do something with search results
  );

  // remaining steps
}

License

MIT

gatsby-plugin-workerize-loader's People

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

gatsby-plugin-workerize-loader's Issues

Support for Gatsby 5

This plugin doesn't appear to properly support Gatsby 5.

It works correctly with a clean state, but when restarting with cache it does not and shows the following error:
image

_workers_searchWorker__WEBPACK_IMPORTED_MODULE_3__.default.createIndex is not a function

This is only an issue on development, since production builds appear to work correctly.

Related code: diogotcorreia/ist-space-finder#2

Peer dependencies should also be updated to remove the following warning:

warn Plugin gatsby-plugin-workerize-loader is not compatible with your gatsby version 5.11.0 - It requires gatsby@^3.0.0 || ^4.0.0

I'll be investigating and (hopefully) submitting a PR shortly.

Script not preloading on build

Consider using <link rel=preload> to prioritize fetching resources that are currently requested later in page load.

Error when run page speed test. Is preload set to default or does it manually need to be enabled? didnt see anything in the setup guide

Support for Gatsby V4

I've been using Gatsby V4 with v2 of this plugin with zero issues. Could you add version 4 to the peerDependencies?

App's request for commons.js returns root HTML page when worker instantiated

Hello,

Thanks for writing this plugin! I'm running into a weird issue that I've narrowed down to instantiating a worker when using this plugin. I have a Gatsby app that loads several JS files including commons.js via its root HTML. I've configured gatsby-plugin-workerize-loader according to the instructions, and the app builds fine, but if I instantiate the worker, the fetch request for commons.js returns the root HTML instead of returning the correct contents (other JavaScript files requested concurrently seem to load fine). If I comment out the line instantiating the worker, the app works fine again (without the worker, obviously).

I'm using Gatsby 2.29.3 and gatsby-plugin-workerize-loader 1.5.0 . Any ideas what could be happening?

Gatsby Plugin Typescript Integration

Does this plugin work alongside gatsby-plugin-typescript? I'm attempting to add a webworker to an existing working TypeScript gatsby project, and I'm not currently seeing an additional thread created even though the code that should be in the webworker is running.

I just wanted to know if this plugin works with the Typescript plugin before I did more troubleshooting.

Coffee support

Could you please add support for .worker.coffee files

Duplicate copy of worker code generated during build

I got things working with this plugin (thanks!), but I'm using it with a rather large Emscripten-generated JS file, and I started hitting Heroku's slug size limits. Based on my last deploy, I had quite a bit of headroom in slug size, so I was surprised to find the error and in investigating it, I found the plugin seems to generate two copies based on the worker source:

$ ls $(find -size +5M)
./0ea3803738c15b994eda.worker.js
./0ea3803738c15b994eda.worker.js.map
./built-foobar.2ffc43.worker.js
./built-foobar.2ffc43.worker.js.map

(We can ignore the .map files--they're not relevant here.)

As far as I can tell, that first file is not actually referenced anywhere in the build result. Is this just an intermediate step? Is there a way to avoid having that in the final bundle?

Support for Gatsby v3

It looks like Gatsby v3 is not supported by this plugin currently. Using it results in the following deprecation messages, and crashes the process.

(node:50629) [DEP_WEBPACK_COMPILATION_CACHE] DeprecationWarning: Compilation.cache was removed in favor of Compilation.getCache()
(node:50629) [DEP_WEBPACK_TEMPLATE_PATH_PLUGIN_REPLACE_PATH_VARIABLES_HASH] DeprecationWarning: [hash] is now [fullhash] (also consider using [chunkhash] or [contenthash], see documentation for details)
(node:50629) [DEP_WEBPACK_DEPRECATION_ARRAY_TO_SET_INDEXER] DeprecationWarning: chunk.files was changed from Array to Set (indexing Array is deprecated)
(node:50629) [DEP_WEBPACK_CHUNK_ENTRY_MODULE] DeprecationWarning: Chunk.entryModule: Use new ChunkGraph API

not finished Building development bundle - 5.768s

error Command failed with exit code 1.

Strange error after upgrading to Gatsby V4

I am using the latest version of this plugin (2.1.0) and I keep getting this error. I tried looking for hints in the call stack with no luck.

Error: null     at Worker.eval (rpc-wrapper.js:15)

Do you know what this might be related to?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.