diff --git a/src/core/client/count/injectJSONPCallback.ts b/src/core/client/count/injectJSONPCallback.ts index a961b92fb..9965e5e8e 100644 --- a/src/core/client/count/injectJSONPCallback.ts +++ b/src/core/client/count/injectJSONPCallback.ts @@ -5,13 +5,21 @@ type GetCountFunction = (opts?: { reset?: boolean }) => void; /** Injects a global CoralCount callback into the window object to be used in JSONP */ function injectJSONPCallback(getCount: GetCountFunction) { (window as any).CoralCount = { - setCount: (data: { ref: string; html: string }) => { + setCount: (data: { ref: string; html: string; count?: number }) => { // Find all the elements with ref. const elements = document.querySelectorAll( `${COUNT_SELECTOR}[data-coral-ref='${data.ref}']` ); Array.prototype.forEach.call(elements, (element: HTMLElement) => { element.innerHTML = data.html; + + // Because this involves a new field being added to the JSONP response, + // we check that the field is available and is a number before we add + // it. + // TODO: (wyattjoh) remove after 6.3.1 + if (data.count !== undefined && typeof data.count === "number") { + element.dataset.coralCount = data.count.toString(); + } }); }, getCount, diff --git a/src/core/client/count/test/__snapshots__/basic.spec.ts.snap b/src/core/client/count/test/__snapshots__/basic.spec.ts.snap index a1c91425f..30e1bde8f 100644 --- a/src/core/client/count/test/__snapshots__/basic.spec.ts.snap +++ b/src/core/client/count/test/__snapshots__/basic.spec.ts.snap @@ -241,6 +241,7 @@ exports[`Inject counts 1`] = ` /> diff --git a/src/core/client/count/test/basic.spec.ts b/src/core/client/count/test/basic.spec.ts index cc7dfd9b4..550d3fa9c 100644 --- a/src/core/client/count/test/basic.spec.ts +++ b/src/core/client/count/test/basic.spec.ts @@ -76,6 +76,7 @@ it("Inject counts", async () => { ref: "ZmFsc2U7aHR0cDovL2xvY2FsaG9zdDo4MDgwLw==", html: '5 Comments', + count: 5, }); expect(document.body).toMatchSnapshot(); }); diff --git a/src/core/server/app/handlers/api/story/count.ts b/src/core/server/app/handlers/api/story/count.ts index e217950c9..52e3892b8 100644 --- a/src/core/server/app/handlers/api/story/count.ts +++ b/src/core/server/app/handlers/api/story/count.ts @@ -83,6 +83,7 @@ export const countHandler = ({ // Reference from the client that we'll just send back as it is. ref, html, + count, }); } catch (err) { return next(err);