#[Tauri v2] Deep link

56 messages · Page 1 of 1 (latest)

ocean condor
#

I kinda made a deep link work on macos, even though it never worked with code from docs, and was very tricky to make it work. I am stucked on linux version. Not sure why clear tauri project works fine, but if copied code to migrated from v1 project it just doesnt work.

The main problem is that i register a schema

  "plugins": {
    "deep-link": {
      "desktop": {
        "schemes": ["test"]
      }
    }
  },

then checking in a js, await isRegistered('test') which returns true.
lib.rs has all setuped as in docs

 tauri::Builder::default()
        .plugin(tauri_plugin_single_instance::init(|app, argv, _cwd| {
            let _ = app.get_webview_window("main")
                .expect("no main window")
                .set_focus();

          println!("a new app instance was opened with {argv:?} and the deep link event was already triggered");
        }))
        .plugin(tauri_plugin_deep_link::init())
        .plugin(tauri_plugin_global_shortcut::Builder::new().build())
        .plugin(tauri_plugin_http::init())
        .plugin(tauri_plugin_fs::init())
        .plugin(tauri_plugin_os::init())
        .plugin(tauri_plugin_dialog::init())
        .plugin(tauri_plugin_process::init())
        .plugin(tauri_plugin_shell::init())
        .setup(|app| {
            #[cfg(any(windows, target_os = "linux"))]
            {
                use tauri_plugin_deep_link::DeepLinkExt;
                app.deep_link().register_all()?;
            }

            Ok(())            
        })
        .plugin(tauri_plugin_websocket::init())

and it just returns error when i am trying to do a xdg-open test://something in terminal gio: test://something: The specified location is not supported all the time.
When i do same code but in clear project it works fine. It is Ubuntu 24.
I even try to build a deb package and install, then it will open an app, but url will have only path to app which was opened, and it never fires onOpenUrl nor other listeners.

slate garden
#

any updates on this? im working through it too. you said in a clear project it works fine?

#

i got it to get past the ``` The specified location is not supported

#

but now its just saying that

xdg-open myapp://url
error: unexpected argument 'myapp://url' found
#

and its not triggering in the js either?

await onOpenUrl((urls) => {
  console.log('deep link:', urls);
});

nothing

#

i have this

tauri = tauri
        .plugin(tauri_plugin_deep_link::init())
        .plugin(tauri_plugin_process::init())
        .setup(move |app| {
            #[cfg(any(windows, target_os = "linux"))]
            {
                println!("register deep links");
                use tauri_plugin_deep_link::DeepLinkExt;
                app.deep_link().register_all()?;
            }

            app.deep_link().on_open_url(|event| {
                println!("deep link URLs: {:?}", event.urls());
            });

slate garden
#

so its like running the xdg-open myapp://url just runs the app and gives the whole myapp://url to my cli... ? the js is registered says true but the js and rust are not triggered when i run the xdg-open

slate garden
#

ok so reading the docs again i found that the deep links are passed as cli arguments. so i had parse them better so my cli didnt complain. now the deep links are going in but stil have 2 problems. 1. its opening a new app. 2. even when the app is closed its not triggering the js or the rust deep link println!

slate garden
#

ok so on mac it seams to be working better. its triggering the js onOpenUrl

ocean condor
#

No progress, still stuck as linux doesnt register a scheme 😦

azure tapir
#
  1. Is also expected, and should be documented in the onOpenUrl inline docs. You'll need to call getCurrent to get the scheme that started the app
azure tapir
ocean condor
#

It looks like i get through this issue.
I have no idea why its working correctly on clear app and not working on my.
Solution that i found :

  1. Run app once with yarn tauri dev -> this will register a deep link i suppose (if i try to run xdg-open test://something its not working saying gio: test://something: The specified location is not supported)
  2. Stop the app dev proccess
  3. Start yarn tauri dev again. Now its registered.

I spend around 4 days to find the problem with this, just bc on clear app it works right away without restarting...

ocean condor
# azure tapir can you check that the file path in `~/.local/share/applications/yourapp.desktop...

