From 56757b5e83474bfc6c2aaf3543ad2cc58d8ceae5 Mon Sep 17 00:00:00 2001 From: Noah Klayman Date: Sat, 30 Jan 2021 18:31:41 -0800 Subject: [PATCH] fix(tauri/asset): better no-server image loading (#1170) --- cli/core/src/templates/tauri.js | 6 +----- tauri/src/endpoints/asset.rs | 14 +++++++++++--- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/cli/core/src/templates/tauri.js b/cli/core/src/templates/tauri.js index 598c340ca..ecc3a6619 100644 --- a/cli/core/src/templates/tauri.js +++ b/cli/core/src/templates/tauri.js @@ -141,11 +141,7 @@ if (!String.prototype.startsWith) { function (e) { var target = e.target while (target != null) { - if ( - target.matches - ? target.matches('img') - : target.msMatchesSelector('img') - ) { + if (target.matches('img')) { window.__TAURI__.loadAsset(target.src, 'image').then(function (img) { target.src = img }) diff --git a/tauri/src/endpoints/asset.rs b/tauri/src/endpoints/asset.rs index b1d54f32f..120c2a520 100644 --- a/tauri/src/endpoints/asset.rs +++ b/tauri/src/endpoints/asset.rs @@ -46,16 +46,24 @@ pub fn load( } if asset_type == "image" { - let ext = if asset.ends_with("gif") { + let mime_type = if asset.ends_with("gif") { "gif" + } else if asset.ends_with("apng") { + "apng" } else if asset.ends_with("png") { "png" + } else if asset.ends_with("avif") { + "avif" + } else if asset.ends_with("webp") { + "webp" + } else if asset.ends_with("svg") { + "svg+xml" } else { "jpeg" }; Ok(format!( - r#""data:image/{};base64,{}""#, - ext, + r#"data:image/{};base64,{}"#, + mime_type, base64::encode(&read_asset.expect("Failed to read asset type").into_owned()) )) } else {