microsoft / cordova-plugin-code-push

Cordova plugin for CodePush
http://appcenter.ms
Other
644 stars 336 forks source link

Custom deployment key not working app startup (first call) #615

Open miqmago opened 4 years ago

miqmago commented 4 years ago

Description

I'm overriding deployment keys via javascript code so I can dynamically move users from one deployment to another.

When first call to sync or checkForUpdate with an overridden deployment key by javascript, the first call is always made with the deployment key from config.xml.

Why it happens:

First time checkFroUpdate is called, Sdk.DefaultAcquisitionManager is not yet instantiated so it enters to NativeAppInfo.getServerURL(... branch which uses deploymentKey from NativeAppInfo: https://github.com/microsoft/cordova-plugin-code-push/blob/master/bin/www/sdk.js#L52

On successive calls, Sdk.DefaultAcquisitionManager is already initialized so it enters to resolveManager(); branch and there it uses userDeploymentKey: https://github.com/microsoft/cordova-plugin-code-push/blob/master/bin/www/sdk.js#L19

Possible solution:

I think it should be easy to fix, just overriding deploymentKey when user defined one (see https://github.com/microsoft/cordova-plugin-code-push/blob/master/bin/www/sdk.js#L52):

else {
    Sdk.DefaultConfiguration = {
        // deploymentKey: deploymentKey, <---- REMOVE
        deploymentKey: userDeploymentKey || deploymentKey, // <---- NEW
        serverUrl: serverURL,
        ignoreAppVersion: false,
        appVersion: appVersion,
        clientUniqueId: device.uuid
    };
    if (deploymentKey) {
        Sdk.DefaultAcquisitionManager = new AcquisitionManager(new HttpRequester(), Sdk.DefaultConfiguration);

Reproduction

config.xml

        <preference name="CodePushDeploymentKey" value="6apbtn709m18fwqxym4p8v5nrtl38ey" />

app.js

codePush.sync(() => {}, { deploymentKey: '0twhsiffnth9tbudnbxwad70ucz9d21' });

I've reached this bug by removing the deployment with key 6apbtn709m18fwqxym4p8v5nrtl38ey from app center and getting this error:

[CodePush] An error occurred while reporting status: 
            {"status":0,"appVersion":"1.2.3","deploymentKey":"6apbtn709m18fwqxym4p8v5nrtl38ey","previousLabelOrAppVersion":null,"previousDeploymentKey":null}
            404: No deployment found.. StackTrace: 
            http://192.168.8.130:8100/plugins/code-push/script/acquisition-sdk.js:130:39 
            http://192.168.8.130:8100/plugins/cordova-plugin-code-push/bin/www/httpRequester.js:30:51 
            H@http://192.168.8.130:8100/build/polyfills.js:3:23955 http://192.168.8.130:8100/build/polyfills.js:3:10844 
            http://192.168.8.130:8100/build/polyfills.js:3:16801 p@http://192.168.8.130:8100/build/polyfills.js:2:27654 
            v@http://192.168.8.130:8100/build/polyfills.js:2:27894 

Additional Information

miqmago commented 4 years ago

In fact this is little more weird: after doing all changes I've found the following situation:

Seems that getAcquisitionManager is called twice internally but just called once from app.js!! The second call uses config.xml deployment key.

But this does not always happen, as when placing breakpoints the function is just called once. Could it be a race condition?

config.xml

<preference name="CodePushDeploymentKey" value="6apbtn709m18fwqxym4p8v5nrtl38ey" />

app.js

console.log('[UpdateManagerService]', 'Checking for updates... using deployment:', 'Production', '0twhsiffnth9tbudnbxwad70ucz9d21');
codePush.sync(() => {}, { deploymentKey: '0twhsiffnth9tbudnbxwad70ucz9d21' });

sync.js

Sdk.getAcquisitionManager = function (callback, userDeploymentKey, contentType) {
        console.log('***************** got', userDeploymentKey);
        var resolveManager = function () {
            if (userDeploymentKey !== Sdk.DefaultConfiguration.deploymentKey || contentType) {
                console.log('***************** 1 using', userDeploymentKey || Sdk.DefaultConfiguration.deploymentKey);
                var customConfiguration = {
                    deploymentKey: userDeploymentKey || Sdk.DefaultConfiguration.deploymentKey,
                    serverUrl: Sdk.DefaultConfiguration.serverUrl,
                    ignoreAppVersion: Sdk.DefaultConfiguration.ignoreAppVersion,
                    appVersion: Sdk.DefaultConfiguration.appVersion,
                    clientUniqueId: Sdk.DefaultConfiguration.clientUniqueId
                };
                var requester = new HttpRequester(contentType);
                var customAcquisitionManager = new AcquisitionManager(requester, customConfiguration);
                callback(null, customAcquisitionManager);
            }
            else if (Sdk.DefaultConfiguration.deploymentKey) {
                console.log('***************** 2 using', userDeploymentKey || Sdk.DefaultConfiguration.deploymentKey);
                callback(null, Sdk.DefaultAcquisitionManager);
            }
            else {
                callback(new Error("No deployment key provided, please provide a default one in your config.xml or specify one in the call to checkForUpdate() or sync()."), null);
            }
        };
        if (Sdk.DefaultAcquisitionManager) {
            resolveManager();
        }

console

console.log: [UpdateManagerService] Checking for updates... using deployment: iosSig 0twhsiffnth9tbudnbxwad70ucz9d21 
console.log: ***************** got 0twhsiffnth9tbudnbxwad70ucz9d21 
console.log: ***************** got 6apbtn709m18fwqxym4p8v5nrtl38ey 
console.log: ***************** 1 using 0twhsiffnth9tbudnbxwad70ucz9d21 
console.log: ***************** 1 using 6apbtn709m18fwqxym4p8v5nrtl38ey 
console.log: [CodePush] Checking for update. 
console.error: [CodePush] An error occurred while reporting status: 
    {"status":0,"appVersion":"1.2.3","deploymentKey":"6apbtn709m18fwqxym4p8v5nrtl38ey","previousLabelOrAppVersion":null,"previousDeploymentKey":null}
    404: No deployment found.. StackTrace: 
    http://192.168.8.130:8100/plugins/code-push/script/acquisition-sdk.js:130:39 
    onreadystatechange@http://192.168.8.130:8100/plugins/cordova-plugin-code-push/bin/www/httpRequester.js:30:51 
    H@http://192.168.8.130:8100/build/polyfills.js:3:23955 
    runTask@http://192.168.8.130:8100/build/polyfills.js:3:10844 
    invokeTask@http://192.168.8.130:8100/build/polyfills.js:3:16801 
    p@http://192.168.8.130:8100/build/polyfills.js:2:27654 
    v@http://192.168.8.130:8100/build/polyfills.js:2:27894 
szh commented 4 years ago

I'm also experiencing this issue. I don't have a deployment key in config.xml since I always set it in JS, so when the app launches it always fails to check for updates.

Krasavinigor commented 4 years ago

Hi @miqmago! Thank you for reporting! I reproduced this issue. We will investigate it as soon as possible.