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
tracker updates #1272
tracker updates #1272
Conversation
if (!persist) { | ||
fetch(endpoint, options).then(res => res.text()).then(resText => { cache = resText }); | ||
} else { | ||
options['keepalive'] = true; |
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.
I think, all the requests should use the keepalive
option. Previously, the other requests were sent using XHR, therefore those requests were not persisted.
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.
I think this should be the structure for fetch requests.
Whether to cache the response could also be checked by type === 'pageview'
, but the previous one is probably better if any other responses need to cached in the future.
@@ -73,17 +58,22 @@ import { removeTrailingSlash } from '../lib/url'; | |||
return a; | |||
}; | |||
|
|||
const collect = (type, payload) => { | |||
const collect = (type, payload, persist = false) => { |
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.
const collect = (type, payload, persist = false) => { | |
const collect = (type, payload, cacheResponse = false) => { |
if (cache) headers['x-umami-cache'] = cache; | ||
let options = { | ||
method: 'POST', | ||
body: JSON.stringify({type, payload}), |
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.
body: JSON.stringify({type, payload}), | |
body: JSON.stringify({type, payload}), | |
keepalive: true, |
body: JSON.stringify({type, payload}), | ||
headers | ||
}; | ||
if (!persist) { |
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.
if (!persist) { | |
if (cacheResponse) { |
if (!persist) { | ||
fetch(endpoint, options).then(res => res.text()).then(resText => { cache = resText }); | ||
} else { | ||
options['keepalive'] = true; |
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.
options['keepalive'] = true; |
assign(getPayload(), { | ||
event_type, | ||
event_value, | ||
}), |
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.
}), | |
}) |
event_type, | ||
event_value, | ||
}), | ||
true |
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.
true |
@@ -93,7 +83,7 @@ import { removeTrailingSlash } from '../lib/url'; | |||
website: uuid, | |||
url, | |||
referrer, | |||
}), | |||
}) |
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.
}) | |
}), | |
true |
The suggested changes differs a lot from the initial PR, which keeps exactly the same behaviour as now: Standard requests not resistant to location change/exit, like in XHR, and the answer always cached locally |
Yeah, okay. I'll probably work this on another PR if @mikecao is interested. |
I'm still undecided on this because the reason we went with XHR is to support older browsers. I haven't read up on the state of fetch support yet. Maybe we can create a separate tracker to support older browsers? |
I understand, but Reference: https://developer.mozilla.org/en-US/docs/Web/API/fetch#browser_compatibility |
Understood, actually went with fetch because it was introduced in tracker in a previous commit. I also believe that kind of error wouldn't happen (#1246), not sure though. |
As
fetch
was used to replacenavigator.sendBeacon
, it might be good to handle all the requests with it.It's more convenient than XHR.
If you're okay, this PR :
collect()
)like it is currently the case with link events (and it can probably be useful for possible future uses)
sendEvent()
as in other functions