Skip to content

fix: drop benign Capacitor iOS teardown exception from error tracking#469

Draft
posthog-eu[bot] wants to merge 1 commit into
devfrom
posthog-code/filter-capacitor-teardown-error
Draft

fix: drop benign Capacitor iOS teardown exception from error tracking#469
posthog-eu[bot] wants to merge 1 commit into
devfrom
posthog-code/filter-capacitor-teardown-error

Conversation

@posthog-eu

@posthog-eu posthog-eu Bot commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

Summary

A single iOS user's error tracking flagged a brand-new TypeError: undefined is not an object (evaluating 'window.webkit.messageHandlers'). It isn't thrown by our code — it comes from Capacitor's injected native-bridge.js: on pagehide it runs sendPageHideMessagesendDataToNative, which posts to window.webkit.messageHandlers. While the WKWebView tears down during navigation that object is already undefined, so the access throws and posthog-js captures it as an unhandled exception.

This adds a narrow before_send rule in lib/posthog/shared.ts (alongside the existing script-error / extension-RPC / rrweb noise filters) that drops the event only when the exact window.webkit.messageHandlers message co-occurs with a Capacitor bridge frame (sendPageHideMessage / sendDataToNative). Neither symbol exists anywhere in our code, so genuine exceptions stay visible.

Why

The page is already unloading when this fires, so no user-facing flow breaks — it's a benign teardown race in a third-party bridge that just clutters error tracking. Chose the before_send filter over a Capacitor dependency bump to match the repo's established noise-suppression pattern and avoid a native-shell upgrade that can't be verified in CI.

Verification

Confirmed the fingerprint in error tracking: 1 occurrence / 1 user, iOS Mobile Safari (WKWebView), on a past-papers PDF page. Stack frames carry sendPageHideMessage and sendDataToNative with the window.webkit.messageHandlers message — both matched by the new filter. grep confirms sendPageHideMessage, sendDataToNative, and window.webkit.messageHandlers appear nowhere in the app source.


Created with PostHog Code from an inbox report.

spent a lot of water and tokens to review your slop

Greptile Summary

This PR adds a targeted PostHog filter for benign Capacitor iOS teardown errors. The main changes are:

  • Adds detection for TypeError events containing window.webkit.messageHandlers.
  • Requires a Capacitor bridge stack frame such as sendPageHideMessage or sendDataToNative before dropping the event.
  • Keeps chained exception events when any entry does not match the benign teardown pattern.

Confidence Score: 5/5

Safe to merge with minimal risk.

The change is isolated to PostHog event filtering and uses narrow matching on exception type, message, and Capacitor bridge frames.

No files require special attention.

T-Rex T-Rex Logs

What T-Rex did

  • The harness ran to completion with exit code 0 and logged all PASS assertions, and a reproducible harness was saved for future runs.
  • The source search confirmed matches exist only in lib/posthog/shared.ts.

View all artifacts

T-Rex Ran code and verified through T-Rex

Important Files Changed

Filename Overview
lib/posthog/shared.ts Adds a narrowly scoped PostHog before_send filter for Capacitor iOS teardown exceptions while preserving existing error-noise filters.

Sequence Diagram

%%{init: {'theme': 'neutral'}}%%
sequenceDiagram
participant WK as iOS WKWebView
participant Cap as Capacitor native-bridge.js
participant PH as posthog-js capture_exceptions
participant BS as before_send filter
participant ET as PostHog error tracking

WK->>Cap: pagehide during teardown
Cap->>Cap: "sendPageHideMessage -> sendDataToNative"
Cap-->>PH: TypeError for window.webkit.messageHandlers
PH->>BS: $exception event with exception_list
BS->>BS: require TypeError + message + bridge frame
alt all exception entries match Capacitor teardown
    BS-->>PH: return null
    PH--xET: event dropped
else any genuine chained exception remains
    BS-->>PH: return event
    PH->>ET: send exception event
end
Loading
%%{init: {'theme': 'base', 'themeVariables': {"darkMode": true, "background": "#0d1117", "primaryColor": "#21262d", "primaryTextColor": "#e6edf3", "primaryBorderColor": "#8b949e", "lineColor": "#8b949e", "textColor": "#e6edf3", "edgeLabelBackground": "#161b22", "actorBkg": "#21262d", "actorBorder": "#8b949e", "actorTextColor": "#e6edf3", "actorLineColor": "#8b949e", "signalColor": "#8b949e", "signalTextColor": "#e6edf3", "noteBkgColor": "#373320", "noteBorderColor": "#d4a72c", "noteTextColor": "#f0e6c0", "labelBoxBkgColor": "#21262d", "labelBoxBorderColor": "#8b949e", "labelTextColor": "#e6edf3", "loopTextColor": "#e6edf3", "activationBkgColor": "#30363d", "activationBorderColor": "#8b949e"}}}%%
sequenceDiagram
participant WK as iOS WKWebView
participant Cap as Capacitor native-bridge.js
participant PH as posthog-js capture_exceptions
participant BS as before_send filter
participant ET as PostHog error tracking

WK->>Cap: pagehide during teardown
Cap->>Cap: "sendPageHideMessage -> sendDataToNative"
Cap-->>PH: TypeError for window.webkit.messageHandlers
PH->>BS: $exception event with exception_list
BS->>BS: require TypeError + message + bridge frame
alt all exception entries match Capacitor teardown
    BS-->>PH: return null
    PH--xET: event dropped
else any genuine chained exception remains
    BS-->>PH: return event
    PH->>ET: send exception event
end
Loading

Reviews (1): Last reviewed commit: "fix: drop benign Capacitor iOS teardown ..." | Re-trigger Greptile

Capacitor's injected native-bridge.js registers a pagehide listener
(sendPageHideMessage -> sendDataToNative) that reads
window.webkit.messageHandlers. During WKWebView teardown on navigation
that object is already undefined, throwing a TypeError that posthog-js
captures as an unhandled exception. The page is already unloading, so no
user-facing flow breaks — it is pure teardown-race noise.

Add a narrow before_send rule matching the exact
window.webkit.messageHandlers access message together with a Capacitor
bridge frame (sendPageHideMessage / sendDataToNative), neither of which
exists in our own code, so real exceptions stay visible.

Generated-By: PostHog Code
Task-Id: 574e2626-3298-407b-9abf-17094dcdf5ce
@vercel

vercel Bot commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
examcooker-dev Error Error Jul 16, 2026 11:01am

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants