josdejong / workerpool

Offload tasks to a pool of workers on node.js and in the browser
Apache License 2.0
2.05k stars 147 forks source link

Next.js #457

Closed moichim closed 2 weeks ago

moichim commented 1 month ago

Hi, please, has someone managed to make workerpool run in Next.js? I tried several approaches, non of them are working...

1.

"use client";

import { useEffect, useRef } from "react";
import { Pool } from "workerpool";

const ComponentWithPool: React.FC<React.PropsWithChildren> = ({children}) => {

    const pool = useRef<Pool>();

    useEffect( () => {

        import( "workerpool" ).then( result => {
            pool.current = result.pool();
        } );

    }, [] );

    return <div>{children}</div>
}

export default ComponentWithPool;

2.

"use client";

import { useRef } from "react";
import * as workerpool from "workerpool";
import { Pool } from "workerpool";

const ComponentWithPool: React.FC<React.PropsWithChildren> = ({children}) => {

    const pool = useRef<Pool>( workerpool.pool() );

    return <div>{children}</div>
}

export default ComponentWithPool;

The components above are loaded dynamically in page.tsx:

const ComponentWithPool = dynamic(
  () => import( "../components/ComponentWithPool" ),
  { ssr: false }
);

In both cases, I get the following error:

Build Error

Failed to compile

Next.js (14.2.5)
../../node_modules/.pnpm/workerpool@9.1.3/node_modules/workerpool/dist/workerpool.js:49:1

Module not found: Can't resolve 'worker_threads'
https://nextjs.org/docs/messages/module-not-found

Import trace for requested module:
./components/App.tsx
./app/page.tsx

Have I missed something?

In fact, what I try to do is much more complex - I have a TS library using workerpool internally. But the components using it have the same issues.

josdejong commented 1 month ago

I'm not sure how I can help you with this, sorry.

josdejong commented 2 weeks ago

I'll now close this issue due to a lack of activity. I hope you've found a solution.