Skip to content
Tutorials Center
My learning

Topics

Lesson 8 of 39 ยท Architecture and Setup

Frontend Frameworks and Bundlers with Tauri

Use React, Vue, Svelte, Angular or plain HTML as a Tauri frontend: dev server settings, static output, SSR limits, the @tauri-apps/api package and how to detect Tauri at runtime.

Tauri is frontend-agnostic. It needs exactly two things from your frontend: a dev server during development and a folder of static files for release. This lesson shows how to wire common tools and how to write frontend code that talks to Tauri.

The contract

flowchart LR
  subgraph dev["tauri dev"]
    A["Dev server<br/>(Vite, webpack...)"] -- "devUrl" --> B["Webview"]
  end
  subgraph rel["tauri build"]
    C["Bundler output<br/>dist/ (static files)"] -- "frontendDist, embedded" --> D["Webview via custom protocol"]
  end

In dev, the window loads a URL. In release, it loads files embedded in the binary.

That is all. Any tool that can serve files and emit a folder of static files works.

Static output only

Because the release app has no web server, your frontend must be client-side rendered or statically generated. Server-side rendering at request time does not run. Practical consequences:

  • Next.js: use output: 'export' and set images.unoptimized: true; no server components that need a server, no API routes.
  • Nuxt: use static generation (nuxt generate) or SPA mode.
  • SvelteKit: use adapter-static with ssr = false (or prerender everything).
  • Angular, Vue (Vite), React (Vite), Solid: the default SPA build already works.
  • Astro: static output works well.

Your "backend" becomes Rust commands, not API routes. If you used fetch('/api/...') before, replace those calls with invoke(...).

Vite settings

Most templates use Vite. Two settings matter, in vite.config.ts:

TypeScript
import { defineConfig } from "vite";

export default defineConfig({
  clearScreen: false,          // keep Rust compile errors visible
  server: {
    port: 1420,
    strictPort: true,          // fail instead of choosing another port
    watch: { ignored: ["**/src-tauri/**"] }, // do not reload on Rust changes
  },
});

strictPort matters because devUrl in tauri.conf.json is fixed; if Vite silently picked 1421, the window would load nothing. For mobile development you also set server.host so a phone can reach your computer, which the mobile chapter covers.

Installing the JavaScript API

Terminal
npm install @tauri-apps/api

It exposes modules you import by area:

TypeScript
import { invoke } from "@tauri-apps/api/core";
import { listen, emit } from "@tauri-apps/api/event";
import { getCurrentWindow } from "@tauri-apps/api/window";
import { WebviewWindow } from "@tauri-apps/api/webviewWindow";
import { appDataDir } from "@tauri-apps/api/path";

Plugins ship as separate packages such as @tauri-apps/plugin-dialog; install the crate and the npm package together.

Using the API in a component

React:

TSX
import { useState } from "react";
import { invoke } from "@tauri-apps/api/core";

export default function Greeter() {
  const [message, setMessage] = useState("");
  return (
    <button onClick={async () => setMessage(await invoke<string>("greet", { name: "Ada" }))}>
      {message || "Greet"}
    </button>
  );
}

The pattern is the same in Vue (call invoke in a handler and store the result in a ref), Svelte (assign the awaited value to a variable) and Angular (wrap it in a service and convert the Promise as needed). invoke returns a Promise, so remember loading and error states.

Running in a plain browser too

During frontend development you may open the page in an ordinary browser, where Tauri does not exist and invoke would throw. Guard for it:

TypeScript
const inTauri = "__TAURI_INTERNALS__" in window;

async function save(text: string) {
  if (inTauri) {
    const { invoke } = await import("@tauri-apps/api/core");
    await invoke("save_note", { text });
  } else {
    localStorage.setItem("note", text); // browser fallback
  }
}

A dynamic import() also keeps the Tauri code out of the initial bundle in the browser build. The real check is isTauri() exported from @tauri-apps/api/core in recent versions; the global marker above is what it looks at.

No bundler at all

For a tiny app you can skip Vite. Put an index.html in a folder, point frontendDist at it, and set app.withGlobalTauri to true. The API then lives on window.__TAURI__:

HTML
<script>
  const { invoke } = window.__TAURI__.core;
  invoke("greet", { name: "Ada" }).then(console.log);
</script>

Try it: a frontend that works with or without Tauri (stand-in)

This stand-in creates a fake invoke only when the real one is missing, so the same UI code runs in both places. It is the pattern many teams use to develop the UI without starting Rust.

Node.js Stand-in: a swappable invoke

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Try it: static export check (stand-in)

Server features break in a Tauri build. This stand-in scans a list of "page features" and flags the ones that need a server.

Node.js Stand-in: will this work as static files?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Practical tips

  • Use hash or memory routing if deep links break on reload; the custom protocol serves your index.html for known paths, so a history router can fail on refresh of nested routes unless you handle the fallback.
  • Disable the browser's context menu and text selection where your design calls for a native feel, and keep the scaffold's -webkit-user-select choices in mind.
  • Keep your bundle lean: the app loads from disk, so size matters less for download but still affects start time.

Continue with the Rust you will write most often in Rust basics for Tauri.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does Tauri require from a frontend for a release build?

    The files are embedded in the app and served by a custom protocol, so no server runs.
  2. 2. Why set strictPort in the Vite config?

    If Vite picked another port, the Tauri window would point at an address with nothing on it.
  3. 3. How do you replace a Next.js API route in a Tauri app?

    There is no request-time server, so server logic moves to Rust commands.
  4. 4. What does a dynamic import of @tauri-apps/api help with?

    You only load Tauri code when you are really inside Tauri, which keeps browser development working.

Summary

  • Tauri needs a dev server URL for development and static files for release.
  • Server rendering and API routes do not run; replace them with Rust commands.
  • Configure Vite with a fixed port, strictPort and clearScreen: false.
  • Import the API from @tauri-apps/api/* and plugin packages separately.
  • Guard Tauri calls so the UI can also run in a normal browser.

Next: Rust basics for Tauri