0

I'm developing an Android app using Angular + Capacitor. I implemented a custom update mechanism that downloads a .zip file from my server, extracts it to a custom folder inside FilesystemDirectory.Data, and then sets the base path using WebView.setServerBasePath.

Everything works great after the update:

The app downloads and extracts the zip successfully.

The new version is shown after window.location.reload().

I store the extracted path and version using @capacitor/storage.

However, after the app is killed or swiped away from memory, the app launches with the old version (the one from www/, not from custom_www/).

Here is what I do in main.ts before platformBrowserDynamic():

const STORAGE_KEY = 'customBasePath';
let customWebDirPath: any = null;

const setCustomBasePath = async (): Promise<void> => {

    const platform = Capacitor.getPlatform();
    const isAlreadyInCustomPath = await Storage.get({ key: STORAGE_KEY });


    if (platform !== 'web' && isAlreadyInCustomPath.value !== 'true') {
        try {

            customWebDirPath = await Storage.get({ key: 'customBasePath' });

            alert(`customWebDirPath: ${customWebDirPath.value}`)

            await Filesystem.stat({
                directory: Directory.Data,
                path: `${customWebDirPath.value}/index.html`,
            });

            const uri = await Filesystem.getUri({
                directory: Directory.Data,
                path: customWebDirPath.value,
            });

            const absolutePath = uri.uri.replace('file://', '');

            await WebView.setServerBasePath({ path: absolutePath });

            await Storage.set({ key: STORAGE_KEY, value: 'true' });


        } catch (err) {
            alert('catch')
            console.warn('⚠️ Could not set custom base path. Error:', err);
        }
    } else {
        const currentBasePath = await WebView.getServerBasePath();
    }
};

and app.component.ts (only unzip function)

this.zip.unzip(zipFilePath, unpackDirPath, async (status: any) => {
                    if (status === 0) {
                        alert('🎉 Unzipped successfully!');

                        try {
                            // Удаляем предыдущую custom_www директорию, если существует
                            try {
                                await Filesystem.rmdir({
                                    directory: Directory.Data,
                                    path: customWebDirPath,
                                    recursive: true
                                });
                            } catch (e) {
                                // log error
                            }

                            // copu to custom_www
                            await this.copyFolder(unpackPath, customWebDirPath);

                            // get absolute path custom_www
                            const webDirUri = await Filesystem.getUri({
                                directory: Directory.Data,
                                path: customWebDirPath
                            });
                            const absoluteWebDirPath = webDirUri.uri.replace('file://', '');

                            // set
                            await WebView.setServerBasePath({ path: absoluteWebDirPath });

                            // save custom path to the storage
                            await Storage.set({ key: 'customBasePath', value: absoluteWebDirPath });
                            await Storage.set({ key: 'app_version', value: version });

                            alert(`✅ Set new base path: ${absoluteWebDirPath}`);


                            // restart app
                            window.location.reload();

                        } catch (e) {
                            console.error('❌ Error copying files:', e);
                        }
                    } else {
                        console.warn('❌ Failed to unzip update');
                    }
                });
    ```

after update my app shows correct version all good. but after restart(close from memory) it loads start version.

It seems that WebView.setServerBasePath() does not persist across app restarts.

Question:
How can I make Capacitor/Angular app consistently load from the updated custom_www folder (inside FilesystemDirectory.Data) even after the app is killed and restarted?

1 Answer 1

0

This is by design to prevent bad updates, once you have verified the app update was ok and it launched from the new path correctly you should call WebView.persistServerBasePath() to persist the path for future launches. If not persisted it will consider the update was bad as it failed to persist the path.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.