Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: Load failed when page is unloaded before posthog events are finished sending #1487

Open
davidbarratt opened this issue Oct 21, 2024 · 2 comments

Comments

@davidbarratt
Copy link

We are getting thousands of "TypeError: Load failed" errors in Sentry and this appears to be because PostHog has a request in flight while we are redirecting the user to an external URL.

It looks like this is because XMLHttpRequest is preferred over fetch.

posthog-js/src/request.ts

Lines 202 to 216 in 38744a5

// We add the transports in order of preference
if (XMLHttpRequest) {
AVAILABLE_TRANSPORTS.push({
transport: 'XHR',
method: xhr,
})
}
if (fetch) {
AVAILABLE_TRANSPORTS.push({
transport: 'fetch',
method: _fetch,
})
}

Indeed, XHR is the default transport:

const transport = options.transport ?? 'XHR'

From my understanding, XMLHttpRequest does not have a mechanism like keepalive that will not cancel the request when the page is unloaded.

Fetch is available on every major supported browser. Could fetch be made the default? If not, could an option be provided to make it the default for our instance?

Alternatively, it could be helpful to have a method that returns a promise that resolves when all the pending events have been sent so we can block the redirect on that request, but that seems unnecessary with keepalive.

@pauldambra
Copy link
Member

Hey,

We're currently testing switching the default to fetch

If you set api_transport: 'fetch', then that's switch the default for you.

@davidbarratt
Copy link
Author

Oh dope. Thanks! I will do that!

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

No branches or pull requests

2 participants