Configuring Automatic Updates for WeChat Mini Programs

After we have developed the applet and released a new version, if users want to experience the new version, they need to delete the applet from the use list, search again and open it again, and the new version will take effect. However, as the number of our updates increases, this method is very unfriendly and the user experience decreases. Therefore, during development, we have to consider configuring the automatic update of the applet.

First of all, we need to understand the operating mechanism of the applet. The startup of the applet can be divided into two situations, one is cold start and the other is hot start .

  • Cold start : If the user opens the applet for the first time, or is opened again by the user after the applet is destroyed, the applet needs to be reloaded and started, that is, cold start.

  • Hot start : If the user has already opened a small program, and then opens the small program again within a certain period of time, the small program is not destroyed at this time, but enters the foreground state from the background state, this process is a hot start.

Under the understanding of the update mechanism

  • Update when not started : It means that the historical version of the applet already exists locally in the user’s local area, and it is not the latest version when it is opened at this time. In the worst case, it will be sent to the user within 24 hours.

  • Update at startup : It is a cold start, the user has never experienced the applet, or the applet is destroyed and then opened. Download the new version asynchronously and start it with the local package at the same time. The new version will not be applied until the next cold start.

The above briefly introduces the operating mechanism and update mechanism of the applet, and the next step is to configure the automatic update of the applet. We add the following code in app.js

// 小程序自动更新
if (wx.canIUse('getUpdateManager')) {
  const updateManager = wx.getUpdateManager()
  updateManager.onCheckForUpdate(function (res) {
    // 请求完新版本信息的回调
    if (res.hasUpdate) {
      updateManager.onUpdateReady(function () {
        wx.showModal({
          title: '更新提示',
          content: '新版本已经准备好,是否重启应用?',
          success: function (res) {
            if (res.confirm) {
              // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
              updateManager.applyUpdate()
            }
          }
        })
      })
      updateManager.onUpdateFailed(function () {
        // 新的版本下载失败
        wx.showModal({
          title: '已经有新版本了哟~',
          content: '新版本已经上线啦~,请您删除当前小程序,重新搜索打开哟~'
        })
      })
    }
  })
}

Add compile mode

 

 The applet will be recompiled, and the version update has been prompted

 

003c8e8f7896545d5f947ed0cfae3fe9.png

 

Guess you like

Origin blog.csdn.net/jewels_w/article/details/130681471