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

[bug] Unable to call window.__TAURI_INTERNALS__.invoke from within an initialization_script #12404

Closed
mattyg opened this issue Jan 15, 2025 · 6 comments · Fixed by #12424
Closed
Labels
status: needs triage This issue needs to triage, applied to new issues type: bug

Comments

@mattyg
Copy link
Contributor

mattyg commented Jan 15, 2025

Thanks for all your great work!

Describe the bug

When attempting to call window.__TAURI_INTERNALS__.invoke from within an initialization_script, I get the following error:

TypeError: undefined is not an object (evaluating 'window.__TAURI_INTERNALS__.invoke')

Reproduction

Here is a minimal reproduction: https://github.com/mattyg/bug-reproduction-tauri-initialization-script-invoke
(the relevant lines are here: https://github.com/mattyg/bug-reproduction-tauri-initialization-script-invoke/blob/f1bf03103b7ebc57b775f15f54a782ad85135f65/tauri-app/src-tauri/src/lib.rs#L10 )

You can see that calling window.__TAURI_INTERNALS__.invoke from within an initialization_script succeeds only after a short delay.

Expected behavior

I can call window.__TAURI_INTERNALS__.invoke from within an initialization_script.

Full tauri info output

[✔] Environment
    - OS: Ubuntu 22.4.0 x86_64 (X64) (i3-gnome on x11)
    ✔ webkit2gtk-4.1: 2.46.5
    ✔ rsvg2: 2.52.5
    ✔ rustc: 1.85.0-nightly (d4025ee45 2024-12-12)
    ✔ cargo: 1.85.0-nightly (20a443231 2024-12-06)
    ✔ rustup: 1.27.1 (54dd3d00f 2024-04-24)
    ✔ Rust toolchain: nightly-x86_64-unknown-linux-gnu (default)
    - node: 21.5.0
    - pnpm: 9.7.1
    - yarn: 1.22.21
    - npm: 10.2.4

[-] Packages
    - tauri 🦀: 2.2.2
    - tauri-build 🦀: 2.0.5
    - wry 🦀: 0.48.1
    - tao 🦀: 0.31.1
    - @tauri-apps/api : 2.2.0
    - @tauri-apps/cli : 2.2.4

[-] Plugins
    - tauri-plugin-opener 🦀: 2.2.4
    - @tauri-apps/plugin-opener : 2.2.4

[-] App
    - build-type: bundle
    - CSP: unset
    - frontendDist: ../build
    - devUrl: http://localhost:1420/
    - framework: Svelte
    - bundler: Vite

Stack trace

No response

Additional context

No response

@mattyg mattyg added status: needs triage This issue needs to triage, applied to new issues type: bug labels Jan 15, 2025
@FabianLars
Copy link
Member

FabianLars commented Jan 15, 2025

There's really nothing we can do here I think. The internals are also injected with the initialization script api (there's no way to inject something earlier).

@FabianLars
Copy link
Member

idk, i think i'll close this in favor of #12348

@FabianLars FabianLars closed this as not planned Won't fix, can't repro, duplicate, stale Jan 15, 2025
@mattyg
Copy link
Contributor Author

mattyg commented Jan 15, 2025

Okay thanks. I wasn't sure if the internals are injected after the user-defined initialization scripts, and it might be possible to just change the order. Or add an initialization_script_after fn to inject a script after the internals.

@FabianLars
Copy link
Member

afaik we cannot influence the order on the webview api level

@mattyg
Copy link
Contributor Author

mattyg commented Jan 16, 2025

I dug into it a bit and it looks like this is caused by the ordering of the Vec intialization_scripts in WebviewAttributes of a PendingWebview.

I was able to change the ordering so that tauri's built-in initialization scripts come before any user-added initialization scripts in that Vec. Here's my (maybe sloppy) implementation: dev...mattyg:tauri:feat/run-user-initialization-scripts-after-builtin-initialization-scripts -- it resolves the issue shown in my reproduction.

I'm happy to tidy it up a bit and open it as a PR. Would you be open to that? I don't think #12348 helps with my issue because I want to inject a script that executes after tauri's built-in scripts, but before any html is parsed.

@FabianLars
Copy link
Member

I was able to change the ordering so that tauri's built-in initialization scripts come before any user-added initialization scripts in that Vec. Here's my (maybe sloppy) implementation: dev...mattyg:tauri:feat/run-user-initialization-scripts-after-builtin-initialization-scripts -- it resolves the issue shown in my reproduction.

I highly doubt this is 100% reliable (on all platforms) but it still makes sense to do, so a pr would be appreciated.

I'm happy to tidy it up a bit and open it as a PR. Would you be open to that? I don't think #12348 helps with my issue because I want to inject a script that executes after tauri's built-in scripts, but before any html is parsed.

I assumed as much, that doesn't mean you can't use the solution in 12348 though (depending on what the solution will end up being ig)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: needs triage This issue needs to triage, applied to new issues type: bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants