17

Deploying a chrome packaged app and publishing updates on the chrome web store allows users to automatically receive application updates. There are situations where you want to know if the running application is the most current or not, and updating it. E.G.:

  • Just keeping the user on the most recent version.
  • Detecting a mismatch between the application and server side APIs, and requiring the client side application to update to use new server side APIs.

Documentation for chrome.runtime.requestUpdateCheck() offers a status of "throttled", "no_update", "update_available", but doesn't indicate what to do if a newer version is required.

Vincent Scheib
  • 17,142
  • 9
  • 61
  • 77

4 Answers4

31

Install a listener for chrome.runtime.onUpdateAvailable, which fires when the new .crx file has been downloaded and the new version is ready to be installed. Then, call chrome.runtime.requestUpdateCheck:

chrome.runtime.onUpdateAvailable.addListener(function(details) {
  console.log("updating to version " + details.version);
  chrome.runtime.reload();
});

chrome.runtime.requestUpdateCheck(function(status) {
  if (status == "update_available") {
    console.log("update pending...");
  } else if (status == "no_update") {
    console.log("no update found");
  } else if (status == "throttled") {
    console.log("Oops, I'm asking too frequently - I need to back off.");
  }
});
SondreB
  • 808
  • 7
  • 14
Antony Sargent
  • 842
  • 7
  • 11
  • 2
    Where is `throttled` documented? How do you know what does it mean? – cprcrack Feb 02 '15 at 01:11
  • 1
    The 'throttled' status is mentioned just as a possible return value at http://developer.chrome.com/apps/runtime.html#method-requestUpdateCheck . It just means that you've been asking too frequently, and instead of actually doing the network fetch to see if there is an update, chrome is throttling your requests and you need to wait a few minutes before asking again. I suppose it would be useful if you also got back an indication of how long you'd need to wait. – Antony Sargent Feb 09 '15 at 20:57
  • 3
    Would it be worth wrapping the `chrome.runtime.requestUpdateCheck(...)` call in an if statement that throttled it to only be called every 15mins, 30mins, or even every hour? I want to put this in popup.html but I imagine it's an expensive operation. – benmccallum Mar 23 '15 at 01:13
  • Does requestUpdateCheck have any significance except to know the update status ? – mallik1055 Nov 03 '15 at 10:04
  • @AntonySargent, What happens when you need to know when an update is available, but the user's browser hasn't downloaded the crx? – Pacerier Jul 26 '17 at 11:57
  • [requestUpdateCheck](https://developer.chrome.com/extensions/runtime#method-requestUpdateCheck) forces the browser to check if your add-on has an update, rather than relying on the existing, automated check for updates. Generally this is not recommended, but should be fine in response to a user-initiated action. On the other hand, `onUpdateAvailable` is especially helpful , since extensions will not auto-update while a background page is in use (which effectively means waiting until a browser restart, in the case of extensions using a persistent background page). – Brian Jul 03 '18 at 14:22
0

Depending on your application, when an update is detected you may want to use something like setTimeout and call chrome.runtime.restart() or chrome.runtime.restart() later

Mac
  • 432
  • 3
  • 9
  • 1
    `restart()` is only available for kiosk mode. https://developer.chrome.com/extensions/runtime#method-restart – Pier Nov 01 '17 at 21:08
0

According to the Google Chrome documentation you need to have

chrome.runtime.onUpdateAvailable.addListener(function(details) {
  chrome.runtime.reload(); // To restart the chrome App instantaneously
});

But this take time to reflect JS changes into the chrome because background.js loaded into the background and it needs to be unloaded and loaded again

To cop this situation you need to include

chrome.runtime.onInstalled.addListener(function(details) {
  chrome.runtime.reload();
});

as wel.

onInstalled called whenever google extension installed first time (fresh installation), google extension updated or google chrome updated.

Touseef Murtaza
  • 1,548
  • 14
  • 20
0

More documentation can be found in this reference as well

https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/runtime/RequestUpdateCheckStatus