DEV Community

Shrijith Venkatramana
Shrijith Venkatramana

Posted on

Exploring System Webviews in Tauri: Native Rendering for Efficient Cross-Platform Apps

Hello, I'm Shrijith Venkatramana. I’m building LiveReview, a private AI code review tool that runs on your LLM key (OpenAI, Gemini, etc.) with highly competitive pricing -- built for small teams. Do check it out and give it a try!

If you're into building lightweight desktop or mobile apps with web tech, Tauri stands out by tapping into the system's built-in webview components. This keeps your apps small and fast without bundling a full browser like Chromium. We'll dig into how Tauri uses these native webviews, platform by platform, with details, examples, and code you can run. Let's get started.

What System Webviews Bring to Tauri's Design

System webviews are the OS-provided components that let apps embed and render web content like HTML, CSS, and JS. Tauri relies on them to display your app's UI, avoiding the bloat of embedded engines seen in tools like Electron.

Key points:

  • Lightweight builds: Apps can be under 1MB since no extra browser is shipped.
  • Native integration: Uses the OS's rendering for better performance and security.
  • Cross-platform consistency: Tauri abstracts differences, so your code works everywhere.

Tauri 2.x extends this to mobile, supporting Android and iOS alongside desktop. For more on versions, check the Tauri webview versions reference.

How Tauri Leverages System Webviews Overall

Tauri creates a native window and injects a webview into it, loading your web assets. The core is Rust-based, with JS for the frontend. Communication happens via a secure IPC (Inter-Process Communication) system.

Here's a comparison table of platforms and their webviews in Tauri:

Platform Webview Component Rendering Engine Key Dependency
Windows WebView2 Chromium (via Edge) Preinstalled on Win11
macOS WKWebView WebKit macOS 10.10+
Linux webkit2gtk WebKit Distro repositories
Android Android WebView Chromium Android SDK
iOS WKWebView WebKit iOS SDK

This setup ensures updates come from the OS, keeping things current without app redeploys.

Unpacking WebView2 on Windows

On Windows, Tauri uses WebView2, which is built on Microsoft Edge's Chromium engine. It auto-updates, so users get the latest features without your intervention.

Details:

  • Versions: Aligns with Edge updates, ensuring recent Chromium builds.
  • Installation: Built-in on Windows 11; for older versions (7+), Tauri's installer handles it.
  • Features: Supports hardware acceleration and modern web standards.

To see it in action, set up a basic Tauri app. First, install Tauri CLI: cargo install tauri-cli --version "^2".

Then, create a project: cargo tauri init --app-name win-example.

In src-tauri/src/main.rs:

#![cfg_attr(all(not(debug_assertions), target_os = "windows"), windows_subsystem = "windows")]

use tauri::{Builder, WindowBuilder};

fn main() {
    Builder::default()
        .setup(|app| {
            let _window = WindowBuilder::new(app, "main", tauri::WindowUrl::App("index.html".into()))
                .title("Windows WebView2 Example")
                .build()?;
            Ok(())
        })
        .run(tauri::generate_context!())
        .expect("error running Tauri");
}

// Output: Runs a window with web content from index.html, using WebView2 on Windows.
Enter fullscreen mode Exit fullscreen mode

Build with cargo tauri build. Run the executable to see the webview.

WKWebView's Role on macOS

macOS in Tauri taps into WKWebView, powered by WebKit—the same engine as Safari. It's available since macOS 10.10 and updates with the OS.

Details:

  • Versions: Tied to macOS, e.g., 616.1 on Sonoma. Check with awk '/CFBundleVersion/{getline;gsub(/<[^>]*>/,"");print}' /System/Library/Frameworks/WebKit.framework/Resources/Info.plist.
  • Dependencies: Core OS component, no extra installs.
  • Advantages: Strong security sandboxing and smooth integration with macOS features like dark mode.

For docs, see Apple's WKWebView reference.

Navigating webkit2gtk on Linux

Linux uses webkit2gtk, a WebKit-based library for GTK apps. Versions differ by distro, affecting features.

Details:

  • Versions: E.g., 2.36 on Ubuntu 22.04 (Safari 16 equivalent).
  • Dependencies: Install via package manager, like sudo apt install libwebkit2gtk-4.0-dev.
  • Notes: Check your distro for updates; older ones might lag in web standards.

Setup tip: Ensure webkit2gtk is installed before building Tauri apps.

A quick table for common distros:

Distro webkit2gtk Version Safari Equivalent
Ubuntu 22.04 2.36 16.0
Debian 11 2.36 (with updates) TP 140

Extending to Mobile: Android WebView and iOS WKWebView

Tauri 2.x brings mobile support, using Android's System WebView (Chromium-based) and iOS's WKWebView (WebKit).

Details for Android:

  • Engine: Chromium, updatable via Play Store.
  • Requirements: Android SDK tools.

Details for iOS:

  • Engine: WebKit, consistent with macOS.
  • Requirements: Xcode and iOS SDK.

Both allow access to native APIs via plugins. For mobile setup, refer to Tauri's mobile guide.

Bridging Rust and JS with Webview APIs

Tauri provides APIs for creating webviews and handling communication. Use WebviewWindow in JS or Rust to manage instances.

Key points:

  • Creation: Label webviews for reference.
  • IPC: Invoke Rust functions from JS with promises.

Example in Rust (add to main.rs):

use tauri::{command, Builder};

#[command]
fn greet(name: &str) -> String {
    format!("Hello, {}!", name)
}

fn main() {
    Builder::default()
        .invoke_handler(tauri::generate_handler![greet])
        .run(tauri::generate_context!())
        .expect("error running Tauri");
}

// In JS (e.g., app.js):
import { invoke } from '@tauri/js/api';
invoke('greet', { name: 'Dev' }).then((response) => console.log(response));  // Output: "Hello, Dev!"
Enter fullscreen mode Exit fullscreen mode

This calls Rust from JS via the webview.

Weighing Benefits Against Trade-offs in Tauri Apps

Using system webviews means smaller apps and OS-level security, but features depend on the platform's webview version—older OSes might miss modern APIs.

Benefits:

  • Size and speed: 600KB apps load fast.
  • Security: Sandboxed by the OS.

Trade-offs:

  • Consistency issues: Varying webview versions across users.
  • Debugging: Platform-specific quirks.

To mitigate, test on multiple OS versions and use polyfills for web features. Pair with plugins for native access, and always profile for performance. This approach lets you build efficient apps that scale across devices without heavy overhead. Keep experimenting!

Top comments (0)