File has a lot of registered schemes, is this okay?

MimeType=x-scheme-handler/test;x-scheme-handler/test;x-scheme-handler/test;x-scheme-handler/test;x-scheme-handler/test;x-scheme-handler/test;x-scheme-handler/test;x-scheme-handler/test;x-scheme-handler/test;x-scheme-handler/test;x-scheme-handler/test;x-scheme-handler/test;x-scheme-handler/test;x-scheme-handler/test;x-scheme-handler/test;x-scheme-handler/test;x-scheme-handler/test;x-scheme-handler/test;x-scheme-handler/test;x-scheme-handler/test;x-scheme-handler/test;x-scheme-handler/test;x-scheme-handler/test;x-scheme-handler/test;x-scheme-handler/test;x-scheme-handler/test;x-scheme-handler/test;x-scheme-handler/test;x-scheme-handler/test;x-scheme-handler/test;x-scheme-handler/test;x-scheme-handler/test;x-scheme-handler/test;x-scheme-handler/test;x-scheme-handler/test;x-scheme-handler/test;x-scheme-handler/test;x-scheme-handler/test;x-scheme-handler/test;x-scheme-handler/test;x-scheme-handler/test;x-scheme-handler/test;x-scheme-handler/test;x-scheme-handler/test;x-scheme-handler/test;x-scheme-handler/test;x-scheme-handler/test;x-scheme-handler/test;x-scheme-handler/test;x-scheme-handler/test;x-scheme-handler/test;x-scheme-handler/test;x-scheme-handler/test;x-scheme-handler/test;x-scheme-handler/test;x-scheme-handler/test;x-scheme-handler/test;x-scheme-handler/test;x-scheme-handler/test;x-scheme-handler/test;x-scheme-handler/test;x-scheme-handler/test;
azure tapir
ocean condor
#

And i just checked, for dev version it works okay and
listen<string[]>('deep-link://new-url', event => {}) is being called, but for production app, it looks like none of the listeners i can put on is never called nor rust listeners too.

#
await onOpenUrl(url => {
  debugger
  console.log(url)
})
or
listen<string[]>('deep-link://new-url', event => {})

never fires, even though deep link is working fine.

azure tapir
#

is the app already running when you call the link, or does the link also open the app itself?

slate garden
# azure tapir Isn't 1) also in the docs? This is just how deep links work on windows and linux...

