Crates.io | tauri-plugin-cors-fetch |
lib.rs | tauri-plugin-cors-fetch |
version | |
source | src |
created_at | 2024-03-23 16:31:06.822451 |
updated_at | 2024-12-22 12:23:33.848574 |
description | Enabling Cross-Origin Resource Sharing (CORS) for Fetch Requests within Tauri applications. |
homepage | |
repository | https://github.com/idootop/tauri-plugin-cors-fetch |
max_upload_size | |
id | 1183647 |
Cargo.toml error: | TOML parse error at line 20, column 1 | 20 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
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 | ✓ |
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.
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.
# src-tauri
cargo add tauri-plugin-cors-fetch
// src-tauri/main.rs
fn main() {
tauri::Builder::default()
.plugin(tauri_plugin_cors_fetch::init())
.run(tauri::generate_context!())
.expect("failed to run app");
}
capabilities
configuration:// src-tauri/capabilities/default.json
{
"permissions": ["cors-fetch:default"]
}
withGlobalTauri
in your Tauri configuration:// src-tauri/tauri.conf.json
{
"app": {
"withGlobalTauri": true
}
}
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.enableCORSFetch(true);
// Use the hooked fetch with CORS support
fetch("https://example.com/api")
.then((response) => response.json())
.then((data) => console.log(data))
.catch((error) => console.error(error));
// Use the hooked fetch directly
window.fetchCORS("https://example.com/api");
// Use the original, unhooked fetch
window.fetchNative("https://example.com/api");
fetch
API and does not support XMLHttpRequest
(XHR) requests.This project is licensed under the MIT License.