-
Notifications
You must be signed in to change notification settings - Fork 118
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
React SDK - Session Recorder - Original fetch args is modified causing application to crash #1326
Comments
@samuellawerentz if I understand you correctly changing to using these two args would work for you? (we'd have to test that doesn't have any other unexpected effect but it seems reasonable - cc @daibhin) |
This is our code const originalFetch = window.fetch
export const fetchInterceptor = () => {
window.fetch = async (...args) => {
const href = decodeURI(args[0]) // <- It breaks around here, as we are expecting a string as the first arg
// Replace region with the appropriate region from the state
if (href?.includes('{{region}}')) {
... some logic ...
args[0] = new URL(href.replace('region', region)
}
const response = await originalFetch(...args)
return response
}
} |
Yes, the first arg we expect it to be URL |
Ah... "It breaks around here, as we are expecting a string as the first arg" the first arg of fetch is not guaranteed to be a string from: https://developer.mozilla.org/en-US/docs/Web/API/Window/fetch#resource so we probably both have not ideal behaviour here :) just for clarity for the future traveller |
so , yes, when we wrap fetch a url+init becomes a request... which is in retrospect probably not as transparent to code as maybe we should be but any other dependency could make a valid Fetch call and your interceptor would see a |
we have to go really carefully with changing the wrapper are you ok in the meantime checking if args[0] is a string or a request and reading the href out of the request if necessary... it's an immediate fix for you and i'll update here when we've changed our behaviour (but it'll make your interceptor more robust regardless since someone else could make a valid fetch where args[0] is not a string) |
Yes, you're right, but we haven't faced a crash until now due to Sure, For now, I can do a manual check if the first object is string/object then act accordingly. But I have a question, why can't you forward the original params back to the Thank you @pauldambra for the time! |
Only two reasons... I'm out of the office at the moment is the most important for me 🤣 I definitely want to address this though! It just won't be as quick as we normally aim for, sorry! |
Hi @pauldambra we are not able to proceed with the fix that we decided, because it is not working.
The problem is, the first arg to our interceptor always comes as a Request object. This is because We invoke Now as you can see, the first arg is always I'm not sure if we can modify the url inside a request object as it is only readonly. let href = ''
// Retrieve the URL
if (typeof args[0] === 'string')
href = decodeURI(args[0])
else if (args[0] instanceof Request)
href = decodeURI(args[0].url)
// Business logic
href?.replace('{{region}}', region)
// Set the URL
if (typeof args[0] === 'string') args[0] = href
else if (args[0] instanceof Request)
args[0] = new Request(href, args[0]) // -> This is not possible, getting error here
const response = await originalFetch(...args)
return response So we got hit with this error in Chrome - Safari seemed to work fine. Upon researching we came across this thread -whatwg/fetch#1486 Looks like duplication of a request is not straight forward. Came across this fix by the ky library for the same issue - https://github.com/sindresorhus/ky/pull/451/files So can u let us know what to do next? Or if you have any simple workarounds? Right now we have reverted our changes and we are not able to use Posthog in produciton. Or let me know an approximate timeline when this will be taken up, so i can share it with my team. Thanks! |
i'm unexpectedly out of the office at the moment... i've opened #1351 to show the change we probably need to make is small - but i don't have my work head on so might be missing something @daibhin are you ok to pick this up? @samuellawerentz if you're using |
(I'm 99.999% sure that change is fine just mindful i've not checked it at all 😊) |
Totally! Thanks for the PR @pauldambra - I can take it from here. I'm also not in work mode this evening but will look at merging it tomorrow morning |
Thank you @pauldambra @daibhin! |
Hi @daibhin just checking if theres an update. Thanks! |
Hey @samuellawerentz, Paul is back today so I will catch up with him about #1351 |
That PR was merged as version 1.156.0. So, this should be fixed for you now 🤞 |
Ah, no, we're reverting PostHog/posthog#24471 I think that change makes the error I was worrying about, that if the first argument to fetch is a request object and we read the body then we pass along a request that a receiver can't use Will need to go way more carefully if we change this again, wrapping fetch is useful and filled with footguns |
Oh okay.. Yes, handling fetch is quite risky.. Please keep me updated.. Thank you! |
Bug description
We use the React SDK to initialize posthog. We have a
fetch
interceptor which modifies the request before the API call is made.After integrating with posthog, the
fetch
API is already modified and the args are not passed properly, because of this our interceptor is not able to function properly. The issue is with therecorder.js
file.TLDR; you are modifying the original fetch and not forwarding all the args in this format
...args
. And hence our fetch interceptor which expects two args, receive only one causing the app to crash.How to reproduce
window.fetch = ...
Please let me know, how to resolve this or what is the next step.
I'm attaching a screenshot of the
recorder.js
file for reference, where you are overriding the default fetch.I have already sent an email, but was not able to track it.
Debug info
No response
The text was updated successfully, but these errors were encountered: