tauri-plugin-cors-fetch 4.0.0

Enabling Cross-Origin Resource Sharing (CORS) for Fetch Requests within Tauri applications.
docs.rs failed to build tauri-plugin-cors-fetch-4.0.0
Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.
Visit the last successful build: tauri-plugin-cors-fetch-3.1.0

tauri-plugin-cors-fetch

crates.io Download MIT licensed Documentation

An unofficial Tauri plugin that enables seamless cross-origin resource sharing (CORS) for web fetch requests within Tauri applications.

Platform Supported
Linux
Windows
macOS
Android
iOS

Overview

When building cross-platform desktop applications with Tauri, we often need to access services like OpenAI that are restricted by Cross-Origin Resource Sharing (CORS) policies in web environments.

However, on the desktop, we can bypass CORS and access these services directly. While the official tauri-plugin-http can bypass CORS, it requires modifying your network requests and might not be compatible with third-party dependencies that rely on the standard fetch API.

How it Works

This plugin extends the official tauri-plugin-http by hooking into the browser's native fetch method during webpage initialization. It transparently redirects requests to the tauri-plugin-http, allowing you to use the standard fetch API without additional code changes or workarounds.

Installation

  1. Add the plugin to your Tauri project's dependencies:
# src-tauri
cargo add tauri-plugin-cors-fetch
  1. Initialize the plugin in your Tauri application setup:
// src-tauri/src/lib.rs
pub fn run() {
    tauri::Builder::default()
        .plugin(tauri_plugin_cors_fetch::init())
        .run(tauri::generate_context!())
        .expect("failed to run app");
}
  1. Add permissions in your capabilities configuration:
// src-tauri/capabilities/default.json
{
  "permissions": ["cors-fetch:default"]
}
  1. Enable withGlobalTauri in your Tauri configuration:
// src-tauri/tauri.conf.json
{
  "app": {
    "withGlobalTauri": true
  }
}

Usage

Once installed, the plugin automatically hooks into the browser's fetch API. You can use fetch normally without any code changes:

// Standard fetch - now works with CORS
fetch("https://api.example.com/data")
  .then((response) => response.json())
  .then((data) => console.log(data));

Configuration (Optional)

Configure which requests should bypass CORS:

window.CORSFetch.config({
  include: [/^https?:\/\//i], // Process all HTTP requests (default)
  exclude: ["https://api.openai.com/v1/chat/completions"], // Skip CORS bypass
});

Alternative Methods

// Direct CORS-enabled fetch
window.fetchCORS("https://api.example.com/data");

// Original native fetch (with CORS restrictions)
window.fetchNative("https://api.example.com/data");

Limitations

  • Streaming: Server-Sent Events (SSE) and streaming responses are not supported. See implementation details.
  • XHR: Only supports the modern fetch API, not XMLHttpRequest.

License

MIT License © 2024-PRESENT Del Wang