
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
- Add the plugin to your Tauri project's dependencies:
# src-tauri
cargo add tauri-plugin-cors-fetch
- Initialize the plugin in your Tauri application setup:
// src-tauri/src/lib.rs
- Add permissions in your
capabilitiesconfiguration:
// src-tauri/capabilities/default.json
- Enable
withGlobalTauriin your Tauri configuration:
// src-tauri/tauri.conf.json
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
.
.;
Configuration (Optional)
Configure which requests should bypass CORS and set default request parameters:
window.;
Configuration Options:
-
include: Array of URL patterns (strings or RegExp) that should use CORS bypass -
exclude: Array of URL patterns that should NOT use CORS bypass -
request: Default request parameters (applied to all CORS requests) object containing:connectTimeout: Default connection timeout (ms)maxRedirections: Default maximum redirections to followproxy: Default proxy configuration for all requests (see Tauri HTTP proxy docs)
These default request parameters will be used for all CORS requests unless overridden in individual fetch calls.
Alternative Methods
// Direct CORS-enabled fetch
window.;
// Original native fetch (with CORS restrictions)
window.;
Limitations
- Streaming: Server-Sent Events (SSE) and streaming responses are not supported. See implementation details.
- XHR: Only supports the modern
fetchAPI, notXMLHttpRequest.
License
MIT License © 2024-PRESENT Del Wang