-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
amp-bind: Fix state/ready race condition #21033
Changes from 5 commits
8e13ca1
d3d4c61
6a7d157
7ad14fe
b1df7ac
dd94adc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,9 +50,27 @@ export class AmpState extends AMP.BaseElement { | |
toggle(this.element, /* opt_display */ false); | ||
this.element.setAttribute('aria-hidden', 'true'); | ||
|
||
// Don't parse or fetch in prerender mode. | ||
const viewer = Services.viewerForDoc(this.getAmpDoc()); | ||
viewer.whenFirstVisible().then(() => this.initialize_()); | ||
const {element} = this; | ||
if (element.hasAttribute('overridable')) { | ||
Services.bindForDocOrNull(element).then(bind => { | ||
devAssert(bind); | ||
bind.addOverridableKey(element.getAttribute('id')); | ||
}); | ||
} | ||
// Parse child <script> tag and/or fetch JSON from `src` attribute. | ||
// The latter is allowed to overwrite the former. | ||
const {children} = element; | ||
if (children.length > 0) { | ||
// Bind relies on this happening synchronously in buildCallback(). | ||
this.parseAndUpdate_(); | ||
} | ||
if (this.element.hasAttribute('src')) { | ||
this.fetchAndUpdate_(/* isInit */ true); | ||
} | ||
|
||
this.registerAction('refresh', () => { | ||
this.fetchAndUpdate_(/* isInit */ false, /* opt_refresh */ true); | ||
}, ActionTrust.HIGH); | ||
} | ||
|
||
/** @override */ | ||
|
@@ -75,40 +93,15 @@ export class AmpState extends AMP.BaseElement { | |
return true; | ||
} | ||
|
||
/** @private */ | ||
initialize_() { | ||
const {element} = this; | ||
if (element.hasAttribute('overridable')) { | ||
Services.bindForDocOrNull(element).then(bind => { | ||
devAssert(bind, 'Bind service can not be found.'); | ||
bind.makeStateKeyOverridable(element.getAttribute('id')); | ||
}); | ||
} | ||
// Parse child script tag and/or fetch JSON from endpoint at `src` | ||
// attribute, with the latter taking priority. | ||
const {children} = element; | ||
if (children.length > 0) { | ||
this.parseChildAndUpdateState_(); | ||
} | ||
if (this.element.hasAttribute('src')) { | ||
this.fetchAndUpdate_(/* isInit */ true); | ||
} | ||
this.registerAction('refresh', () => { | ||
this.fetchAndUpdate_(/* isInit */ false, /* opt_refresh */ true); | ||
}, ActionTrust.HIGH); | ||
} | ||
|
||
|
||
/** | ||
* Parses JSON in child script element and updates state. | ||
* Parses JSON in child <script> and updates state. | ||
* @private | ||
*/ | ||
parseChildAndUpdateState_() { | ||
parseAndUpdate_() { | ||
const TAG = this.getName_(); | ||
const {children} = this.element; | ||
if (children.length != 1) { | ||
this.user().error( | ||
TAG, 'Should contain exactly one <script> child.'); | ||
this.user().error(TAG, 'Should contain exactly one <script> child.'); | ||
return; | ||
} | ||
const firstChild = children[0]; | ||
|
@@ -118,8 +111,7 @@ export class AmpState extends AMP.BaseElement { | |
return; | ||
} | ||
const json = tryParseJson(firstChild.textContent, e => { | ||
this.user().error( | ||
TAG, 'Failed to parse state. Is it valid JSON?', e); | ||
this.user().error(TAG, 'Failed to parse state. Is it valid JSON?', e); | ||
}); | ||
this.updateState_(json, /* isInit */ true); | ||
} | ||
|
@@ -154,10 +146,11 @@ export class AmpState extends AMP.BaseElement { | |
*/ | ||
fetchAndUpdate_(isInit, opt_refresh) { | ||
const ampdoc = this.getAmpDoc(); | ||
return this.fetch_(ampdoc, this.element, isInit, opt_refresh) | ||
.then(json => { | ||
this.updateState_(json, isInit); | ||
}); | ||
// Don't fetch in prerender mode. | ||
const viewer = Services.viewerForDoc(this.getAmpDoc()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fetchAndUpdate_ is also called in the registered refresh action method call. We should set the viewer service as a private property on the buildCallback so that we don't need to get the service everything time this method is called. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd prefer not to do this optimization due to the readability and testing cost. I don't think it makes a perf difference in practice. |
||
return viewer.whenFirstVisible() | ||
.then(() => this.fetch_(ampdoc, this.element, isInit, opt_refresh)) | ||
.then(json => this.updateState_(json, isInit)); | ||
} | ||
|
||
/** | ||
|
@@ -170,12 +163,14 @@ export class AmpState extends AMP.BaseElement { | |
return; | ||
} | ||
const id = userAssert(this.element.id, '<amp-state> must have an id.'); | ||
const state = /** @type {!JsonObject} */ (map()); | ||
state[id] = json; | ||
Services.bindForDocOrNull(this.element).then(bind => { | ||
devAssert(bind, 'Bind service can not be found.'); | ||
bind.setState(state, | ||
/* opt_skipEval */ isInit, /* opt_isAmpStateMutation */ !isInit); | ||
devAssert(bind); | ||
const state = /** @type {!JsonObject} */ (map()); | ||
state[id] = json; | ||
// As a rule, initialization should skip evaluation. | ||
// If we're not initializing then this must be a mutation, so we must | ||
// skip <amp-state> evaluation to prevent update cycles. | ||
bind.setState(state, /* skipEval */ isInit, /* skipAmpState */ !isInit); | ||
}); | ||
} | ||
|
||
|
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 should have made this change when I added refresh support for amp-state but we should only register the action if the element has a src attribute. We should move this block in the if statement above.
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 call. Though it's possible for the element to not have a
src
attribute at buildCallback but have one later (via amp-bind), so I added auserAssert
here instead.