This repo demonstrates how you can use Next JS and Tauri together to build an application which can be deployed on the web, and also packaged up as a desktop application. Since Tauri usually doesn't support NextJS API routes, this example shows how to share code and logic between NextJS API routes and a local server used in the Tauri application.
To achieve this, the pkg package is used to package up a node server as a binary. This binary is then used as a sidecar in the Tauri application.
For simplicity, I decided to write the server using express, but you can use any server you want.
- Clone the repo
git clone git@github.com:srsholmes/tauri-nextjs-api-routes.git. - Install dependencies using
npm installoryarnorpnpm install.
npm run dev # or yarn devnpm run tauri dev # or yarn tauri devnpm run tauri build # or yarn tauri buildnpm run package:server # or yarn package:server - The Next JS application is run using
npm run devand is available athttp://localhost:3000. - The Tauri application is run using
npm run tauri dev. The Tauri application uses thepkg-generated binary to run a local server on the user's machine. - The NextJS API routes are normally not supported by Tauri, but by running the local server as a sidecar using the binary generated by
pkg, the Tauri application is able to access the routes and their functionality. - Shared code between Tauri and NextJS is located in the
src/shareddirectory, while code for NextJS API routes and pages is located in thepages/apidirectory. - The tauri server is located in the
serverdirectory.
MIT

