rustvitetauri

In the Tauri Release app, how can I access a file in the distDir folder?


In Tauri, I am running an HTTP server for a specific desktop app, and I displayed its content during development by loading the corresponding HTML file from the appropriate location. This HTML file will be placed in the dist folder by Vite during the build process.

The question is, how can I reference this file in the release app?

I tried something straightforward like: ./../dist/index.client.html, but it didn't work. After several attempts, I started searching online, but I only found solutions related to "resources". I haven't really used this part of Tauri before.

How can I access the distDir in the release app from Rust?

let is_release = env::var("APP_ENV").unwrap_or_else(|_| "development".to_string()) == "production";

if is_release {
    let html_path = "../dist/index.client.html"; // here
    match fs::read_to_string(html_path) {
        Ok(html_content) => {
            // Success
            Ok::<_, warp::Rejection>(warp::reply::html(html_content))
        }
        Err(_) => {
            let html_content = format!(r#"
                <html>
                    <head><title>Error</title></head>
                    <body>
                        <h1>Index file not found...</h1>
                        <p>{}</p>
                    </body>
                </html>
            "#, html_path).to_string();
            Ok::<_, warp::Rejection>(warp::reply::html(html_content))
        }
    }
}

Solution

  • There is no dist directory in a release build; the assets are embedded into the application - they don't exist in the filesystem. In order to access them, you have to use the .asset_resolver() on an app handle:

    let file = app.asset_resolver().get("index.client.html".to_owned()).unwrap();
    // use file.bytes