Skip to content

Commit

Permalink
fix(tauri.js) windows Edge blank screen on tauri dev (#808)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed Jul 11, 2020
1 parent 0afbc49 commit fedee83
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 12 deletions.
7 changes: 7 additions & 0 deletions .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 changes: 10 additions & 0 deletions cli/tauri.js/src/runner.ts
Expand Up @@ -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))
Expand Down
37 changes: 25 additions & 12 deletions tauri-api/src/rpc.rs
Expand Up @@ -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")"#));
/// ```
///
/// ```
Expand All @@ -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.
Expand All @@ -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>,
Expand All @@ -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
}
Expand All @@ -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),
))
}
}

0 comments on commit fedee83

Please sign in to comment.