Replies: 7 comments 12 replies
-
Hi, were you ever able to figure this out? I'm in the same situation of trying to use worker_threads from within Next.js. |
Beta Was this translation helpful? Give feedback.
-
Yes, there should be a separate example for server side worker_threads (with typescript). For now we have only example for client side web workers: https://github.com/vercel/next.js/tree/canary/examples/with-web-worker Currently worker_threads is not working out of the box with worker.ts extension. |
Beta Was this translation helpful? Give feedback.
-
Did anyone ever figure out a good pattern here? Been trying with Piscina, Threads.js and raw worker_threads with no luck. Similar sentiment: I'll settle for js but would prefer ts. |
Beta Was this translation helpful? Give feedback.
-
Here's a clever pattern that will work with a node module. Might have to be careful in dev mode if it persists. I haven't looked into the implications of that or... any of the other implications of coloring outside of the lines. (My initial use was a fire-and-forget kinda thing. Works surprisingly well.) import {
Worker
} from 'node:worker_threads';
export interface WorkerOptions {
package: string;
}
/**
* Escaping webpack!. :)
*/
export function newEscapeHatchWorker(opts:WorkerOptions) {
const escapeHatch = `
(async () => {
await import ("${opts.package}");
})();
`;
return new Worker(escapeHatch, {eval:true});
} |
Beta Was this translation helpful? Give feedback.
-
Okay, I think I found a solution that doesn't rely on any hacks and supports TypeScript. As of writing this, Next.js uses Webpack 5, so you have to reference your worker file the Webpack 5 way: new Worker(new URL('./my_worker.ts', import.meta.url)) Next, you need to update your const nextConfig = {
...,
webpack: (config, { isServer }) => {
if (isServer) {
config.output.publicPath = "";
}
return config;
}
};
export default nextConfig; |
Beta Was this translation helpful? Give feedback.
-
I made it work with typescript on localhost but I'm trying to put my app in production with Vercel and it doesn't work... maybe the worker thread aren't working at all with Vercel ? |
Beta Was this translation helpful? Give feedback.
-
So I'm running my NextJs instance in a docker container and need to execute a long-running tasks that uploads uploads bunch of files after getting "pinged" that update occurred. I'm trying to achieve this with a WorkerThread, since that should not have the inherent resource limitation of Next's route handlers. My current solution: src/api/pushed/route.ts: import { Worker } from 'node:worker_threads';
export GET = () => {
new Worker(
/* webpackChunkName: "uploadWorker" */ new URL(
'./uploadWorker.ts',
import.meta.url
),
);
} src/api/pushed/uploadWorker.ts: import { workerData } from 'node:worker_threads';
// Imports work just fine here, even import aliases next.config.js
Dockerfile addition:
Also for this to work in dev you need to disable |
Beta Was this translation helpful? Give feedback.
-
I am using the Next.js 13.4 App Router and Route Handlers. I have a computation in a Route Handler that I do not want to run on the main thread and block the thread. I am running the application on Node.js and using the standalone build. I want to use a Node.js worker thread to run the computation. How can I do this with Next.js?
When I try and add a file such as
my.worker.js
and thenimport
orrequire
it in the route handler, Next.js build bundles it together with the route handler code and the worker file is not in the output directory and I cannot get that to work. After looking at the docs and googling, comnfiguringswc
in Next.js seems to not be supported. I would like to not fall back to babel if at all possible.I'm thinking of some hack to manually copy a worker file to the output directory and then try and construct a path to it and require in such a way that
swc
is unable to resolve the code at build time, so it won't try and bundle it.Ideally, I would also use typescript, but to get it working with JavaScript is ok, just to get this working somehow.
Beta Was this translation helpful? Give feedback.
All reactions