i didnt mention it but i am using the single instance plugin

 let mut tauri = tauri::Builder::default();

    #[cfg(desktop)]
    {
         tauri = tauri.plugin(tauri_plugin_single_instance::init(|_app, argv, _cwd| {
          println!("a new app instance was opened with {argv:?} and the deep link event was already triggered");
          // when defining deep link schemes at runtime, you must also check `argv` here
        }));
    }
    tauri = tauri
        .plugin(tauri_plugin_deep_link::init())
        .plugin(tauri_plugin_process::init())
        .setup(move |app| {
            #[cfg(any(windows, target_os = "linux"))]
            {
                println!("register deep links");
                use tauri_plugin_deep_link::DeepLinkExt;
                app.deep_link().register_all()?;
            }

            app.deep_link().on_open_url(|event| {
                println!("deep link URLs: {:?}", event.urls());
            });

i just opens a new instance on linux.

#

it works on mac when the app is already running.

ocean condor
azure tapir
azure tapir
slate garden
# azure tapir hmm that's weird, so the single instance plugin isn't working. Does it work if y...

yeah single instance plugin not working on linux.
if i start the app manually and then do an xdg-open myapp://url
it just tries to open a new version. even after i start the app again.
another thing that makes me think the single instance is messed up is that when the app is closed and i do an xdg-open it opens the app but it doesnot print the ``` tauri = tauri.plugin(tauri_plugin_single_instance::init(|_app, argv, _cwd| {
println!("a new app instance was opened with {argv:?} and the deep link event was already triggered");

    }));```
azure tapir
#

another thing that makes me think the single instance is messed up is that when the app is closed and i do an xdg-open it opens the app but it doesnot print the
that part at least is how it's supposed to work. The single-instance callback is only supposed to run if secondary instances are opened (and killed)

slate garden
#

ok that part makes sense

#

to see if the link is coming in when the app is closed i think i need to use that getCurrent to get the scheme that started the app.

#

but the confusing part now is still why the single instance is not working.

#

and whatabout when the app is closed and the xdg-open fires should the ``` app.deep_link().on_open_url(|event| {
println!("deep link URLs: {:?}", event.urls());
});

ocean condor
# azure tapir did you enable the deep-link feature flag on the single-instance dependency in c...

i did all by the docs. And this is how it looks like

[build-dependencies]
tauri-build = { version = "2", features = [] }

[dependencies]
tauri = { version = "2", features = [ "protocol-asset", "image-png", "tray-icon"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
tauri-plugin-websocket = "2"
tauri-plugin-deep-link = "2.0.2"
tokio = "1.36.0"
open = "5.1.3"
tauri-plugin-shell = "2"
tauri-plugin-process = "2"
tauri-plugin-dialog = "2.0.4"
tauri-plugin-os = "2"
tauri-plugin-fs = "2"
tauri-plugin-http = "2.0.4"

[target."cfg(any(target_os = \"macos\", windows, target_os = \"linux\"))".dependencies]
tauri-plugin-single-instance = { version = "2.0.2", features = ["deep-link"] }
#

and for lib.rs

    tauri::Builder::default()
        .plugin(tauri_plugin_single_instance::init(|app, argv, _cwd| {
            let _ = app.get_webview_window("main")
                .expect("no main window")
                .set_focus();

          println!("a new app instance was opened with {argv:?} and the deep link event was already triggered");
        }))
        .plugin(tauri_plugin_deep_link::init())
        .plugin(tauri_plugin_http::init())
        .plugin(tauri_plugin_fs::init())
        .plugin(tauri_plugin_os::init())
        .plugin(tauri_plugin_dialog::init())
        .plugin(tauri_plugin_process::init())
        .plugin(tauri_plugin_shell::init())
        .setup(|app| {
            #[cfg(any(windows, target_os = "linux"))]
            {
                use tauri_plugin_deep_link::DeepLinkExt;
                app.deep_link().register_all()?;
            }

            Ok(())            
        })
#

And again, on totally clean app with cargo.toml

[package]
name = "test"
version = "0.1.0"
description = "A Tauri App"
authors = ["you"]
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[lib]
# The `_lib` suffix may seem redundant but it is necessary
# to make the lib name unique and wouldn't conflict with the bin name.
# This seems to be only an issue on Windows, see https://github.com/rust-lang/cargo/issues/8519
name = "test_lib"
crate-type = ["staticlib", "cdylib", "rlib"]

[build-dependencies]
tauri-build = { version = "2", features = [] }

[dependencies]
tauri = { version = "2", features = [] }
tauri-plugin-shell = "2"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
tauri-plugin-deep-link = "2"

[target."cfg(any(target_os = \"macos\", windows, target_os = \"linux\"))".dependencies]
tauri-plugin-single-instance = { version = "2.0.0", features = ["deep-link"] }
#

and lib.rs

use tauri::Manager;

// Learn more about Tauri commands at https://tauri.app/develop/calling-rust/
#[tauri::command]
fn greet(name: &str) -> String {
    format!("Hello, {}! You've been greeted from Rust!", name)
}

#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
    tauri::Builder::default()
        .plugin(tauri_plugin_single_instance::init(|app, argv, _cwd| {
             let _ = app.get_webview_window("main")
                .expect("no main window")
                .set_focus();
          println!("a new app instance was opened with {argv:?} and the deep link event was already triggered");
          // when defining deep link schemes at runtime, you must also check `argv` here
        }))
        .plugin(tauri_plugin_deep_link::init())
        .plugin(tauri_plugin_shell::init())
        .setup(|app| {
            #[cfg(any(windows, target_os = "linux"))]
            {
            use tauri_plugin_deep_link::DeepLinkExt;
               let _= app.deep_link().register_all()?;
            }

            Ok(())
        })
        .invoke_handler(tauri::generate_handler![greet])
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}
#

it works totally fine and onOpenUrl works just fine

azure tapir
ocean condor
#

Unfortunatelly not...

#

Could it be that migration cli from v1-v2 make some changes that broke functionality?

azure tapir
#

never say never but i don't see what could cause this.

#

can you also share your tauri.conf.json file? (you can remove the app name and identifier and whatever to keep it private)

ocean condor
#

Sure

{
  "$schema": "https://schema.tauri.app/config/2",
  "build": {
    "beforeDevCommand": "yarn dev",
    "beforeBuildCommand": "yarn build",
    "frontendDist": "../dist",
    "devUrl": "http://localhost:1420"
  },
  "bundle": {
    "active": true,
    "targets": "all",
    "resources": {
      "../service/.env.development": "./",
      "../service/.env.staging": "./",
      "../service/.env": "./"
    },
    "icon": ["icons/32x32.png", "icons/128x128.png", "icons/[email protected]", "icons/icon.icns", "icons/icon.ico"]
  },
  "productName": "Test",
  "mainBinaryName": "Test",
  "version": "../package.json",
  "identifier": "ui.test.desktop",
  "plugins": {
    "deep-link": {
      "desktop": {
        "schemes": ["something"]
      }
    }
  },
  "app": {
    "windows": [
      {
        "fullscreen": false,
        "resizable": true,
        "title": "Test",
        "width": 1440,
        "height": 900,
        "minHeight": 460,
        "minWidth": 600
      }
    ],
    "withGlobalTauri": false,
    "security": {
      "assetProtocol": {
        "scope": ["$CACHE/test/**"],
        "enable": true
      },
      "csp": "style-src 'unsafe-inline' 'self'; default-src 'self'; img-src 'self' data: https: asset: http://asset.localhost; connect-src ipc: http://ipc.localhost https://plausible.io/api/event"
    }
  }
}

this is main one

#

and this is linux.conf.json

{
  "bundle": {
    "resources": {
      "../service/test": "./",
      "../service/testt": "./",
      "../service/testtt": "./",
      "../service/testttt": "./",
      "../pkgScripts/lin/test.service": "./"
    },
    "linux": {
      "deb": {
        "files": {
          "../control/postinst": "../pkgScripts/lin/postinst",
          "../control/postrm": "../pkgScripts/lin/postrem"
        },
        "depends": ["net-tools", "iw", "resolvconf"]
      }
    }
  }
}
azure tapir
#

hmm that all looks fine

#

was the issue the same for tauri dev vs appimage vs deb vs rpm (or whatever subset of those you tested)?

ocean condor
#

i am using only deb bc we need to include postinstall and preinstall scripts

#

with tauri dev issue was that it didnt even register a scheme on a first run, i always need to stop it and run again to register it.

#

after i build it. it register schema correctly and shows app, but no callback is called :/

#

so now i am kinda trying to brut force it, by making a clear repo and copying thing there from our code and see when it breaks 😄

#

Another thing that for example for macos, even though docs says that it should work fine with single instance plugin or without, it actually always opened second instance if i started my app with yarn tauri dev and i have all same code in lib.rs. And it never called onOpenUrl thats why i found this work around for listen('deep-link://new-url) which kinda works okay.
So i am a little suspicious about that migration, bc as i tried to search other people dont really have this problem.

ocean condor
#

@azure tapir could the reason why its not working being a properties productName or mainBinaryName ? When i want to use some user friendly name like "Something Test" its not working. If i change it to "something-test" it works ...

ocean condor
#

PS. after i build it, it calls onOpenUrl all the time over and over again and never stops

azure tapir
azure tapir
ocean condor
#

Sure 👍