Forcing Automatic Updates in WeChat Mini Programs

Running Mechanisms

Mini programs can be launched in two different ways: cold start and hot start.

A hot start occurs when a user has previously opaned a mini program and reopens it within a certain time frame (typically five minutes). In this case, the mini program doesn't need to reload—it simply transitions from background to foreground.

A cold start happens when a user opens the mini program for the first time, or after the program has been terminated by WeChat. During a cold start, the mini program must be completely reloaded.

Key points to remember:

  • Mini programs don't have a true "restart" concept
  • When a mini program moves to the background, WeChat keeps it running for a limited period (approximately 5 minutes) before terminating it
  • If the system detects multiple memory warnings within a short window (5 seconds), the minni program will be terminated

Update Mechanism

During a cold start, if WeChat detects a new version is available, it asynchronously downloads the updated code package while continuing to run the current local version. This means the new version won't take effect until the next cold start.

For scenarios requiring immediate adoption of updates, use the wx.getUpdateManager API.

Update API Implementation

The wx.getUpdateManager() interface provides functionality to detect new versions, check download status, and apply updates.

Implementation: Add the following code to the onLaunch function in app.js:

onLaunch() {
    wx.showLoading({
        title: 'Loading...',
        mask: true
    });
    
    const manager = wx.getUpdateManager();
    
    manager.onCheckForUpdate((response) => {
        console.log('New version available:', response.hasUpdate);
        if (!response.hasUpdate) {
            wx.hideLoading();
        }
    });
    
    manager.onUpdateReady(() => {
        wx.hideLoading();
        wx.showModal({
            title: 'Update Available',
            content: 'A new version is ready. Restart to apply the update?',
            showCancel: false,
            success: (dialogResult) => {
                if (dialogResult.confirm) {
                    manager.applyUpdate();
                }
            }
        });
    });
    
    manager.onUpdateFailed(() => {
        wx.hideLoading();
        wx.showModal({
            title: 'Update Failed',
            content: 'A new version was detected but failed to download. Please check your network and try again.',
            showCancel: false
        });
    });
    
    setTimeout(wx.hideLoading, 5000);
}

Since onCheckForUpdate introduces a noticeable delay (approximately 4 seconds in the developer tools), a loading overlay prevents users from interacting with the interface during the version check process.

Note that even after applying the update, the changes won't take effect immediately—the user must perform a cold start for the new version to fully activate.

Testing

To test the update funcsionality, use WeChat developer tools to simulate different version scenarios and verify that the update flow behaves as expected.

Tags: WeChat Mini Program Update Mechanism wx.getUpdateManager Cold Start Hot Start

Posted on Sat, 06 Jun 2026 16:01:14 +0000 by floppy