Skip to content
Merged
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
16 changes: 13 additions & 3 deletions packages/core/src/transports/offline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { Envelope } from '../types/envelope';
import type { InternalBaseTransportOptions, Transport, TransportMakeRequestResponse } from '../types/transport';
import { debug } from '../utils/debug-logger';
import { envelopeContainsItemType } from '../utils/envelope';
import { isThenable } from '../utils/is';
import { safeDateNow } from '../utils/randomSafeContext';
import { parseRetryAfterHeader } from '../utils/ratelimit';
import { safeUnref } from '../utils/timer';
Expand Down Expand Up @@ -139,8 +140,15 @@ export function makeOfflineTransport<TO>(
}

try {
if (options.shouldSend && (await options.shouldSend(envelope)) === false) {
throw new Error('Envelope not sent because `shouldSend` callback returned false');
if (options.shouldSend) {
const decision = options.shouldSend(envelope);
// avoid extra microtask tick, as some hosts stop JS execution
// when the app goes to the background.
const shouldSend = isThenable(decision) ? await decision : decision;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

m: I think we have a similar issue with shouldQueue/options.shouldStore below. might be worth checking and adjusting while we're at it.

@isaacs isaacs Sep 4, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, yes, applied that as well.

I wonder if there's a way to make our linter catch this general pattern, where we unconditionally await something that could be a non-promise. 🤔 It could be annoying to be nagged to isThenable(x) everywhere, but I'm curious now how common this is. It's basically always going to be a correctness and/or performance improvement to avoid the await for sync values.


UPDATE: ok, threw a clanker at this question, and I'm now convinced it's not worth doing, so I'm ditching the idea.

  • It's not something we can easily do with oxlint, or even scripting against the tsgo compiler we use, since none of the type checking is easily pluggable.
  • eslint rules are syntax only, not pluggable type-aware rules.
  • we CAN do it with a standalone script that uses the JS typescript lib, which I had it do, and it found 26 cases where we have a return value that's awaited, and might not contain a then function. However, apart from these two being fixed in this PR, the others are all build-time or startup paths where it doesn't matter as much.
  • isThenable(x) ? await x : x everywhere would increase bundle size for little gain, and it is uglier to human eyes.
  • We can just be mindful of this when operating in the send() path, which we'll probably do just by copying the pattern that's fixed here anyway, so there's little added benefit.


if (shouldSend === false) {
throw new Error('Envelope not sent because `shouldSend` callback returned false');
}
}

const result = await transport.send(envelope);
Expand All @@ -163,7 +171,9 @@ export function makeOfflineTransport<TO>(
retryDelay = START_DELAY;
return result;
} catch (e) {
if (await shouldQueue(envelope, e as Error, retryDelay)) {
// do not unnecessarily await if it's not a Promise
const decision = shouldQueue(envelope, e as Error, retryDelay);
if (isThenable(decision) ? await decision : decision) {
// If this envelope was a retry, we want to add it to the front of the queue so it's retried again first.
if (isRetry) {
await store.unshift(envelope);
Expand Down
48 changes: 48 additions & 0 deletions packages/core/test/lib/transports/offline.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,54 @@ describe('makeOfflineTransport', () => {
expect(getCalls()).toEqual(['push']);
});

it('a synchronous shouldSend does not defer the send by a microtask', async () => {
vi.useFakeTimers();
onTestFinished(() => {
vi.useRealTimers();
});

const { store } = createTestStore();
const { getSendCount, baseTransport } = createTestTransport({ statusCode: 200 });
const transport = makeOfflineTransport(baseTransport)({
...transportOptions,
createStore: store,
shouldSend: () => true,
});

// Some hosts stop JS execution when the app goes to the background.
// They only send what the transport already got. So a synchronous
// `shouldSend` must not push the send into the next microtask.
const result = transport.send(ERROR_ENVELOPE);
expect(getSendCount()).toEqual(1);

await expect(result).resolves.toEqual({ statusCode: 200 });
});

it('a synchronous shouldStore does not defer the store by a microtask', async () => {
vi.useFakeTimers();
onTestFinished(() => {
vi.useRealTimers();
});

const { getCalls, store } = createTestStore();
const { getSendCount, baseTransport } = createTestTransport({ statusCode: 200 });
const transport = makeOfflineTransport(baseTransport)({
...transportOptions,
createStore: store,
shouldSend: () => false,
shouldStore: () => true,
});

// A synchronous `shouldSend` that says no throws in the same tick,
// so the envelope must reach the store before the host can stop JS
// execution.
const result = transport.send(ERROR_ENVELOPE);
expect(getCalls()).toEqual(['push']);

await expect(result).resolves.toEqual({});
expect(getSendCount()).toEqual(0);
});

it('should not store client report envelopes on send failure', async () => {
const { getCalls, store } = createTestStore();
const { getSendCount, baseTransport } = createTestTransport(new Error());
Expand Down
Loading