Skip to content

Commit 6251645

Browse files
authored
fix(core): Update webview metadata on window close (#9360)
* fix(core): Update webview metadata on window close * make it multiwebview friendlier * support webview.close() too -> THIS IS STILL MISSING AN EVENT LIKE tauri://destroyed !!!
1 parent 4c2e747 commit 6251645

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

.changes/fix-metadata-on-close.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": patch:bug
3+
---
4+
5+
Fixes an issue causing `getAll()` to list webviews that were already destroyed.

core/tauri/src/manager/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,12 @@ impl<R: Runtime> AppManager<R> {
544544

545545
pub(crate) fn on_webview_close(&self, label: &str) {
546546
self.webview.webviews_lock().remove(label);
547+
548+
if let Ok(webview_labels_array) = serde_json::to_string(&self.webview.labels()) {
549+
let _ = self.webview.eval_script_all(format!(
550+
r#"(function () {{ const metadata = window.__TAURI_INTERNALS__.metadata; if (metadata != null) {{ metadata.webviews = {webview_labels_array}.map(function (label) {{ return {{ label: label }} }}) }} }})()"#,
551+
));
552+
}
547553
}
548554

549555
pub fn windows(&self) -> HashMap<String, Window<R>> {

core/tauri/src/manager/window.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -167,12 +167,11 @@ fn on_window_event<R: Runtime>(window: &Window<R>, event: &WindowEvent) -> crate
167167
WindowEvent::Destroyed => {
168168
window.emit_to_window(WINDOW_DESTROYED_EVENT, ())?;
169169
let label = window.label();
170-
let webviews_map = window.manager().webview.webviews_lock();
171-
let webviews = webviews_map.values();
172-
for webview in webviews {
173-
webview.eval(&format!(
174-
r#"(function () {{ const metadata = window.__TAURI_INTERNALS__.metadata; if (metadata != null) {{ metadata.windows = window.__TAURI_INTERNALS__.metadata.windows.filter(w => w.label !== "{label}"); }} }})()"#,
175-
))?;
170+
171+
if let Ok(webview_labels_array) = serde_json::to_string(&window.manager().webview.labels()) {
172+
let _ = window.manager().webview.eval_script_all(format!(
173+
r#"(function () {{ const metadata = window.__TAURI_INTERNALS__.metadata; if (metadata != null) {{ metadata.windows = window.__TAURI_INTERNALS__.metadata.windows.filter(w => w.label !== "{label}"); metadata.webviews = {webview_labels_array}.map(function (label) {{ return {{ label: label }} }}) }} }})()"#,
174+
));
176175
}
177176
}
178177
WindowEvent::Focused(focused) => window.emit_to_window(

0 commit comments

Comments
 (0)