Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

WeChat small program update mechanism


May 17, 2021 WeChat Mini Program Development Document


Table of contents


The small program update mechanism

The update was not started

After the developer manages the release of a new version of the gadget in the background, if a user has a historical version of the gadget locally, the old version may still be open at this time. W eChat clients will have several opportunities to check if the locally cached gadgets have an updated version, and if so, to silently update to the new version. I n general, developers can't immediately affect all current users after a new version is released in the background, but in the worst case, a new version information is sent to the user within 24 hours of release. The next time the user opens, the latest version is updated before opening.

Update at startup

Each time a small program starts cold, it checks for an updated version, and if a new version is found, a new version of the code package is downloaded asynchronously and started with the client's local package, i.e. the new version of the small program needs to wait for the next cold launch before it is applied.

If you need to apply the latest version right away, you can do so using the wx.getUpdateManager API.

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 () {
  // 新版本下载失败
})