# Weixin Mini Program Update mechanism

After a developer releases a new version of Weixin Mini Program in the management background, the WeChat client has several opportunities to check whether the local cached Mini Program has a new release and to update the Mini Program's codebase. However, if the user has an historical version of the Mini Program locally, the older version is likely to be opened at this time.

# 1. Sync up when you start

When Weixin Mini Program starts, the code package is updated synchronously in the following cases. The synchronous update will block the startup process of the Mini Program, affecting the startup time of Mini Programs.

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

# Check regularly to find version updates

The WeChat runtime periodically checks the recently used Weixin Mini Program for updates. If there are updates, the next time the Mini Program starts, it will be updated synchronously, update to the latest version before opening the Mini Program, and ensure that the user can use the latest versions of the Mini Program as soon as possible.

# Weixin Mini Program

When the user has not used Weixin Mini Program for a long time, in order to ensure the real-time nature of the Mini Program version, it is forced to check the version update synchronously, and then open the Mini Program after updating to the latest version.

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

# 2. Update asynchronously at startup

Each time cold starts , an asynchronous check for an updated version is made, even if no update is found before startup. If a new version is found, the new version of the code package is downloaded asynchronously.However, the older version of the client-native code is still used during the startup, i.e. the newer version of the Mini Program needs to wait for to be used the next cold startup .

# Developers manually trigger updates

In the case of asynchronous updates at startup, if the developer wishes to update the version immediately, the wx.getUpdateManager API can handle this.Prompt the user to restart Weixin Mini Program when a new version is available to update the new version.

const updateManager = wx.getUpdateManager()

updateManager.onCheckForUpdate(function (res) {
  // 请求完新版本信息的回调
  console.log(res.hasUpdate)
})

updateManager.onUpdateReady(function () {
  wx.showModal({
    title: '更新提示',
    content: '新版本已经准备好,是否重启应用?',
    success(res) {
      if (res.confirm) {
        // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
        updateManager.applyUpdate()
      }
    }
  })
})

updateManager.onUpdateFailed(function () {
  // 新版本下载失败
})

# 3. Weixin Mini Program Manage the relevant settings in the background

Weixin Mini Program Developers can influence the update logic by setting it in the Mini Program management background.

# Use the local version of the settings first

If a developer determines that some newer Weixin Mini Program versions do not need to force users to synchronize to the latest version, they can be set up in Settings - Version Settings - Local Version Settings in the Petty Administration background.When set up, if the local version is checked to be no lower than that version when synchronizing updates, the local version will be given priority and the latest version of the code bundle will be downloaded asynchronously.

# Weixin Mini Program Minimum Available Version Settings

If a developer determines that some of the older Weixin Mini Program version services are no longer available, they can be set in Settings - Version Settings - Minimum Available Version Settings for a Particle in the Particle Administration background.After you set it up, if the local version is checked when you update synchronously, you cannot open it and continue to try to download the latest version. If you update asynchronously, you will prompt the user to restart the Mini Program to update the new version after checking for an update.

# Be careful

  1. Developers cannot immediately affect all users on the Internet when they release a new version in the background, and under normal circumstances, a new release can reach more than 99% of users 24 hours after full release.
  2. Weixin Mini Program The "Local Version Preference Settings" and "Mini Program Minimum Available Version Settings" in the management background will not affect the choice of synchronous and asynchronous updates.