Explorar o código

fix(tauri.js) windows Edge blank screen on tauri dev (#808)

Lucas Fernandes Nogueira %!s(int64=5) %!d(string=hai) anos
pai
achega
fedee835e3
Modificáronse 3 ficheiros con 42 adicións e 12 borrados
  1. 7 0
      .changes/fix-edge-blank.md
  2. 10 0
      cli/tauri.js/src/runner.ts
  3. 25 12
      tauri-api/src/rpc.rs

+ 7 - 0
.changes/fix-edge-blank.md

@@ -0,0 +1,7 @@
+---
+"tauri.js": patch
+"tauri-api": patch
+---
+
+Fixes Edge blank screen on Windows when running `tauri dev` (Tauri crashing window due to Edge reloading app because of missing Content-Type header).
+

+ 10 - 0
cli/tauri.js/src/runner.ts

@@ -116,6 +116,16 @@ class Runner {
             writeFileSync(path.join(indexDir, 'index.html'), bodyStr)
             self.__parseHtml(cfg, indexDir, false)
               .then(({ html }) => {
+                const headers: { [key: string]: string } = {}
+                if(proxyRes.headers['content-type']) {
+                  headers['content-type'] = proxyRes.headers['content-type']
+                } else {
+                  const charsetMatch = /charset="(\S+)"/g.exec(bodyStr)
+                  if (charsetMatch) {
+                    headers['content-type'] = `'text/html; charset=${charsetMatch[1]}`
+                  }
+                }
+                res.writeHead(200, headers)
                 res.end(html)
               }).catch(err => {
                 res.writeHead(500, JSON.stringify(err))

+ 25 - 12
tauri-api/src/rpc.rs

@@ -9,7 +9,7 @@ use std::fmt::Display;
 /// use tauri_api::rpc::format_callback;
 /// // callback with a string argument
 /// let cb = format_callback("callback-function-name", "the string response");
-/// assert_eq!(cb, r#"window["callback-function-name"]("the string response")"#);
+/// assert!(cb.contains(r#"window["callback-function-name"]("the string response")"#));
 /// ```
 ///
 /// ```
@@ -23,13 +23,23 @@ use std::fmt::Display;
 /// let cb = format_callback("callback-function-name", serde_json::to_value(&MyResponse {
 ///   value: "some value".to_string()
 /// }).expect("failed to serialize"));
-/// assert_eq!(cb, r#"window["callback-function-name"]({"value":"some value"})"#);
+/// assert!(cb.contains(r#"window["callback-function-name"]({"value":"some value"})"#));
 /// ```
 pub fn format_callback<T: Into<JsonValue>, S: AsRef<str> + Display>(
   function_name: S,
   arg: T,
 ) -> String {
-  format!(r#"window["{}"]({})"#, function_name, arg.into().to_string())
+  format!(
+    r#"
+      if (window["{fn}"]) {{
+        window["{fn}"]({arg})
+      }} else {{
+        console.warn("[TAURI] Couldn't find callback id {fn} in window. This happens when the app is reloaded while Rust is running an asynchronous operation.")
+      }}
+    "#,
+    fn = function_name,
+    arg = arg.into().to_string()
+  )
 }
 
 /// Formats a Result type to its Promise response.
@@ -48,11 +58,11 @@ pub fn format_callback<T: Into<JsonValue>, S: AsRef<str> + Display>(
 /// use tauri_api::rpc::format_callback_result;
 /// let res: Result<u8, &str> = Ok(5);
 /// let cb = format_callback_result(res, "success_cb".to_string(), "error_cb".to_string()).expect("failed to format");
-/// assert_eq!(cb, r#"window["success_cb"](5)"#);
+/// assert!(cb.contains(r#"window["success_cb"](5)"#));
 ///
 /// let res: Result<&str, &str> = Err("error message here");
 /// let cb = format_callback_result(res, "success_cb".to_string(), "error_cb".to_string()).expect("failed to format");
-/// assert_eq!(cb, r#"window["error_cb"]("error message here")"#);
+/// assert!(cb.contains(r#"window["error_cb"]("error message here")"#));
 /// ```
 pub fn format_callback_result<T: Serialize, E: Serialize>(
   result: Result<T, E>,
@@ -78,7 +88,11 @@ mod test {
     if f != "" && a != "" {
       // call format callback
       let fc = format_callback(f.clone(), a.clone());
-      fc == format!(r#"window["{}"]({})"#, f, serde_json::Value::String(a))
+      fc.contains(&format!(
+        r#"window["{}"]({})"#,
+        f,
+        serde_json::Value::String(a),
+      ))
     } else {
       true
     }
@@ -94,11 +108,10 @@ mod test {
       Err(e) => (ec, e),
     };
 
-    resp
-      == format!(
-        r#"window["{}"]({})"#,
-        function,
-        serde_json::Value::String(value),
-      )
+    resp.contains(&format!(
+      r#"window["{}"]({})"#,
+      function,
+      serde_json::Value::String(value),
+    ))
   }
 }