Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions dist/web/pubnub.js
Original file line number Diff line number Diff line change
Expand Up @@ -6272,8 +6272,12 @@
// no `document`, and `fetch` cannot be reached through a fresh browsing context — return the context `fetch`
// directly. This is safe there: without a DOM there is no APM page script to monkey patch `fetch` in the
// first place. This environment check is what keeps the unconditional call in the constructor safe.
//
// `fetch` must be bound to the global scope: native `fetch` requires its `this` to be the `Window` /
// `WorkerGlobalScope`, and it is later invoked detached (`WebTransport.originalFetch(...)`). Without the bind
// a service worker throws "Failed to execute 'fetch' on 'WorkerGlobalScope': Illegal invocation".
if (typeof document === 'undefined' || !document.body)
return fetch;
return fetch.bind(globalThis);
let iframe = document.querySelector('iframe[name="pubnub-context-unpatched-fetch"]');
if (!iframe) {
iframe = document.createElement('iframe');
Expand All @@ -6284,7 +6288,7 @@
}
if (iframe.contentWindow)
return iframe.contentWindow.fetch.bind(iframe.contentWindow);
return fetch;
return fetch.bind(globalThis);
}
}
/**
Expand Down
2 changes: 1 addition & 1 deletion dist/web/pubnub.min.js

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions src/transport/web-transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,11 @@ export class WebTransport implements Transport {
// no `document`, and `fetch` cannot be reached through a fresh browsing context — return the context `fetch`
// directly. This is safe there: without a DOM there is no APM page script to monkey patch `fetch` in the
// first place. This environment check is what keeps the unconditional call in the constructor safe.
if (typeof document === 'undefined' || !document.body) return fetch;
//
// `fetch` must be bound to the global scope: native `fetch` requires its `this` to be the `Window` /
// `WorkerGlobalScope`, and it is later invoked detached (`WebTransport.originalFetch(...)`). Without the bind
// a service worker throws "Failed to execute 'fetch' on 'WorkerGlobalScope': Illegal invocation".
if (typeof document === 'undefined' || !document.body) return fetch.bind(globalThis);

let iframe = document.querySelector<HTMLIFrameElement>('iframe[name="pubnub-context-unpatched-fetch"]');

Expand All @@ -457,6 +461,6 @@ export class WebTransport implements Transport {
}

if (iframe.contentWindow) return iframe.contentWindow.fetch.bind(iframe.contentWindow);
return fetch;
return fetch.bind(globalThis);
}
}
Loading