当前位置:网站首页>Applet control version update best practices
Applet control version update best practices
2022-06-23 02:21:00 【seven hundred and ninety million five hundred and thirty-one th】
Analysis of applet update mechanism
According to wechat applet Official documents Explanation , The update mechanism of the applet is mainly divided into Update when not started and Update on startup Two modes . Update on startup It will asynchronously check whether there is a new version when the applet is cold started , If there's a new version , Will download it , Wait for the next cold start to start with the new version code ; and Update when not started There will be a timer for the most recent 7 Regularly check whether there is a new version of the applet used within days , Every time 6 Once an hour , If there is a new version, it will be pre downloaded , You can directly use the latest version at the next cold start .
Applets from the base library 1.9.90 From the beginning wx.getUpdateManager Interface , Use this interface , You can know if there is a new version of the applet 、 Whether the new version is downloaded well and the ability to apply the new version . In the officially recommended Best practices in , To ensure the user experience , The advice is only in highly necessary Pop up a box to remind the user to force the update .
And the time when we need to update is uncertain , Sometimes it's urgent bug Repair requires forced updates , Sometimes the new function of the activity needs to be forced to update if it wants to cover all users quickly , So we need a distribution mechanism that can flexibly control user version updates .
Forced update version configuration distribution
To achieve version comparison , And determine whether to force the update , First of all, we must ensure that the applet release has standard version management and inject the version number into the code , Then, the minimum version number to be updated is issued through the interface and compared with the currently open version to complete the reminder of forced update .
Version management and version injection
In our applet project standard-version Perform version number management , follow Semver Semantic version specification . Each time it is published, it passes standard-version To strike tag And update the package.json In the document version Field
tag Of push Will trigger pipeline construction , stay npm run build Stage , Get by extracting package.json In the field , And pass gulp-batch-replace Plug in version number substitution , Complete the injection of version number into the code
// gulpfile.js
const gulpif = require('gulp-if');
const batchReplace = require('gulp-batch-replace');
const version = require('./package.json').version;
gulp.src('src/**/*.js').pipe(gulpif(
isEnvJs,
batchReplace([
['process.env.APP_VERSION', JSON.stringify(version)],
...
]),
))// env.js
const env = {
version: process.env.APP_VERSION,
...
}Version configuration management
On the back-end version configuration , We have adopted a more granular version control configuration , It is divided into application level version control , Page level version control and interface level version control .
- Application level (app)
The application level configuration allows the user to enter the applet , stay app.onLaunch Phase to judge the version number
- Page level (pages)
When a user visits a page , If the page path hits the configuration path , It will trigger version number judgment
- Interface level (apis)
When a user requests a specific interface using an applet , It will trigger version number judgment
By refining the granularity of version control , If a new version is downloaded when the user accesses , However, the page or interface that the user is currently visiting does not require forced update , There is no need to pop-up a box to remind the user to perform forced update , More in-depth guarantee of user experience .
Client version comparison
On the applet side , adopt Object.defineProperty() Method pair wx.request Method for proxy and global Page.onLoad Method to check the updated code .
Such as through wx.getUpdateManager() Check that a new version has been downloaded , And the current request Requested url Hit the above configuration apis Interface path in or page.onLoad Medium this.route hit pages The path configured in , You can pop up a window to remind the user to force the update .
function getVersionArr(versionStr) {
const str = versionStr;
return str.match(/[0-9]+/ig);
}
// Version comparison method
export function shouldUpgrade(currentVersion, targetVersion = '0.0.0') {
const currentVersionArr = getVersionArr(currentVersion);
const targetVersionArr = getVersionArr(targetVersion);
const len = Math.max(currentVersionArr.length, targetVersionArr.length);
while (currentVersionArr.length < len) {
currentVersionArr.push('0');
}
while (targetVersionArr.length < len) {
targetVersionArr.push('0');
}
for (let i = 0; i < len; i++) {
const current = parseInt(currentVersionArr[i], 10);
const target = parseInt(targetVersionArr[i], 10);
if (current !== target) {
return current < target;
}
}
return false;
}Automate forced version updates
Problem analysis
As mentioned above, the forced version update is implemented by manually changing the configuration distribution , However, there is an implicit feature in the version management of wechat side applet , Small program code package cdn There are time and number limits for caching code package versions , The current limit is 30 The applet version released within days cannot exceed 30 individual .
for example 30 It was released in a few days v1 ~ v31 this 31 A version , Before a user accesses v1 Version of the applet , Then I haven't visited again for a long time , So when you publish v31 Version when the user comes to visit , By default, the applet in the cold start phase is cached locally by the user v1 Version of the code package , When the user operation jumps to v1 Version of other subcontracting pages , Because the wechat server v1 Version of the code package cache has expired , Will lead to “ The network can't connect ” Error of .
In this case, we need an automatic access applet before 30 Functions of version numbers , And update the version number to the distributed configuration , As applet app In the start-up phase, a judgment basis for forced update .
But as mentioned earlier, our version number is based on semver Semantic version number , The format is as follows X.Y.Z The pattern of , It can be used to judge the version size , However, it is necessary to calculate whether there is a difference between the two version numbers 30 A version , This cannot be calculated .
adopt git Of tag Number extract version number
No direct access semver Before the version number of is calculated 30 The value of the version number of versions , But thanks to the fact that every time we publish it, we make tag publishable , So you can go through git Submitted in tag Count , Look back at each release 20 A version ( Here we use 20 The first is to prevent rolling back and then publishing after publishing , The same version number has been released many times ) Version number of .
git ls-remote --tags --refs --sort="v:refname" | tail -n20 | sed 's/.*\///'
Every time a version is released , Change the previous paragraph 20 individual tag The corresponding version number is updated to the background configuration , This achieves the goal of automatically updating the minimum version number , It effectively avoids user access errors caused by too frequent version release .
边栏推荐
- //1.13 auto increment and auto decrement operators (+ +, --)
- Unity official case nightmare shooter development summary < I > realization of the role's attack function
- Uniapp View Horizontal Center
- Mobile communication Overview - Architecture
- Push RTMP stream using ffmpeg
- JS case: support canvas electronic signature function on PC and mobile
- Chapter 3 tensorflow linear regression
- Rebirth -- C language and the story I have to tell (text)
- Primary pointer part
- Spread spectrum and frequency hopping
猜你喜欢

