cordova-plugin-android-cookieflush
v1.0.0
Published
Forces the Android WebView CookieManager to flush cookies to disk immediately, so cookies are not lost when the app process is killed.
Maintainers
Readme
cordova-plugin-android-cookieflush
A Cordova plugin that forces the Android WebView's CookieManager to flush cookies to disk immediately, so cookies (e.g. login/session cookies) are not lost when the app process is killed shortly after they are set or cleared.
Platform: Android only Minimum SDK: Android 5.0 (API 21) npm: https://www.npmjs.com/package/cordova-plugin-android-cookieflush
Why
Android's WebView (Chromium) batches cookie writes and only persists them to disk periodically (roughly every 30 seconds). If the app process is killed before that flush happens — for example when the user exits via the hardware back button, or the OS reclaims the process — any cookie written or deleted in that window is lost. On the next launch the WebView reads the stale cookie jar from disk, so a fresh login can appear logged out, or a logout can appear still logged in.
android.webkit.CookieManager.flush() forces the in-memory cookie jar to be written to disk. This plugin exposes that call to JavaScript and also runs it automatically when the app is paused or destroyed.
Installation
cordova plugin add cordova-plugin-android-cookieflushWhat it does
Provides one method on cordova.plugins.cookieFlush:
| Method | Description |
|--------|-------------|
| flush(success, error) | Forces the WebView cookie jar to be written to disk. Both callbacks are optional. |
In addition, the plugin flushes automatically (no JS required) on the Android lifecycle events onPause and onDestroy, covering Home-button, task-switch, and swipe-away closes.
Usage
Call flush right before exiting the app, or after a login/logout, so the cookie change reaches disk before the process can be killed:
cordova.plugins.cookieFlush.flush(
function () {
// cookies persisted — safe to exit
navigator.app.exitApp();
},
function (err) {
console.error('Cookie flush failed:', err);
navigator.app.exitApp();
}
);flush is safe to call fire-and-forget (both callbacks are optional):
cordova.plugins.cookieFlush.flush();Notes
- Android only. iOS WKWebView has no equivalent flush API; cookies there are persisted by
WKWebsiteDataStoreautomatically. The plugin is not installed on other platforms, so a guarded call is a safe no-op. - The
flush()JS call runsCookieManager.flush()on a background thread; the automaticonPause/onDestroyflushes run synchronously so they complete before the process is torn down.
License
ISC
