@@ -507,7 +507,9 @@ impl<R: Runtime> WindowManager<R> {
507
507
use crate :: api:: file:: SafePathBuf ;
508
508
use tokio:: io:: { AsyncReadExt , AsyncSeekExt } ;
509
509
use url:: Position ;
510
- let asset_scope = self . state ( ) . get :: < crate :: Scopes > ( ) . asset_protocol . clone ( ) ;
510
+ let state = self . state ( ) ;
511
+ let asset_scope = state. get :: < crate :: Scopes > ( ) . asset_protocol . clone ( ) ;
512
+ let mime_type_cache = MimeTypeCache :: default ( ) ;
511
513
pending. register_uri_scheme_protocol ( "asset" , move |request| {
512
514
let parsed_path = Url :: parse ( request. uri ( ) ) ?;
513
515
let filtered_path = & parsed_path[ ..Position :: AfterPath ] ;
@@ -626,7 +628,7 @@ impl<R: Runtime> WindowManager<R> {
626
628
response = response. header ( k, v) ;
627
629
}
628
630
629
- let mime_type = MimeType :: parse ( & data, & path) ;
631
+ let mime_type = mime_type_cache . get_or_insert ( & data, & path) ;
630
632
response. mimetype ( & mime_type) . status ( status_code) . body ( data)
631
633
} else {
632
634
match crate :: async_runtime:: safe_block_on ( async move { tokio:: fs:: read ( path_) . await } ) {
@@ -1431,6 +1433,26 @@ fn request_to_path(request: &tauri_runtime::http::Request, base_url: &str) -> St
1431
1433
}
1432
1434
}
1433
1435
1436
+ // key is uri/path, value is the store mime type
1437
+ #[ cfg( protocol_asset) ]
1438
+ #[ derive( Debug , Clone , Default ) ]
1439
+ struct MimeTypeCache ( Arc < Mutex < HashMap < String , String > > > ) ;
1440
+
1441
+ #[ cfg( protocol_asset) ]
1442
+ impl MimeTypeCache {
1443
+ pub fn get_or_insert ( & self , content : & [ u8 ] , uri : & str ) -> String {
1444
+ let mut cache = self . 0 . lock ( ) . unwrap ( ) ;
1445
+ let uri = uri. to_string ( ) ;
1446
+ if let Some ( mime_type) = cache. get ( & uri) {
1447
+ mime_type. clone ( )
1448
+ } else {
1449
+ let mime_type = MimeType :: parse ( content, & uri) ;
1450
+ cache. insert ( uri, mime_type. clone ( ) ) ;
1451
+ mime_type
1452
+ }
1453
+ }
1454
+ }
1455
+
1434
1456
#[ cfg( test) ]
1435
1457
mod tests {
1436
1458
use super :: replace_with_callback;
0 commit comments