diff --git a/packages/@ourworldindata/grapher/src/core/Grapher.jsdom.test.ts b/packages/@ourworldindata/grapher/src/core/Grapher.jsdom.test.ts index 10eaf349674..f0ce835a22a 100755 --- a/packages/@ourworldindata/grapher/src/core/Grapher.jsdom.test.ts +++ b/packages/@ourworldindata/grapher/src/core/Grapher.jsdom.test.ts @@ -465,6 +465,16 @@ describe("urls", () => { expect(grapher.canonicalUrl?.includes("country")).toBeFalsy() }) + it("embedUrl includes the tab param even if it's the default value", () => { + const grapher = new Grapher({ + isPublished: true, + slug: "foo", + bakedGrapherURL: "/grapher", + tab: GrapherTabOption.map, + }) + expect(grapher.embedUrl).toEqual("/grapher/foo?tab=map") + }) + it("can upgrade legacy urls", () => { expect(legacyToCurrentGrapherQueryParams("?year=2000")).toEqual({ time: "2000", diff --git a/packages/@ourworldindata/grapher/src/core/Grapher.tsx b/packages/@ourworldindata/grapher/src/core/Grapher.tsx index a139eacf68f..507b2ed0d19 100644 --- a/packages/@ourworldindata/grapher/src/core/Grapher.tsx +++ b/packages/@ourworldindata/grapher/src/core/Grapher.tsx @@ -2999,7 +2999,17 @@ export class Grapher } @computed get embedUrl(): string | undefined { - return this.manager?.embedDialogUrl ?? this.canonicalUrl + const url = this.manager?.embedDialogUrl ?? this.canonicalUrl + if (!url) return + + // We want to preserve the tab in the embed URL so that if we change the + // default view of the chart, it won't change existing embeds. + // See https://github.com/owid/owid-grapher/issues/2805 + let urlObj = Url.fromURL(url) + if (!urlObj.queryParams.tab) { + urlObj = urlObj.updateQueryParams({ tab: this.allParams.tab }) + } + return urlObj.fullUrl } @computed get embedDialogAdditionalElements(): diff --git a/packages/@ourworldindata/grapher/src/modal/EmbedModal.tsx b/packages/@ourworldindata/grapher/src/modal/EmbedModal.tsx index ba479feeba5..14e00415c04 100644 --- a/packages/@ourworldindata/grapher/src/modal/EmbedModal.tsx +++ b/packages/@ourworldindata/grapher/src/modal/EmbedModal.tsx @@ -30,7 +30,7 @@ export class EmbedModal extends React.Component { } @computed private get codeSnippet(): string { - const url = this.manager.embedUrl ?? this.manager.canonicalUrl + const url = this.manager.embedUrl return `` }