If your environment requires custom request logic (e.g., adding a JWT Authorization header for an authenticated proxy), you can provide handleSendEvents and handleFetchConfig callbacks.
Key Behaviors
- URL Resolution: The SDK handles URL resolution (using
trackServerUrl and configServerUrl), batching, retries, and compression. Your callback's only job is to execute the request and return a Response. - Retries: The SDK calls your callback once per logical attempt. Do not implement your own retry logic; the SDK manages retries and backoff around your callback.
- Error Handling: Non-2xx responses or rejected promises from your callback are handled by the SDK's existing retry and logging logic.
- Web Worker Support: If
useWebWorker: true is set, the SDK performs compression off the main thread and delegates only the network call back to the main thread to run your callback. - Page Exit: For authenticated proxies, the SDK routes the final batch through your callback with
keepalive: true instead of using navigator.sendBeacon (which cannot carry custom headers).
import * as sessionReplay from '@amplitude/session-replay-browser';
import type { SendEventsRequest, FetchConfigRequest } from '@amplitude/session-replay-browser';
sessionReplay.init(API_KEY, {
deviceId,
sessionId,
trackServerUrl: 'https://my-proxy.example.com/sr-events',
configServerUrl: 'https://my-proxy.example.com/sr-config',
handleSendEvents: async ({ url, method, headers, body, keepalive }: SendEventsRequest) => {
return fetch(url, {
method,
headers: { ...headers, Authorization: `Bearer ${getJwt()}` },
body,
keepalive,
});
},
handleFetchConfig: async ({ url, method, headers, signal }: FetchConfigRequest) => {
return fetch(url, {
method,
headers: { ...headers, Authorization: `Bearer ${getJwt()}` },
signal,
});
},
});