Bläddra i källkod

Don't strip the prefix from custom assets

Jonathan Kelley 1 år sedan
förälder
incheckning
e9978c119f
2 ändrade filer med 2 tillägg och 31 borttagningar
  1. 2 4
      examples/dynamic_asset.rs
  2. 0 27
      packages/desktop/src/protocol.rs

+ 2 - 4
examples/dynamic_asset.rs

@@ -7,10 +7,8 @@ fn main() {
 
 fn app() -> Element {
     use_asset_handler("logos", |request, response| {
-        // Note that the "logos" prefix is stripped from the URI
-        //
-        // However, the asset is absolute to its "virtual folder" - meaning it starts with a leading slash
-        if request.uri().path() != "/logo.png" {
+        // We get the original path - make sure you handle that!
+        if request.uri().path() != "/logos/logo.png" {
             return;
         }
 

+ 0 - 27
packages/desktop/src/protocol.rs

@@ -124,13 +124,6 @@ pub(super) fn desktop_handler(
 
     if let Some(name) = name.to_str() {
         if asset_handlers.has_handler(name) {
-            // Trim the leading path from the URI
-            //
-            // I hope this is reliable!
-            //
-            // so a request for /assets/logos/logo.png?query=123 will become /logos/logo.png?query=123
-            strip_uri_prefix(&mut request, name);
-
             return asset_handlers.handle_request(name, request, responder);
         }
     }
@@ -162,26 +155,6 @@ fn serve_from_fs(path: PathBuf) -> Result<Response<Vec<u8>>> {
         .body(std::fs::read(asset)?)?)
 }
 
-fn strip_uri_prefix(request: &mut Request<Vec<u8>>, name: &str) {
-    // trim the leading path
-    if let Some(path) = request.uri().path_and_query() {
-        let new_path = path
-            .path()
-            .trim_start_matches('/')
-            .strip_prefix(name)
-            .expect("expected path to have prefix");
-
-        let new_uri = Uri::builder()
-            .scheme(request.uri().scheme_str().unwrap_or("http"))
-            .path_and_query(format!("{}{}", new_path, path.query().unwrap_or("")))
-            .authority("index.html")
-            .build()
-            .expect("failed to build new URI");
-
-        *request.uri_mut() = new_uri;
-    }
-}
-
 /// Construct the inline script that boots up the page and bridges the webview with rust code.
 ///
 /// The arguments here: