Skip to content

Commit

Permalink
✨ use CF function to update no-JS fallback image on grapher page HTML
Browse files Browse the repository at this point in the history
  • Loading branch information
ikesau committed Nov 6, 2024
1 parent 0c68bdb commit c49298d
Show file tree
Hide file tree
Showing 14 changed files with 54 additions and 18 deletions.
3 changes: 1 addition & 2 deletions baker/formatWordpressPost.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import urlSlug from "url-slug"
import React from "react"
import ReactDOMServer from "react-dom/server.js"
import { HTTPS_ONLY } from "../settings/serverSettings.js"
import { FormattingOptions } from "@ourworldindata/types"
import { FormattingOptions, GRAPHER_PREVIEW_CLASS } from "@ourworldindata/types"
import { FormattedPost, FullPost, TocHeading, Url } from "@ourworldindata/utils"
import { parseKeyValueArgs } from "../serverUtils/wordpressUtils.js"
import { Footnote } from "../site/Footnote.js"
Expand All @@ -19,7 +19,6 @@ import {
import { renderHelp } from "../site/blocks/Help.js"
import { renderCodeSnippets } from "@ourworldindata/components"
import { formatUrls, getBodyHtml } from "../site/formatting.js"
import { GRAPHER_PREVIEW_CLASS } from "../site/SiteConstants.js"
import { INTERACTIVE_ICON_SVG } from "../site/InteractionNotice.js"
import { renderProminentLinks } from "./siteRenderers.js"
import { RELATED_CHARTS_CLASS_NAME } from "../site/blocks/RelatedCharts.js"
Expand Down
21 changes: 20 additions & 1 deletion functions/grapher/[slug].ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { Env, extensions, Etag } from "../_common/env.js"
import {
GRAPHER_PREVIEW_CLASS,
HIDE_IF_JS_ENABLED_CLASSNAME,
} from "@ourworldindata/types"
import {
fetchCsvForGrapher,
fetchMetadataForGrapher,
Expand Down Expand Up @@ -142,6 +146,9 @@ async function handleHtmlPageRequest(
// In the case of the redirect, the browser will then request the new URL which will again be handled by this worker.
if (grapherPageResp.status !== 200) return grapherPageResp

const thumbnailUrl = `/grapher/${slug}.png?${
url.search ? "&" + url.search.slice(1) : ""
}`
const openGraphThumbnailUrl = `/grapher/${slug}.png?imType=og${
url.search ? "&" + url.search.slice(1) : ""
}`
Expand All @@ -153,8 +160,20 @@ async function handleHtmlPageRequest(
// If we fail to capture the origin, we end up with relative image URLs, which should also be okay.
let origin = ""

// Rewrite the two meta tags that are used for a social media preview image.
// Rewrite the fallback image (for no-JS cases) and meta tags that are used for a social media preview image.
const rewriter = new HTMLRewriter()
// Grapher pages
.on(`div#fallback.${HIDE_IF_JS_ENABLED_CLASSNAME} img`, {
element: (element) => {
element.setAttribute("src", thumbnailUrl)
},
})
// Data pages
.on(`figure.${GRAPHER_PREVIEW_CLASS} img`, {
element: (element) => {
element.setAttribute("src", thumbnailUrl)
},
})
.on('meta[property="og:url"]', {
// Replace canonical URL, otherwise the preview image will not include the search parameters.
element: (element) => {
Expand Down
6 changes: 6 additions & 0 deletions packages/@ourworldindata/types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -691,3 +691,9 @@ export type {
View,
ViewEnriched,
} from "./siteTypes/MultiDimDataPage.js"

export {
GRAPHER_PREVIEW_CLASS,
HIDE_IF_JS_DISABLED_CLASSNAME,
HIDE_IF_JS_ENABLED_CLASSNAME,
} from "./siteTypes/SiteConstants.js"
7 changes: 7 additions & 0 deletions packages/@ourworldindata/types/src/siteTypes/SiteConstants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Used in GrapherPage.tsx
export const HIDE_IF_JS_ENABLED_CLASSNAME = "js--hide-if-js-enabled"

export const HIDE_IF_JS_DISABLED_CLASSNAME = "js--hide-if-js-disabled"

// Used in GrapherWithFallback.tsx
export const GRAPHER_PREVIEW_CLASS = "grapherPreview"
8 changes: 6 additions & 2 deletions site/GrapherPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ import {
diffGrapherConfigs,
} from "@ourworldindata/utils"
import { MarkdownTextWrap } from "@ourworldindata/components"
import {
HIDE_IF_JS_DISABLED_CLASSNAME,
HIDE_IF_JS_ENABLED_CLASSNAME,
} from "@ourworldindata/types"
import React from "react"
import urljoin from "url-join"
import {
Expand Down Expand Up @@ -118,12 +122,12 @@ window.Grapher.renderSingleGrapherOnGrapherPage(jsonConfig)`
<SiteHeader baseUrl={baseUrl} />
<main>
<figure
className="js--hide-if-js-disabled"
className={HIDE_IF_JS_DISABLED_CLASSNAME}
data-grapher-src={`/grapher/${grapher.slug}`}
>
<LoadingIndicator />
</figure>
<div className="js--hide-if-js-enabled" id="fallback">
<div className={HIDE_IF_JS_ENABLED_CLASSNAME} id="fallback">
{grapher.slug && (
<GrapherImage
slug={grapher.slug}
Expand Down
2 changes: 1 addition & 1 deletion site/GrapherWithFallback.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Grapher } from "@ourworldindata/grapher"
import { GRAPHER_PREVIEW_CLASS } from "@ourworldindata/types"
import React from "react"
import { GrapherFigureView } from "./GrapherFigureView.js"
import cx from "classnames"
import { GRAPHER_PREVIEW_CLASS } from "./SiteConstants.js"
import GrapherImage from "./GrapherImage.js"

export const GrapherWithFallback = ({
Expand Down
2 changes: 0 additions & 2 deletions site/SiteConstants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ export const POLYFILL_URL: string = `https://cdnjs.cloudflare.com/polyfill/v3/po

export const DEFAULT_LOCAL_BAKE_DIR = "localBake"

export const GRAPHER_PREVIEW_CLASS = "grapherPreview"

export const SMALL_BREAKPOINT_MEDIA_QUERY = "(max-width: 768px)"

export const TOUCH_DEVICE_MEDIA_QUERY =
Expand Down
10 changes: 7 additions & 3 deletions site/blocks/RelatedCharts.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import React, { useState, useRef } from "react"
import { orderBy, RelatedChart } from "@ourworldindata/utils"
import {
HIDE_IF_JS_ENABLED_CLASSNAME,
orderBy,
RelatedChart,
} from "@ourworldindata/utils"
import { useEmbedChart } from "../hooks.js"
import { GalleryArrow, GalleryArrowDirection } from "./GalleryArrow.js"
import { AllChartsListItem } from "./AllChartsListItem.js"
import { BAKED_BASE_URL } from "../../settings/clientSettings.js"
import { GRAPHER_PREVIEW_CLASS } from "../SiteConstants.js"
import { GRAPHER_PREVIEW_CLASS } from "@ourworldindata/types"
import GrapherImage from "../GrapherImage.js"

export const RELATED_CHARTS_CLASS_NAME = "related-charts"
Expand Down Expand Up @@ -53,7 +57,7 @@ export const RelatedCharts = ({
key={activeChartSlug}
data-grapher-src={grapherUrl}
>
<div className="js--hide-if-js-enabled">
<div className={HIDE_IF_JS_ENABLED_CLASSNAME}>
<a href={grapherUrl}>
<GrapherImage
slug={activeChartSlug}
Expand Down
2 changes: 1 addition & 1 deletion site/collections/DynamicCollection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
import { observer } from "mobx-react"
import { WindowGraphers } from "./DynamicCollectionPage.js"
import { Grapher } from "@ourworldindata/grapher"
import { GRAPHER_PREVIEW_CLASS } from "../SiteConstants.js"
import { GRAPHER_PREVIEW_CLASS } from "@ourworldindata/types"
import InteractionNotice from "../InteractionNotice.js"
import GrapherImage from "../GrapherImage.js"

Expand Down
2 changes: 1 addition & 1 deletion site/collections/StaticCollectionPage.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import cx from "classnames"
import React from "react"
import { Head } from "../Head.js"
import { GRAPHER_PREVIEW_CLASS } from "../SiteConstants.js"
import { GRAPHER_PREVIEW_CLASS } from "@ourworldindata/types"
import { SiteHeader } from "../SiteHeader.js"
import { SiteFooter } from "../SiteFooter.js"
import InteractionNotice from "../InteractionNotice.js"
Expand Down
2 changes: 1 addition & 1 deletion site/formatting.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import cheerio from "cheerio"
import { WP_ColumnStyle } from "@ourworldindata/utils"
import { splitContentIntoSectionsAndColumns } from "./formatting.js"
import { formatAuthors } from "./clientFormatting.js"
import { GRAPHER_PREVIEW_CLASS } from "./SiteConstants.js"
import { GRAPHER_PREVIEW_CLASS } from "@ourworldindata/types"

const paragraph = `<p>Some paragraph</p>`
const chart = `<figure data-grapher-src="https://ourworldindata.org/grapher/pneumococcal-vaccination-averted-deaths" class="${GRAPHER_PREVIEW_CLASS}"></figure>`
Expand Down
3 changes: 1 addition & 2 deletions site/formatting.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ import { bakeGlobalEntitySelector } from "./bakeGlobalEntitySelector.js"
import { PROMINENT_LINK_CLASSNAME } from "./blocks/ProminentLink.js"
import { Byline } from "./Byline.js"
import { SectionHeading } from "./SectionHeading.js"
import { FormattingOptions } from "@ourworldindata/types"
import { GRAPHER_PREVIEW_CLASS } from "./SiteConstants.js"
import { FormattingOptions, GRAPHER_PREVIEW_CLASS } from "@ourworldindata/types"

export const RESEARCH_AND_WRITING_CLASSNAME = "wp-block-research-and-writing"

Expand Down
2 changes: 1 addition & 1 deletion site/gdocs/components/Chart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
} from "@ourworldindata/utils"
import { renderSpans, useLinkedChart } from "../utils.js"
import cx from "classnames"
import { GRAPHER_PREVIEW_CLASS } from "../../SiteConstants.js"
import { GRAPHER_PREVIEW_CLASS } from "@ourworldindata/types"
import InteractionNotice from "../../InteractionNotice.js"
import GrapherImage from "../../GrapherImage.js"

Expand Down
2 changes: 1 addition & 1 deletion site/multiembedder/MultiEmbedder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import {
EMBEDDED_EXPLORER_PARTIAL_GRAPHER_CONFIGS,
EXPLORER_EMBEDDED_FIGURE_SELECTOR,
} from "../../explorer/ExplorerConstants.js"
import { GRAPHER_PREVIEW_CLASS } from "../SiteConstants.js"
import { GRAPHER_PREVIEW_CLASS } from "@ourworldindata/types"
import {
ADMIN_BASE_URL,
BAKED_GRAPHER_URL,
Expand Down

0 comments on commit c49298d

Please sign in to comment.