Skip to content

Commit fedee83

Browse files
authored
fix(tauri.js) windows Edge blank screen on tauri dev (#808)
1 parent 0afbc49 commit fedee83

File tree

3 files changed

+42
-12
lines changed

3 files changed

+42
-12
lines changed

.changes/fix-edge-blank.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"tauri.js": patch
3+
"tauri-api": patch
4+
---
5+
6+
Fixes Edge blank screen on Windows when running `tauri dev` (Tauri crashing window due to Edge reloading app because of missing Content-Type header).
7+

cli/tauri.js/src/runner.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,16 @@ class Runner {
116116
writeFileSync(path.join(indexDir, 'index.html'), bodyStr)
117117
self.__parseHtml(cfg, indexDir, false)
118118
.then(({ html }) => {
119+
const headers: { [key: string]: string } = {}
120+
if(proxyRes.headers['content-type']) {
121+
headers['content-type'] = proxyRes.headers['content-type']
122+
} else {
123+
const charsetMatch = /charset="(\S+)"/g.exec(bodyStr)
124+
if (charsetMatch) {
125+
headers['content-type'] = `'text/html; charset=${charsetMatch[1]}`
126+
}
127+
}
128+
res.writeHead(200, headers)
119129
res.end(html)
120130
}).catch(err => {
121131
res.writeHead(500, JSON.stringify(err))

tauri-api/src/rpc.rs

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::fmt::Display;
99
/// use tauri_api::rpc::format_callback;
1010
/// // callback with a string argument
1111
/// let cb = format_callback("callback-function-name", "the string response");
12-
/// assert_eq!(cb, r#"window["callback-function-name"]("the string response")"#);
12+
/// assert!(cb.contains(r#"window["callback-function-name"]("the string response")"#));
1313
/// ```
1414
///
1515
/// ```
@@ -23,13 +23,23 @@ use std::fmt::Display;
2323
/// let cb = format_callback("callback-function-name", serde_json::to_value(&MyResponse {
2424
/// value: "some value".to_string()
2525
/// }).expect("failed to serialize"));
26-
/// assert_eq!(cb, r#"window["callback-function-name"]({"value":"some value"})"#);
26+
/// assert!(cb.contains(r#"window["callback-function-name"]({"value":"some value"})"#));
2727
/// ```
2828
pub fn format_callback<T: Into<JsonValue>, S: AsRef<str> + Display>(
2929
function_name: S,
3030
arg: T,
3131
) -> String {
32-
format!(r#"window["{}"]({})"#, function_name, arg.into().to_string())
32+
format!(
33+
r#"
34+
if (window["{fn}"]) {{
35+
window["{fn}"]({arg})
36+
}} else {{
37+
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.")
38+
}}
39+
"#,
40+
fn = function_name,
41+
arg = arg.into().to_string()
42+
)
3343
}
3444

3545
/// Formats a Result type to its Promise response.
@@ -48,11 +58,11 @@ pub fn format_callback<T: Into<JsonValue>, S: AsRef<str> + Display>(
4858
/// use tauri_api::rpc::format_callback_result;
4959
/// let res: Result<u8, &str> = Ok(5);
5060
/// let cb = format_callback_result(res, "success_cb".to_string(), "error_cb".to_string()).expect("failed to format");
51-
/// assert_eq!(cb, r#"window["success_cb"](5)"#);
61+
/// assert!(cb.contains(r#"window["success_cb"](5)"#));
5262
///
5363
/// let res: Result<&str, &str> = Err("error message here");
5464
/// let cb = format_callback_result(res, "success_cb".to_string(), "error_cb".to_string()).expect("failed to format");
55-
/// assert_eq!(cb, r#"window["error_cb"]("error message here")"#);
65+
/// assert!(cb.contains(r#"window["error_cb"]("error message here")"#));
5666
/// ```
5767
pub fn format_callback_result<T: Serialize, E: Serialize>(
5868
result: Result<T, E>,
@@ -78,7 +88,11 @@ mod test {
7888
if f != "" && a != "" {
7989
// call format callback
8090
let fc = format_callback(f.clone(), a.clone());
81-
fc == format!(r#"window["{}"]({})"#, f, serde_json::Value::String(a))
91+
fc.contains(&format!(
92+
r#"window["{}"]({})"#,
93+
f,
94+
serde_json::Value::String(a),
95+
))
8296
} else {
8397
true
8498
}
@@ -94,11 +108,10 @@ mod test {
94108
Err(e) => (ec, e),
95109
};
96110

97-
resp
98-
== format!(
99-
r#"window["{}"]({})"#,
100-
function,
101-
serde_json::Value::String(value),
102-
)
111+
resp.contains(&format!(
112+
r#"window["{}"]({})"#,
113+
function,
114+
serde_json::Value::String(value),
115+
))
103116
}
104117
}

0 commit comments

Comments
 (0)