capacitor-updater icon indicating copy to clipboard operation
capacitor-updater copied to clipboard

bug: multiple updates on Android

Open khamaileon opened this issue 8 months ago • 1 comments

Bug Report

Capacitor Version

6.1.0

Plugin Version

6.0.29

context(s)

ManualModel: true
AutoMode: false
CapgoCloud: false
OnPremise: false

Platform(s)

Android for now. Haven't try iOS yet.

Current Behavior

The update is repeated several times (from 2 to 5 times).

Expected Behavior

The update goes through and the version of my app is up to date after the reload.

Code Reproduction

If the code I provide doesn't help find a solution, I'll try to provide a test project.

Other Technical Details

npm --version output:

9.1.0

node --version output:

v22.1.0

pod --version output (iOS issues only):

Additional Context

Here is my Vue JS update dialog view:

<template>
  <v-dialog v-if="updateExists" persistent :value="true" :width="width">
    <v-card>
      <v-card-title class="text-h5">
        {{ $t("updaterDialog.title") }}
      </v-card-title>
      <v-card-text>
        {{ $t("updaterDialog.message") }}
      </v-card-text>
      <v-card-actions>
        <v-spacer />
        <v-btn class="rounded-lg" color="primary" large @click="update">
          {{ $t("updaterDialog.action") }}
        </v-btn>
      </v-card-actions>
    </v-card>
  </v-dialog>
</template>

<script>
import { Capacitor } from "@capacitor/core";
import { App as CapacitorApp } from "@capacitor/app";
import { CapacitorUpdater } from "@capgo/capacitor-updater";
import { compareVersions } from "compare-versions";

export default {
  data() {
    return {
      updateExists: false,
      bundle: null,
    };
  },
  computed: {
    width() {
      const { name, thresholds } = this.$vuetify.breakpoint;
      return (thresholds[name] || 1920) * 0.5;
    },
  },
  async created() {
    CapacitorUpdater.notifyAppReady();
    if (process.env.NODE_ENV === "production") {
      const state = await CapacitorApp.getState();
      if (state.isActive) {
        await this.checkForUpdate();
      }
      CapacitorApp.addListener("appStateChange", async (state) => {
        if (state.isActive) {
          await this.checkForUpdate();
        }
      });
    }
  },
  methods: {
    async fetchReleaseInfo() {
      const protocol = process.env.VUE_APP_URL_PROTOCOL;
      const host = process.env.VUE_APP_URL_HOST;
      return await fetch(
        `${protocol}://static.${host}/releases/latest.json`
      ).then((response) => response.json());
    },
    async checkForUpdate() {
      let currentVersion;
      if (Capacitor.isNativePlatform()) {
        const current = await CapacitorUpdater.current();
        currentVersion = await current.options?.version;
      }

      if (!currentVersion) {
        currentVersion = process.env.VUE_APP_VERSION;
      }

      const latest = await this.fetchReleaseInfo();

      if (compareVersions(currentVersion, latest.version) >= 0) return;

      if (Capacitor.isNativePlatform()) {
        this.bundle = await CapacitorUpdater.download({
          url: latest.url,
          version: latest.version,
          checksum: latest.checksum,
        });
        if (this.bundle) {
          this.updateExists = true;
        }
      } else {
        this.updateExists = true;
      }
    },
    async update() {
      if (Capacitor.isNativePlatform()) {
        await CapacitorUpdater.set(this.bundle);
      } else {
        window.location.reload();
      }
    },
  },
};
</script>

khamaileon avatar Jun 17 '24 09:06 khamaileon