This is a minimal example of how to use Next.js with Threads.js, using only TypeScript.
Start a new Next.js project with TypeScript.
My recommendation is to use create-t3-app.
npm create t3-app@latest
Don't use this repo as a template for your Next.js project. It's not a template, it's a minimal example.
Add threads
as a dependency.
npm i threads
Add ts-node
as a dev dependency.
npm i -D ts-node
Add the following code to tsconfig.json
:
{
"ts-node": {
"compilerOptions": {
"module": "CommonJS"
}
},
... // other options
}
Copy the content of the api
folder to your api
folder.
Copy the workers
folder to your project
Start the development server.
npm run dev
Next, the workers/workers.config.ts
file needs to be adjusted according to your project:
This file is only used for improved type-safety. You can use the
Worker
class directly if you want.
Go to the fibonacci endpoint and check that everything works.
If the error Error: Cannot find module
appears, add or remove the string ../
from the code of the class SingleThreadWorker
.
Then, do the same for the fibonacciPool endpoint and the class PoolWorker
.
Happy Coding!