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 [email protected]:srsholmes/tauri-nextjs-api-routes.git
. - Install dependencies using
npm install
oryarn
orpnpm install
.
npm run dev # or yarn dev
npm run tauri dev # or yarn tauri dev
npm run tauri build # or yarn tauri build
npm run package:server # or yarn package:server
- The Next JS application is run using
npm run dev
and 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/shared
directory, while code for NextJS API routes and pages is located in thepages/api
directory. - The tauri server is located in the
server
directory.
MIT