Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tauri spawns multiple windows with app.create_window() in certain situations #2284

Closed
woubuc opened this issue Jul 23, 2021 · 4 comments
Closed
Labels
scope: api.js The @tauri-apps/api npm package type: bug

Comments

@woubuc
Copy link
Contributor

woubuc commented Jul 23, 2021

Describe the bug

If no window is added in the config file, but a window is created through app.create_window(), if the Javascript code in that window imports @tauri-apps/api then Tauri will incorrectly spawn a second window pointing to index.html (the default). This happens both in dev and in release builds.

To Reproduce

Steps to reproduce the behavior:

  1. Create a Tauri project with API
  2. Add a frontend page with a Javascript module that contains an import * as Tauri from '@tauri-apps/api';
  3. Remove all windows from the config file ("windows": [],)
  4. Create a window in the app setup with a label that isn't main, that loads the frontend page with the api import:
app.create_window(
    "test".into(),
    WindowUrl::App("test".into()),
    |builder, attrs| (builder, attrs)
).expect("Could not create window");
  1. Run the app
  2. It will spawn the created window and then quickly after it will spawn a second window pointing to the index page of the frontend app

I've reduced my test project to the minimal code that reproduces the error: woubuc/tauri-double-windows-repro.

Expected behavior

I expect that only 1 window opens after 1 call to app.create_window().

Platform and Versions (required):

Operating System - Windows, version 10.0.19042 X64
Webview2 - 91.0.864.71

Node.js environment
  Node.js - 14.16.0
  @tauri-apps/cli - 1.0.0-beta.6
  @tauri-apps/api - 1.0.0-beta.5

Global packages
  npm - 6.14.11
  yarn - 1.22.4

Rust environment
  rustc - 1.53.0
  cargo - 1.53.0

App directory structure
/.git
/.idea
/.nuxt
/assets
/components
/dist
/i18n
/layouts
/node_modules
/pages
/src-tauri
/static
/types

App
  tauri.rs - 1.0.0-beta.5
  build-type - bundle
  CSP - default-src blob: data: filesystem: ws: wss: http: https: tauri: 'unsa
fe-eval' 'unsafe-inline' 'self' img-src: 'self'
  distDir - ../dist
  devPath - http://localhost:3000
  framework - Vue.js (Nuxt)
  bundler - Webpack
@woubuc
Copy link
Contributor Author

woubuc commented Jul 23, 2021

The import doesn't have to happen on page load. If I remove the top-level import and instead add a button with this onclick handler..

async onClick() {
    await import('@tauri-apps/api');
}

..then when clicking the button, the second window will spawn just like with a top-level import.

Once this second window has spawned, clicking the button again (either in the original or the new window) does not spawn any additional windows.

@amrbashir
Copy link
Member

I think I know where the bug happens, will open a PR in a second.

@woubuc
Copy link
Contributor Author

woubuc commented Jul 23, 2021

I've pulled the branch with your fix and built de API package locally, and it seems to work. No more double windows! 😄

@amrbashir
Copy link
Member

Thanks for testing it. I guess we can merge it soon.

@amrbashir amrbashir added the scope: api.js The @tauri-apps/api npm package label Jul 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
scope: api.js The @tauri-apps/api npm package type: bug
Projects
None yet
Development

No branches or pull requests

2 participants