-
Notifications
You must be signed in to change notification settings - Fork 135
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
improve Segment.io retries #714
Conversation
🦋 Changeset detectedLatest commit: f0b02ed The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
60a71d5
to
59b8bd2
Compare
@@ -10,13 +10,13 @@ | |||
export const onPageLeave = (cb: (...args: unknown[]) => void) => { | |||
let unloaded = false // prevents double firing if both are supported | |||
|
|||
// using document instead of window because of bug affecting browsers before safari 14 (detail in footnotes https://caniuse.com/?search=visibilitychange) | |||
document.addEventListener('pagehide', () => { | |||
window.addEventListener('pagehide', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch on this!
// Buffer may already have items if they were previously stored in localStorage. | ||
// Start flushing them immediately. | ||
if (buffer.todo) { | ||
scheduleFlush(flushing, buffer, segmentio, scheduleFlush) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice! Not much code for a very nice improvement.
This PR improves retry handling for the
Segment.io
plugin. It also fixes an issue wherePersistedPriorityQueue
was no longer storing events inlocalStorage
onpagehide
.Fix
PersistedPriorityQueue
The PersistedPriorityQueue listens for the
pagehide
event then stores all of its events in localStorage. Currently, it listens for thepagehide
event to be emitted ondocument
, butpagehide
is aWindow
event. This caused the queue to stop working withpagehide
.Apply backoff to segment retries
Currently, when fetch errors are encountered events are pushed back onto the buffer via
buffer.push(ctx)
. This does not apply any backoff, so this is updated tobuffer.pushWithBackoff(ctx)
. This causes the event to be part of the buffer'stodo
length. Theschedule-flush
function already checksbuffer.todo > 0
, so we're covered here.Store canceled events
In the scenario where the user navigates away from the page between while there are pending HTTP requests, those events are not currently persisted in localStorage. This change keeps track of events that are 'in-flight' and adds them to the persisted priority queue.
Testing
I was able to add tests for this using playwright. Also tested manually with Chrome and Firefox.
[x] I've included a changeset (psst. run
yarn changeset
. Read about changesets here).