The UpdateManager object, used to manage updates. An instance can be obtained via the wx.getUpdateManager API.
Methods
UpdateManager.applyUpdate()
Forces the Mini Program to restart and use the new version. Call this after the new version of the Mini Program has been downloaded (i.e., after receiving the onUpdateReady callback).
UpdateManager.onCheckForUpdate(function listener)
Listens to the event for the result of checking for updates with the WeChat backend. WeChat automatically checks for updates (including hot restarts) every time it launches Mini Program, so developers do not need to trigger this process manually.
UpdateManager.onUpdateReady(function listener)
Listens for the event that a new version of the Mini Program is available. The client initiates the download proactively (no action required from the developer) and invokes the callback upon successful download.
UpdateManager.onUpdateFailed(function listener)
Listens to the Mini Program update failure event. When a new version of Mini Program is available, the client initiates the download (no developer action is required). This callback is triggered if the download fails (e.g., due to network issues).
Sample Code
const updateManager = wx.getUpdateManager()
updateManager.onCheckForUpdate(function (res) {
// 请求完新版本信息的回调
console.log(res.hasUpdate)
})
updateManager.onUpdateReady(function () {
wx.showModal({
title: '更新提示',
content: '新版本已经准备好,是否重启应用?',
success: function (res) {
if (res.confirm) {
// 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
updateManager.applyUpdate()
}
}
})
})
updateManager.onUpdateFailed(function () {
// 新版本下载失败
})
Tips
- In WeChat DevTools, you can debug by toggling the "Simulate Update for Next Compilation" switch under "Compilation Mode"
- Mini Program Development versions and Experience versions do not have a "version" concept, so you cannot test version update scenarios on Development versions or Experience versions