plugins-workspace icon indicating copy to clipboard operation
plugins-workspace copied to clipboard

handling of websocket on webview reload

Open rtmotiondev opened this issue 1 year ago • 3 comments

What's the recommended way to handle the websocket when reloading the webview? If we don't disconnect the websocket we get a lot of error messages similar to the one below:

[Error] TypeError: window['_' + 1465076431] is not a function. (In 'window['_' + 1465076431]({ message: response, id: 8 })', 'window['_' + 1465076431]' is undefined) — 127.0.0.1:1:162
	error

We tried to disconnect the socket during the unload event but it doesn't work. Fetch API cannot load ipc://localhost/plugin%3Awebsocket%7Csend due to access control checks.

beforeunload event doesn't trigger.

> tauri info


[✔] Environment
    - OS: Mac OS 14.5.0 X64
    ✔ Xcode Command Line Tools: installed
    ✔ rustc: 1.77.2 (25ef9e3d8 2024-04-09)
    ✔ cargo: 1.77.2 (e52e36006 2024-03-26)
    ✔ rustup: 1.27.1 (54dd3d00f 2024-04-24)
    ✔ Rust toolchain: 1.77.2-aarch64-apple-darwin (default)
    - node: 20.11.0
    - npm: 10.2.4

[-] Packages
    - tauri [RUST]: 2.0.0-beta.22
    - tauri-build [RUST]: 2.0.0-beta.17
    - wry [RUST]: 0.40.1
    - tao [RUST]: 0.28.1
    - @tauri-apps/api [NPM]: 2.0.0-beta.13
    - @tauri-apps/cli [NPM]: 2.0.0-beta.20

[-] App
    - build-type: bundle
    - CSP: unset
    - frontendDist: ../src
    - bundler: Webpack

Permissions

{
  "identifier": "main-capability",
  "description": "Capability for the main window",
  "local": true,
  "windows": [
    "main"
  ],
  "permissions": [
    "store:allow-get",
    "store:allow-set",
    "store:allow-save",
    "store:allow-load",
    "path:default",
    "event:default",
    "window:default",
    "app:default",
    "resources:default",
    "menu:default",
    "dialog:default",
    "fs:default",
    "log:default",
    "websocket:default",
    "window-state:default",
    "window-state:allow-restore-state",
    "window:allow-show"
  ]
}

rtmotiondev avatar Jun 30 '24 18:06 rtmotiondev

unload is not really recommended but the same issue applies to visibilitychange event

rtmotiondev avatar Jun 30 '24 18:06 rtmotiondev

Is there any update to this? I'm getting the same issue when reloading the page. Infinite errors until I restart the application. We need a way to clean up the websockets when the page is reloaded.

997R8V10 avatar Feb 07 '25 03:02 997R8V10

A similar issue we're having (https://github.com/deltachat/deltachat-desktop/issues/3321) is that, at least on Windows (WebView2), when you close a webview window, visibilitychange, beforeunload, pagehide (but not unload) events do get fired (which you can track with localStorage.set()), but invoking a command from inside of them doesn't work, because, I assume, the respective IPC HTTP request gets interrupted.

Electron also has this problem, but it's only present inside of <iframe>s.

  • https://github.com/electron/electron/issues/4366
  • https://github.com/electron/electron/issues/22845
  • https://stackoverflow.com/questions/36091378/closing-application-and-notifying-renderer-process

The thing that HTTP requests do not finish when a page gets closed is sort of expected web behavior, and there is even a thing to work around this: navigator.sendBeacon(). The question is whether Tauri commands should promise to work inside of these event listeners.

I believe there is the https://tauri.app/reference/javascript/api/namespacewindow/#closerequestedevent event, and you can invoke commands from inside of there, according to this https://github.com/tauri-apps/tauri/discussions/4963#discussioncomment-3413270, but in our case we don't want to rely on Tauri-specific APIs.

WofWca avatar May 01 '25 10:05 WofWca