-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
fix(core): Ensure fill
only patches functions
#15632
Conversation
@@ -61,7 +61,7 @@ export function supportsDOMException(): boolean { | |||
* @returns Answer to the given question. | |||
*/ | |||
export function supportsHistory(): boolean { | |||
return 'history' in WINDOW; | |||
return 'history' in WINDOW && !!WINDOW.history; |
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.
Improved this check so that if history was undefined
or null
we'd also return false
here
size-limit report 📦
|
I will rework this to more generally fix the wird behaviour in our |
|
||
expect(WINDOW.history).toEqual({ | ||
replaceState: expect.any(Function), // patched function | ||
pushState: undefined, // unpatched |
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.
prior to this PR, pushState
would have become a Function
, resulting in the wrapper function calling undefined.apply
on the "original function".
window.history
propertiesfill
only patches functions
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, thanks for guarding this
This PR adds a guard to our
fill
utility that we use to instrument/wrap methods defined on an object. However, due to a misleading type cast infill
we didn't check if the name of the method to be patched actually corresponded to a function on an object.This was surfaced via #15552 where we'd call
fill
onwindow.history(pushState|replaceState)
without checking if these two methods were actually available or functions.Note: I initially solved this on the
instrumentHistory
level but then noticed that this is a more general bug infill
. Therefore I added some history-specific tests as well but I'd rather keep them than removing them. Fun fact: The more general fix saves ~8 Bytes of bundle size compared to my initial history-based fix 😅closes #15552