The "Not allowed to load local resource" error in Tauri typically occurs when the webview in Tauri is trying to load a local resource (file) from the user's file system, but it's blocked due to security restrictions. Tauri enforces strict security measures by default to prevent any potential security vulnerabilities.
To resolve this issue, you have a few options depending on your use case:
Allow Local File Access (Not Recommended): Tauri restricts access to local files by default to ensure security. However, if you have a valid use case for loading local resources, you can allow it by configuring Tauri to disable webview security. Keep in mind that this approach is not recommended for production use as it poses security risks.
To disable webview security, you can add the following configuration in your
tauri.conf.json
file:json{ "tauri": { "window": { "webview": { "security": "none" } } } }
Again, use this approach with caution and only for development or testing purposes.
Serve Files via Tauri API: Instead of directly loading local files in the webview, you can use the Tauri API to serve those files and then access them from your webview. This way, you can bypass the security restrictions while still keeping control over which files are accessible.
In your Tauri main process, use the
serve
function to expose the local files:rust#[tauri::command] async fn serve_file(path: String) -> Result<String, String> { let content = tokio::fs::read_to_string(&path).await.map_err(|err| err.to_string())?; Ok(content) }
And in your JavaScript code, call this function to fetch the file content:
javascript// Make sure you have set the proper Tauri API endpoint for your serve_file function const fileContent = await window.__TAURI__.invoke('serve_file', 'path/to/your/file.txt');
This way, you can control what files are accessible to the webview while still serving them through the Tauri API.
Always keep security in mind when dealing with local file access or disabling webview security. Loading local files directly in the webview can expose your application to various security risks. It's essential to consider alternative approaches that provide the necessary functionality while maintaining the security of your application.