An unofficial Tauri plugin that enables seamless cross-origin resource sharing (CORS) for web fetch requests within Tauri applications.
Motivation
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 forks the official tauri-plugin-http plugin. During webpage initialization, it hooks the browser's native fetch
method and redirects requests to tauri-plugin-http, allowing you to continue using the standard fetch
API without the need for 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/main.rs
- Add permissions in your
capabilities
configuration:
// src-tauri/capabilities/default.json
- Enable
withGlobalTauri
in your Tauri configuration:
// src-tauri/tauri.conf.json
Usage
After installing and initializing the plugin, you can start making fetch
requests from your Tauri application without encountering CORS-related errors.
// Enable CORS for the hooked fetch globally (default is true on app start)
window.;
// Use the hooked fetch with CORS support
.
.
.;
// Use the hooked fetch directly
window.;
// Use the original, unhooked fetch
window.;
Limitation
- No Custom CSP Policy Support: By default, all HTTP/HTTPS requests will be redirected to local native requests.
- No XMLHttpRequest Support: The plugin is designed specifically to work with the modern
fetch
API and does not supportXMLHttpRequest
(XHR) requests.
License
This project is licensed under the MIT License.