Skip to content

Commit

Permalink
Introduce AMP Live List Cache Busting (ampproject#25295)
Browse files Browse the repository at this point in the history
* Use a random number as a URL parameter for live list refreshes
* JsonObject casting required
* Is a dict
* Add amp-live-list-random origin trial to gate adding a random identifier to each update of the live list
* Changes based on PR feedback
* Guard origin trial check in tri-state boolean
* Add dep-check allowance
* moved dep-check to correct segment
* Only check for origin trial if the list is enabled
  • Loading branch information
kristoferbaxter authored and Micajuine Ho committed Dec 27, 2019
1 parent 69687ba commit 5617a2a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
2 changes: 2 additions & 0 deletions build-system/test-configs/dep-check-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,8 @@ exports.rules = [
'src/service/origin-experiments-impl.js',
'extensions/amp-script/0.1/amp-script.js->' +
'src/service/origin-experiments-impl.js',
'extensions/amp-live-list/0.1/live-list-manager.js->' +
'src/service/origin-experiments-impl.js',
// For action macros.
'extensions/amp-action-macro/0.1/amp-action-macro.js->' +
'src/service/action-impl.js',
Expand Down
37 changes: 31 additions & 6 deletions extensions/amp-live-list/0.1/live-list-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@

import {Poller} from './poller';
import {Services} from '../../../src/services';
import {addParamToUrl} from '../../../src/url';
import {addParamsToUrl} from '../../../src/url';
import {dict} from '../../../src/utils/object';
import {fetchDocument} from '../../../src/document-fetcher';
import {getMode} from '../../../src/mode';
import {getServicePromiseForDoc} from '../../../src/service';
import {
installOriginExperimentsForDoc,
originExperimentsForDoc,
} from '../../../src/service/origin-experiments-impl';
import {startsWith} from '../../../src/string';
import {toArray} from '../../../src/types';
import {userAssert} from '../../../src/log';
Expand Down Expand Up @@ -77,6 +82,9 @@ export class LiveListManager {
/** @private @const {boolean} */
this.isTransformed_ = isDocTransformed(ampdoc.getRootNode());

/** @private {?boolean} */
this.enrolledInAppendRandomExperiment_ = null;

// Only start polling when doc is ready and when the doc is visible.
this.whenDocReady_().then(() => {
// Switch out the poller interval if we can find a lower one and
Expand Down Expand Up @@ -147,11 +155,17 @@ export class LiveListManager {
fetchDocument_() {
let url = this.url_;
if (this.latestUpdateTime_ > 0) {
url = addParamToUrl(
url,
'amp_latest_update_time',
String(this.latestUpdateTime_)
);
const parameters = this.enrolledInAppendRandomExperiment_
? dict({
'amp_latest_update_time': String(this.latestUpdateTime_),
// AMP Caches do not always evict entries from their caches.
// This experiment adds a random identifier to reduce cache hits for enrolled documents.
'amp_random': String(Math.random()),
})
: dict({
'amp_latest_update_time': String(this.latestUpdateTime_),
});
url = addParamsToUrl(url, parameters);
}

if (this.isTransformed_) {
Expand Down Expand Up @@ -279,6 +293,17 @@ export class LiveListManager {
// Polling may not be started yet if no live lists were registered by
// doc ready in LiveListManager's constructor.
if (liveList.isEnabled() && this.poller_ && this.ampdoc.isVisible()) {
if (this.enrolledInAppendRandomExperiment_ === null) {
// Origin Trial for cache busting requests for `amp-live-list`.
installOriginExperimentsForDoc(this.ampdoc);
originExperimentsForDoc(liveList.element)
.getExperiments()
.then(
trials =>
(this.enrolledInAppendRandomExperiment_ =
trials && trials.includes('amp-live-list-random'))
);
}
this.poller_.start();
}
}
Expand Down

0 comments on commit 5617a2a

Please sign in to comment.