Quellcode durchsuchen

fix(api): do not throw an exception if __TAURI_METADATA__ is not set, fixes #3554 (#3572)

Lucas Fernandes Nogueira vor 3 Jahren
Ursprung
Commit
9cb1059aa3
3 geänderte Dateien mit 31 neuen und 15 gelöschten Zeilen
  1. 5 0
      .changes/api-app-window-browser.md
  2. 0 0
      core/tauri/scripts/bundle.js
  3. 26 15
      tooling/api/src/window.ts

+ 5 - 0
.changes/api-app-window-browser.md

@@ -0,0 +1,5 @@
+---
+"api": patch
+---
+
+Do not crash if `__TAURI_METADATA__` is not set, log an error instead.

Datei-Diff unterdrückt, da er zu groß ist
+ 0 - 0
core/tauri/scripts/bundle.js


+ 26 - 15
tooling/api/src/window.ts

@@ -879,12 +879,12 @@ class WindowManager extends WebviewWindowHandle {
             type: 'setMinSize',
             payload: size
               ? {
-                  type: size.type,
-                  data: {
-                    width: size.width,
-                    height: size.height
-                  }
+                type: size.type,
+                data: {
+                  width: size.width,
+                  height: size.height
                 }
+              }
               : null
           }
         }
@@ -921,12 +921,12 @@ class WindowManager extends WebviewWindowHandle {
             type: 'setMaxSize',
             payload: size
               ? {
-                  type: size.type,
-                  data: {
-                    width: size.width,
-                    height: size.height
-                  }
+                type: size.type,
+                data: {
+                  width: size.width,
+                  height: size.height
                 }
+              }
               : null
           }
         }
@@ -1157,13 +1157,24 @@ class WebviewWindow extends WindowManager {
 }
 
 /** The WebviewWindow for the current window. */
-const appWindow = new WebviewWindow(
-  window.__TAURI_METADATA__.__currentWindow.label,
-  {
+let appWindow
+if ('__TAURI_METADATA__' in window) {
+  appWindow = new WebviewWindow(
+    window.__TAURI_METADATA__.__currentWindow.label,
+    {
+      // @ts-expect-error
+      skip: true
+    }
+  )
+} else {
+  console.warn(
+    `Could not find "window.__TAURI_METADATA__". The "appWindow" value will reference the "main" window label.\nNote that this is not an issue if running this frontend on a browser instead of a Tauri window.`
+  )
+  appWindow = new WebviewWindow('main', {
     // @ts-expect-error
     skip: true
-  }
-)
+  })
+}
 
 /** Configuration for the window to create. */
 interface WindowOptions {

Einige Dateien werden nicht angezeigt, da zu viele Dateien in diesem Diff geändert wurden.