fix(android): skip lifecycle events when the Capacitor bridge is not present

Merged
AL
1mo ago
+8-1

What

MockCordovaWebViewImpl.triggerDocumentEvent() evaluated window.Capacitor.triggerEvent(...) without checking that the bridge exists. On a page served without the bridge (for example the one behind server.errorPath), every pause and resume logged:

Capacitor/Console: File: https://localhost/error.html - Line 1 - Msg: Uncaught TypeError: Cannot read properties of undefined (reading 'triggerEvent')

The evaluated script is now guarded with typeof window.Capacitor !== 'undefined' && typeof window.Capacitor.triggerEvent === 'function'. When the bridge is missing the event is dropped rather than deferred, so a page never receives a pause or resume for a transition it did not go through. Pages with the bridge are unaffected.

Fixes #8637

Reproduction

The Cordova compatibility layer only runs when the app has at least one Cordova plugin, so the app needs one:

  1. Android app with a Cordova plugin installed, server.errorPath: 'error.html' pointing at a plain page with no JavaScript, and a way to land on it (for example server.url pointing at an unreachable host).
  2. Background and foreground the app and watch Logcat.

Verification

Pixel 7, Android 17, Capacitor 8.5.3:

  • Before: the TypeError appears on every pause and resume while the error page is shown.
  • After: no errors across repeated background/foreground cycles.
  • After, on a normal page with the bridge: pause and resume document listeners still fire on every transition.
  • npm run verify -w android passes.

The same change for next is in a separate PR.