# Mini Program update mechanism

After the developer releases a new version of the Mini Program in the management background, the WeChat client will have several opportunities to check whether the locally cached Mini Program has a new version and update the code package of the Mini Program. However, if the user has a historical version of the Mini Program locally, it may still be an old version open at this time.

# 1. Synchronous update at startup

When the Mini Program starts, the code package is updated synchronously. The synchronous update will block the startup process of the Mini Program and affect the startup time of the Mini Program.

If the update fails or times out, the local version is still opened to ensure the availability of the Mini Program.

# Check regularly to find updates

When WeChat is running, it will regularly check whether the recently used mini programs have been updated. If there is an update, the next time the Mini Program starts will be updated synchronously, updated to the latest version and then open the Mini Program, as far as possible to ensure that users can use the latest version of the Mini Program as soon as possible.

# Users do not use Mini programs for a long time

When the user does not use the Mini Program for a long time, in order to ensure the real-time nature of the Mini Program version, it will be forced to synchronize the update of the version, update to the latest version, and then open the Mini Program.

If the user is in a weak network environment and fails to download the latest version, the local lower version will still be launched.

# 2. Asynchronous update at startup

Even if the update is not detected before startup, each time the Mini ProgramCold startIs checked asynchronously for an updated version. If a new version is found, the new version of the code package is downloaded asynchronously. However, when the first boot will still use the client local old version of the code, that is, the new version of the Mini Program needs to wait.The Next Cold StartBefore they use it.

# Developers manually trigger updates

In the case of asynchronous updates at startup, if the developer wants to update the version immediately, they can use the wx.getUpdateManager API For processing. When there is a new version, prompt the user to restart the Mini Program to update the new version.

const updateManager = wx.getUpdateManager()

updateManager.onCheckForUpdate(function (res) {
  // Callback after requesting new version information
  console.log(res.hasUpdate )
})

updateManager.onUpdateReady(function () {
  wx.showModal({
    title: 'Update ',
    content: The new version is ready, do you want to restart the application? ',
    success(res) {
      if (res.confirm) {
        // The new version has been downloaded, call the ApplyUpdate  Apply the new version and restart
        updateManager.applyUpdate()
      }
    }
  })
})

updateManager.onUpdateFailed(function () {
  // New Version Download Failed
})

Mini Program developers can affect the update logic by setting it in the Mini Program management background.

# Preference for local version settings

If the developer determines that some of the newer versions of the Mini Program do not need to force users to synchronize to the latest version, they can use the Mini Program management background.Set up-Version settings-Preference for local version settingsAfter setting, if the local version is not lower than the version when updating synchronously, the local version will be used first, and the latest version of the code package will be downloaded asynchronously.

# Minimum Available Version Settings for Mini Program

If the developer determines that some of the older Mini Program version services are no longer available, they can use the Mini Program management background.Set up-Version settings-Minimum Available Version Settings for Mini ProgramSet up in. After setting up, if the local version is lower than the version when updating synchronously, it cannot be opened, and continue to try to download the latest version. If updating asynchronously, it will prompt the user to restart the Mini Program to update the new version after checking for updates.

# Be careful

  1. After the developer releases the new version in the background, it cannot immediately affect all current users. 24 Hours later, the new version can override 99% The above users.
  2. Mini Program management backgroundPreference for local version settingsandMinimum Available Version Settings for Mini ProgramDoes not affect the choice between synchronous and asynchronous updates.