electron-builder: app.setAsDefaultProtocolClient doesn’t work on Linux .AppImage

  • electon-builder: 20.44.4 and tried 21.0.13
  • electon-updater: 4.0.14 and tried 4.1.2
  • Target: linux (.AppImage, Ubuntu)

app.setAsDefaultProtocolClient works for macOS and Windows, but not for Linux, at least when using .AppImage, which is the only one I use.

The custom protocol / schema is never registered and app.isDefaultProtocolClient will always return false.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 9
  • Comments: 28 (10 by maintainers)

Commits related to this issue

Most upvoted comments

The bug is still present in electron@6.0.9 and electron-builder@21.2.0

I ran into the same problem. I had to workaround using : childProcess.exec('xdg-mime default myApp.desktop x-scheme-handler/myapp')

First of all app.setAsDefaultProtocolClient works only on mac and windows, for linux you need an entry in your desktop file. So I see the following steps:

1. Mimetype/Protocol in .desktop file

Have the mimetype inside of your build configuration and validate you did the right thing by extracting the appimage (./path/to/appimage --appimage-extract) and checking the .desktop file. see https://github.com/deltachat/deltachat-desktop/commit/2e722f4484193602a15b49c7ca1f5d8d10eff9ea#diff-b9cfc7f2cdf78a7f4b91a753d10865a2 for an example for the build configuration. [looks like you have already done this step in https://github.com/devhubapp/devhub/blob/master/packages/desktop/package.json]

2. Install the AppImage

Make sure you have the appimage “installed”/“Integrated” so that the .desktop file is found by your OS. (on Manjaro Linux, the already installed https://github.com/TheAssassin/AppImageLauncher works fine for this. Double click the appimage and click on Integrate and Run) -> now it should open your app when doing: xdg-open “devhub:test”, but your app won’t know that it was opened by this url, thats where step 3 comes into play.

3. Patch electron-builder

In the .desktop file there is an Exec key that say what should happen when executing it. You can use variables there to pass more information to the app (see documentation). what we need here is %U for passing the filepaths/urls that should be processed by the app to the app, which is automatically added by electron-builder, except for app images, there the `Exec``key is just overwritten by “AppRun” and the %U was forgotten to be added. We can fix it like that:

sed -i "s/\"AppRun\"/\"AppRun\ %U\"/" node_modules/app-builder-lib/out/targets/AppImageTarget.js

For me it looks like you are still on 2 while my PR is doing step 3.

I have this issue not only with .AppImage but also with .deb packages. My workaround is as follows:

Create a file called <yourapp>.desktop in ~/.local/share/applications/ if intended for current user only, or in /usr/share/applications/ if for all users. Make sure that <yourapp> is in the path, otherwise set the whole path for Exec and Icon (below).

[Desktop Entry]
Name=<Your App>
Exec=<yourapp> %u
Icon=<yourapp>
Type=Application

Set handler for a specific protocol

execSync ( 'xdg-settings set default-url-scheme-handler <your prot name> <yourapp>.desktop' )

This creates an entry like this:

x-scheme-handler/<your prot name>=<yourapp>.desktop

in:

~/.config/mimeapps.list

of course you can create that entry manually as well.

Get handler assigned to a specific protocol

execSync ( 'xdg-settings get default-url-scheme-handler <your prot name>' ).toString().trim()

The result will be:

<yourapp>

You do need to add “x-scheme-handler/MyApp;” as a known mimetype in your .desktop file.

You don’t need to specify the full path of your .desktop file, as ~/.local/share/applications/ is one of the default paths for them.

Desktop file are generated by electron-builder, and managed automaticaly by the OS. You can customize it through your package.json:

"linux": {
  "desktop": {
    "MimeType": "x-scheme-handler/MyApp;" 
  }
}

The electron-builder doc might not be extremely explicit: https://www.electron.build/configuration/appimage

BTW, don’t put a space in your appimage name ! There is an escaping probleme somewhere… see: https://github.com/electron-userland/electron-builder/issues/3547