diff --git a/browser/components/newtab/lib/RecommendationProviderSwitcher.jsm b/browser/components/newtab/lib/RecommendationProviderSwitcher.jsm index 947fe2b0475d..579095e45c1c 100644 --- a/browser/components/newtab/lib/RecommendationProviderSwitcher.jsm +++ b/browser/components/newtab/lib/RecommendationProviderSwitcher.jsm @@ -57,6 +57,15 @@ this.RecommendationProviderSwitcher = class RecommendationProviderSwitcher { setAffinityProvider(...args) { const { affinityProviderV2 } = this; if (affinityProviderV2 && affinityProviderV2.modelKeys) { + // A v2 provider is already set. This can happen when new stories come in + // and we need to update their scores. + // We can use the existing one, a fresh one is created after startup. + // Using the existing one might be a bit out of date, + // but it's fine for now. We can rely on restarts for updates. + // See bug 1629931 for improvements to this. + if (this.affinityProvider) { + return; + } // At this point we've determined we can successfully create a v2 personalization provider. this.affinityProvider = new PersonalityProvider(...args, { modelKeys: affinityProviderV2.modelKeys, diff --git a/browser/components/newtab/test/unit/lib/RecommendationProviderSwitcher.test.js b/browser/components/newtab/test/unit/lib/RecommendationProviderSwitcher.test.js index 2e1d27d95fc8..5160e15555a3 100644 --- a/browser/components/newtab/test/unit/lib/RecommendationProviderSwitcher.test.js +++ b/browser/components/newtab/test/unit/lib/RecommendationProviderSwitcher.test.js @@ -31,6 +31,7 @@ describe("RecommendationProviderSwitcher", () => { assert.equal(feed.affinityProvider.modelKeys, undefined); + feed.affinityProvider = null; feed.affinityProviderV2 = { modelKeys: "1234", }; @@ -45,6 +46,31 @@ describe("RecommendationProviderSwitcher", () => { assert.equal(feed.affinityProvider.modelKeys, "1234"); }); + it("should use old provider", async () => { + feed.setAffinityProvider( + undefined, + undefined, + undefined, + undefined, + undefined + ); + + assert.equal(feed.affinityProvider.modelKeys, undefined); + + feed.affinityProviderV2 = { + modelKeys: "1234", + }; + + feed.setAffinityProvider( + undefined, + undefined, + undefined, + undefined, + undefined + ); + + assert.equal(feed.affinityProvider.modelKeys, undefined); + }); }); describe("#init", () => {