DEV Community

Frederik Hudák
Frederik Hudák

Posted on

Useful vscode extension patterns - Update Notification

Basic problem - when you ship a new version, you want users to know and look at what's changed.

I will demonstrate a simple pattern you can reuse in your extension. The result is exactly this:

notification

Don't wing it

Winging a notification on startup introduces two problems:

  • brand-new installs get a toast for software they just installed
  • the toast fires again on every window reload. your users will uninstall.

Proper solution

On activation, compare the running version against the last version you told the user about. The "last notified" version is persisted in globalState, which is shared across all windows and survives restarts.

stored == none     → fresh install   → store silently, never notify
stored == current  → reload/restart  → silent
current  > stored  → update          → store, notify if the bump clears the user's preference
current  < stored  → downgrade       → store, stay silent
Enter fullscreen mode Exit fullscreen mode

We also introduce a user preference where they can set if they want to be notified about major/minor/patch versions.

Don't overcomplicate the changelog

It can and should be dead simple - you don't need a webview. Keep a CHANGELOG.md, bundle it with the extension, then have the "what's new" button open a preview.

const uri = vscode.Uri.joinPath(context.extensionUri, 'CHANGELOG.md');
await vscode.commands.executeCommand('markdown.showPreview', uri);
Enter fullscreen mode Exit fullscreen mode

Side benefit

When your extension inevitably becomes compromised and the hackers publish a malicious new version for you, at least your users get a nice toast message.

I don't think they'll bother to add a changelog entry though.

Example code

Self-contained example extension can be found here:
https://github.com/FreHu/vscode-update-notification

The example was carved out from my larger extension, Fresh File Explorer

I have a few more up my sleeve and plan to turn this into a short series with similar self-contained examples.

Top comments (0)