Bug 1516034 - Use non-prerendered version of HTML for DiscoveryStream new tab (#4672)
This commit is contained in:
Родитель
d0365c3303
Коммит
8a3f3c3125
|
@ -93,6 +93,8 @@ for (const src of ${JSON.stringify(scripts, null, 2)}) {
|
|||
script.src = src;
|
||||
}
|
||||
</script>`;
|
||||
// Comment to indicate if prerendered version of html is used. See Bug 1516034
|
||||
const isPrerenderedStatus = "<!-- Prerendered version html not shown -->";
|
||||
return `<!doctype html>
|
||||
<html lang="${options.locale}" dir="${options.direction}">
|
||||
<head>
|
||||
|
@ -108,6 +110,7 @@ for (const src of ${JSON.stringify(scripts, null, 2)}) {
|
|||
<div id="snippets-container">
|
||||
<div id="snippets"></div>
|
||||
</div>${options.noscripts ? "" : scriptTag}
|
||||
${isPrerendered ? "" : isPrerenderedStatus}
|
||||
</body>
|
||||
</html>
|
||||
`;
|
||||
|
|
|
@ -28,11 +28,22 @@ class _PrerenderData {
|
|||
return result.concat(next.oneOf);
|
||||
} else if (next && next.indexedDB) {
|
||||
return result.concat(next.indexedDB);
|
||||
} else if (next && next.jsonPrefs) {
|
||||
return result.concat(next.jsonPrefs);
|
||||
}
|
||||
throw new Error("Your validation configuration is not properly configured");
|
||||
}, []);
|
||||
}
|
||||
|
||||
_isPrefEnabled(prefObj) {
|
||||
try {
|
||||
let data = JSON.parse(prefObj);
|
||||
return data && data.enabled;
|
||||
} catch (e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
arePrefsValid(getPref, indexedDBPrefs) {
|
||||
for (const prefs of this.validation) {
|
||||
// {oneOf: ["foo", "bar"]}
|
||||
|
@ -45,6 +56,13 @@ class _PrerenderData {
|
|||
if (anyModifiedPrefs) {
|
||||
return false;
|
||||
}
|
||||
// {jsonPrefs: ["foo", "bar"]}
|
||||
} else if (prefs && prefs.jsonPrefs) {
|
||||
const isPrefModified =
|
||||
prefs.jsonPrefs.some(name => this._isPrefEnabled(getPref(name)) !== this.initialPrefs[name].enabled);
|
||||
if (isPrefModified) {
|
||||
return false;
|
||||
}
|
||||
// "foo"
|
||||
} else if (getPref(prefs) !== this.initialPrefs[prefs]) {
|
||||
return false;
|
||||
|
@ -64,6 +82,7 @@ this.PrerenderData = new _PrerenderData({
|
|||
"feeds.section.highlights": true,
|
||||
"sectionOrder": "topsites,topstories,highlights",
|
||||
"collapsed": false,
|
||||
"discoverystream.config": {"enabled": false},
|
||||
},
|
||||
// Prefs listed as invalidating will prevent the prerendered version
|
||||
// of AS from being used if their value is something other than what is listed
|
||||
|
@ -82,6 +101,9 @@ this.PrerenderData = new _PrerenderData({
|
|||
// If any component has the following preference set to `true` it will
|
||||
// invalidate the prerendered version.
|
||||
{indexedDB: ["collapsed"]},
|
||||
// For below prefs, parse value to check enabled property. If enabled property
|
||||
// differs from initial prefs enabled value, prerendering cannot be used
|
||||
{jsonPrefs: ["discoverystream.config"]},
|
||||
],
|
||||
initialSections: [
|
||||
{
|
||||
|
|
|
@ -94,6 +94,33 @@ describe("_PrerenderData", () => {
|
|||
|
||||
assert.isFalse(instance.arePrefsValid(getPrefs, [{collapsed: true}]));
|
||||
});
|
||||
it("should return false if any of jsonPrefs group enabled value not matches", () => {
|
||||
FAKE_PREFS = {foo: true, bar: "{\"enabled\": true}", baz: "{\"enabled\": true}"};
|
||||
const instance = new _PrerenderData({
|
||||
initialPrefs: {foo: true, bar: {"enabled": true}, baz: {"enabled": false}},
|
||||
validation: ["foo", {jsonPrefs: ["baz", "bar"]}],
|
||||
});
|
||||
|
||||
assert.isFalse(instance.arePrefsValid(getPrefs));
|
||||
});
|
||||
it("should return false if any of jsonPrefs group doesn't have valid json", () => {
|
||||
FAKE_PREFS = {foo: true, bar: "{\"enabled\": true}", baz: {}};
|
||||
const instance = new _PrerenderData({
|
||||
initialPrefs: {foo: true, bar: {"enabled": true}, baz: {"enabled": false}},
|
||||
validation: ["foo", {jsonPrefs: ["baz", "bar"]}],
|
||||
});
|
||||
|
||||
assert.isFalse(instance.arePrefsValid(getPrefs));
|
||||
});
|
||||
it("should return true if all of jsonPrefs group enabled value matches", () => {
|
||||
FAKE_PREFS = {foo: true, bar: "{\"enabled\": true}", baz: "{\"enabled\": false}"};
|
||||
const instance = new _PrerenderData({
|
||||
initialPrefs: {foo: true, bar: {"enabled": true}, baz: {"enabled": false}},
|
||||
validation: ["foo", {jsonPrefs: ["baz", "bar"]}],
|
||||
});
|
||||
|
||||
assert.isTrue(instance.arePrefsValid(getPrefs));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
|
|
@ -42,6 +42,12 @@ describe("PrefsFeed", () => {
|
|||
overrider.restore();
|
||||
sandbox.restore();
|
||||
});
|
||||
|
||||
function setFakePrefsWithInitialValue() {
|
||||
Object.keys(initialPrefs).forEach(name => FAKE_PREFS.set(name,
|
||||
typeof(initialPrefs[name]) === "object" ? JSON.stringify(initialPrefs[name]) : initialPrefs[name]));
|
||||
}
|
||||
|
||||
it("should set a pref when a SET_PREF action is received", () => {
|
||||
feed.onAction(ac.SetPref("foo", 2));
|
||||
assert.calledWith(feed._prefs.set, "foo", 2);
|
||||
|
@ -84,14 +90,13 @@ describe("PrefsFeed", () => {
|
|||
assert.calledOnce(feed._setPrerenderPref);
|
||||
});
|
||||
it("should set prerender pref to true if prefs match initial values", async () => {
|
||||
Object.keys(initialPrefs).forEach(name => FAKE_PREFS.set(name, initialPrefs[name]));
|
||||
|
||||
setFakePrefsWithInitialValue();
|
||||
await feed._setPrerenderPref();
|
||||
|
||||
assert.calledWith(feed._prefs.set, PRERENDER_PREF_NAME, true);
|
||||
});
|
||||
it("should set prerender pref to false if a pref does not match its initial value", async () => {
|
||||
Object.keys(initialPrefs).forEach(name => FAKE_PREFS.set(name, initialPrefs[name]));
|
||||
setFakePrefsWithInitialValue();
|
||||
FAKE_PREFS.set("showSearch", false);
|
||||
|
||||
await feed._setPrerenderPref();
|
||||
|
@ -99,7 +104,7 @@ describe("PrefsFeed", () => {
|
|||
assert.calledWith(feed._prefs.set, PRERENDER_PREF_NAME, false);
|
||||
});
|
||||
it("should set prerender pref to true if indexedDB prefs are unchanged", async () => {
|
||||
Object.keys(initialPrefs).forEach(name => FAKE_PREFS.set(name, initialPrefs[name]));
|
||||
setFakePrefsWithInitialValue();
|
||||
feed._storage.getAll.resolves([{collapsed: false}, {collapsed: false}]);
|
||||
|
||||
await feed._setPrerenderPref();
|
||||
|
@ -107,7 +112,7 @@ describe("PrefsFeed", () => {
|
|||
assert.calledWith(feed._prefs.set, PRERENDER_PREF_NAME, true);
|
||||
});
|
||||
it("should set prerender pref to false if a indexedDB pref changed value", async () => {
|
||||
Object.keys(initialPrefs).forEach(name => FAKE_PREFS.set(name, initialPrefs[name]));
|
||||
setFakePrefsWithInitialValue();
|
||||
FAKE_PREFS.set("showSearch", false);
|
||||
feed._storage.getAll.resolves([{collapsed: false}, {collapsed: true}]);
|
||||
|
||||
|
@ -154,7 +159,7 @@ describe("PrefsFeed", () => {
|
|||
});
|
||||
it("should set the prerender pref to false if a pref in invalidatingPrefs is changed from its original value", () => {
|
||||
sandbox.stub(feed, "_setPrerenderPref");
|
||||
Object.keys(initialPrefs).forEach(name => FAKE_PREFS.set(name, initialPrefs[name]));
|
||||
setFakePrefsWithInitialValue();
|
||||
|
||||
feed._prefs.set("showSearch", false);
|
||||
feed.onPrefChanged("showSearch", false);
|
||||
|
@ -162,7 +167,7 @@ describe("PrefsFeed", () => {
|
|||
});
|
||||
it("should set the prerender pref back to true if the invalidatingPrefs are changed back to their original values", () => {
|
||||
sandbox.stub(feed, "_setPrerenderPref");
|
||||
Object.keys(initialPrefs).forEach(name => FAKE_PREFS.set(name, initialPrefs[name]));
|
||||
setFakePrefsWithInitialValue();
|
||||
FAKE_PREFS.set("showSearch", false);
|
||||
|
||||
feed._prefs.set("showSearch", true);
|
||||
|
@ -170,7 +175,7 @@ describe("PrefsFeed", () => {
|
|||
assert.calledOnce(feed._setPrerenderPref);
|
||||
});
|
||||
it("should set the prerendered pref to true", async () => {
|
||||
Object.keys(initialPrefs).forEach(name => FAKE_PREFS.set(name, initialPrefs[name]));
|
||||
setFakePrefsWithInitialValue();
|
||||
FAKE_PREFS.set("showSearch", false);
|
||||
feed._prefs.set("showSearch", true);
|
||||
feed.onPrefChanged("showSearch", true);
|
||||
|
@ -180,7 +185,7 @@ describe("PrefsFeed", () => {
|
|||
assert.calledWith(feed._prefs.set, PRERENDER_PREF_NAME, true);
|
||||
});
|
||||
it("should set the prerendered pref to false", async () => {
|
||||
Object.keys(initialPrefs).forEach(name => FAKE_PREFS.set(name, initialPrefs[name]));
|
||||
setFakePrefsWithInitialValue();
|
||||
FAKE_PREFS.set("showSearch", false);
|
||||
feed._prefs.set("showSearch", false);
|
||||
feed.onPrefChanged("showSearch", false);
|
||||
|
|
Загрузка…
Ссылка в новой задаче