Qvault’s Offline Mode in Electron

We recently added “Offline Mode” to Qvault as a new feature. This was in preparation to also add Bitcoin and cryptocurrency key generation. There is now a toggle switch at the top of the app that, when switched off, ensures that the app will make no network requests.

offline mode addition

You have always been able to use Qvault offline, that is, assuming the user chose not to sign up for Qvault’s cloud backup option. Now, even if a user is storing their encrypted vault files on Qvault servers, the user can choose to go offline and work locally temporarily.

How Does it Work?

Electron has an API that allows developers to intercept and modify web requests. By intercepting all webRequests before they are made using the onBeforeRequest hook, we can check them see if we want to allow each request or not. For example:

// callback({cancel: true}) stops the request before it is sent.
// callback({cancel: false}) sends the webRequest

mainWindow.webContents.session.webRequest.onBeforeRequest({
    // Intercept all webRequests
    urls: [ '<all_urls>' ]
  }, (details, callback) => {
    // allow all filesystem calls
    if (details.url.substring(0, details.url.indexOf(':')) == 'file'){
      callback({cancel: false});
      return;
    }
    // don't allow any network calls if in offlineMode
    if (!onlineMode){
      callback({cancel: true});
      return;
    }
    // allow al other network calls
    callback({cancel: false});
  });

Hopefully, this helps anyone else looking to control webRequests in Electron, and helps our users understand ‘offline mode’ in Qvault! Thanks for reading.

Thanks For Reading

Hit me up on twitter @wagslane if you have any questions or comments.

Take your coding career to the next level with courses on Qvault Classroom

Follow me on Dev.to: wagslane

%d bloggers like this: