@@ -507,7 +507,9 @@ impl<R: Runtime> WindowManager<R> {
507507 use crate :: api:: file:: SafePathBuf ;
508508 use tokio:: io:: { AsyncReadExt , AsyncSeekExt } ;
509509 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 ( ) ;
511513 pending. register_uri_scheme_protocol ( "asset" , move |request| {
512514 let parsed_path = Url :: parse ( request. uri ( ) ) ?;
513515 let filtered_path = & parsed_path[ ..Position :: AfterPath ] ;
@@ -626,7 +628,7 @@ impl<R: Runtime> WindowManager<R> {
626628 response = response. header ( k, v) ;
627629 }
628630
629- let mime_type = MimeType :: parse ( & data, & path) ;
631+ let mime_type = mime_type_cache . get_or_insert ( & data, & path) ;
630632 response. mimetype ( & mime_type) . status ( status_code) . body ( data)
631633 } else {
632634 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
14311433 }
14321434}
14331435
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+
14341456#[ cfg( test) ]
14351457mod tests {
14361458 use super :: replace_with_callback;
0 commit comments