Application and challenge of ten billion level map data in Kwai security intelligence

Rebirth -- C language and the story I have to tell (text)

6. const usage, combination of first and second level pointers

Nfv and SDN

WebService details

1. Mx6u bare metal program (2) - Lighting master (imitating 32 register version)

Ansible practice of Nepal graph

Deep learning environment configuration (III) pytorch GPU under Anaconda

Mongodb aggregate query implements multi table associated query, type conversion, and returns specified parameters.

Vs Code inadvertently disable error waveform curve
随机推荐
what the fuck! If you can't grab it, write it yourself. Use code to realize a Bing Dwen Dwen. It's so beautiful ~!
Using mock data in vite projects -vite plugin mock
Bc116 xiaolele changed to digital
Custom shapes for ugui skill learning
Three methods for solving Fibonacci sequence feibonacci (seeking rabbit) - program design
Deep learning environment configuration (III) pytorch GPU under Anaconda
Ugui empty button implementation
Deep learning environment configuration (I) installation of CUDA and cudnn
9. class and object practice and initialization list
Deep scan log4j2 vulnerability using codesec code audit platform
Digital integrated circuit design process
Mongodb aggregate query implements multi table associated query, type conversion, and returns specified parameters.
Phpexcel export with picture Excel
II Data preprocessing
Common mistakes in C language (sizeof and strlen)
1. Mx6u bare metal program (5) - external interrupt
WebService details
Spread spectrum and frequency hopping
//1.7 use of escape characters
Bc117 xiaolele walks up the steps