-
Notifications
You must be signed in to change notification settings - Fork 27.4k
refactor($browser): wrap Get(history.state) in try/catch #10369
Conversation
Reportedly, MSIE can throw under certain conditions when fetching this attribute. Will confirm before landing. Fixes angular#10367
CLAs look good, thanks! |
It would be totally awesome to try to narrow down "certain conditions" IMO |
Yeah --- I'll probably bug mr. terlson about it later (but I feel bad about doing that, which is why I really need to find more IE developers to bug with this stuff :>) maybe there's something on their issue tracker |
FWIW I wasn't able to reproduce in SL, but that doesn't mean much --- the IE feedback site is too hard to find anything on, so it's gonna come down to asking I think |
@@ -235,10 +235,22 @@ function Browser(window, document, $log, $sniffer) { | |||
|
|||
// This variable should be used *only* inside the cacheState function. | |||
var lastCachedState = null; | |||
function getCurrentState() { | |||
try { | |||
var state = history.state; |
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.
Curious: Why not use window
?
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.
we cache history
anyways, might as well use it
@caitp any progress with this? Apparently we are experiencing this issue in one of our apps as well. |
try the patch and see if it fixes it, i have no idea how we test this since nobody has produced a repro scenario |
Yeah, I found out a couple of days ago that we've been using this patch for a while now in some project. @donataswix any chance you can post a reproduce of this issue? |
does that mean the patch solved some issue, or it's still causing problems? |
Yes, this patch solved the issue. Hopefully @donataswix will be able to add more details tomorrow. |
The patch solved the issue, I'll try to make reproducible example later today or at least figure out some more details. |
@donataswix - that would be extremely helpful. Thanks |
For better or worse I can no longer reproduce this no matter what I try. So even though that patch helped at the time, it's no longer necessary for us. Although, I'm not sure I want to remove it from our build... If anyone's interested our setup has dynamically created iframe where our app is loaded. There is a lot of code that runs before ours, and it seems that that code was responsible for creating preconditions for bug to appear. |
function getCurrentState() { | ||
try { | ||
var state = history.state; | ||
if (state === void 0) { |
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.
Perhaps we should change isDefined()
to use this method of testing for undefined
and use it here?
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.
Since history.state
is either going to be a valid object, undefined or null, or it is going to throw an error we can simply do:
function getCurrentState() {
try {
return history.state || null;
} catch(e) {
return null;
}
}
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.
It's not going to be either a valid object, undefined or null, eg https://dxr.mozilla.org/mozilla-central/source/dom/webidl/History.webidl#18
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.
could be "false", could be "0", could be the empty string, etc
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.
technically, it can legally be undefined
, too... so hmm
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.
Looks like we are pretending that it will never be undefined
: https://github.com/angular/angular.js/blob/master/src/ng/browser.js#L159-L164
Given that whatever we do here it is not going to be correct, we should keep the behaviour as close to what it is currently, so can we simply go with the following? function getCurrentState() {
try {
return history.state;
} catch (e) {
// MSIE can reportedly throw when there is no state (UNCONFIRMED).
return null;
}
}
function cacheState() {
cachedState = getCurrentState();
cachedState = isUndefined(cachedState) ? null : cachedState;
} Is that OK with you @caitp ? |
We also are experiencing this issue. We tried the patch and didn't work (it didn't throw an exception). To solve we did like this (when we load the page): window.history._state = null;
Object.defineProperty(window.history, 'state', {
get: function() { return window.history._state; },
set: function(state) { window.history._state = state; }
}); |
@dbabaioff - when you say it didn't throw an exception with this patch, do you mean that you get the error message without an error being thrown? Are you able to create a reproduction of this that we could add to our unit tests? |
@petebacondarwin - yes - I get the error message ("SCRIPT16389: Unspecified error .") without an error being throw. I'll try to create a reproduction of this. |
@dbabaioff - did you ever manage to create a reproduction of this without an error being thrown? |
Reportedly, MSIE can throw under certain conditions when fetching this attribute. We don't have a reliable reproduction for this but it doesn't do any real harm to wrap access to this variable in a try-catch block. Fixes angular#10367 Closes angular#10369
Reportedly, MSIE can throw under certain conditions when fetching this attribute. We don't have a reliable reproduction for this but it doesn't do any real harm to wrap access to this variable in a try-catch block. Fixes angular#10367 Closes angular#10369
@dbabaioff yes! it worked for me like a charm! Thanks!!! |
Reportedly, MSIE can throw under certain conditions when fetching this attribute.
Will confirm before landing.
Fixes #10367