Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: incorporate query params for grapher page fallback svg #3873

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions functions/grapher/[slug].ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ export const onRequestGet: PagesFunction = async (context) => {
const twitterThumbnailUrl = `/grapher/thumbnail/${lowerCaseSlug}.png?imType=twitter${
url.search ? "&" + url.search.slice(1) : ""
}`
const grapherThumbnailUrl = url.search.length
? `/grapher/thumbnail/${lowerCaseSlug}.svg${url.search}`
: undefined

// Take the origin (e.g. https://ourworldindata.org) from the canonical URL, which should appear before the image elements.
// If we fail to capture the origin, we end up with relative image URLs, which should also be okay.
Expand All @@ -105,6 +108,12 @@ export const onRequestGet: PagesFunction = async (context) => {
element.setAttribute("content", origin + twitterThumbnailUrl)
},
})
.on("img[data-owid-populate-url-params]", {
element: (element) => {
if (grapherThumbnailUrl)
element.setAttribute("src", grapherThumbnailUrl)
},
})

return rewriter.transform(grapherPageResp)
}
2 changes: 2 additions & 0 deletions site/DataPageV2Content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ export const DataPageV2Content = ({
<GrapherWithFallback
grapher={grapher}
slug={grapherConfig.slug}
enablePopulatingUrlParams
/>
</div>
<div className="DataPageContent grid grid-cols-12-full-width">
Expand Down Expand Up @@ -189,6 +190,7 @@ export const DataPageV2Content = ({
// non-grapher pages then we need to make sure that there are thunbnails that are generated for the these non-chart graphers and
// then this piece will have to change anyhow and know how to provide the thumbnail.
id="explore-the-data"
enablePopulatingUrlParams
/>
<AboutThisData
datapageData={datapageData}
Expand Down
5 changes: 5 additions & 0 deletions site/GrapherImage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ export default function GrapherImage({
alt,
slug,
noFormatting,
enablePopulatingUrlParams,
}: {
slug: string
alt?: string
noFormatting?: boolean
enablePopulatingUrlParams?: boolean
}) {
return (
<img
Expand All @@ -27,6 +29,9 @@ export default function GrapherImage({
loading="lazy"
data-no-lightbox
data-no-img-formatting={noFormatting}
// This tells our Cloudflare functions to replace the src with the dynamic thumbnail URL, including URL params like `?time=2020`.
// Enabling this option only makes sense if this is the _main_ chart on a _standalone_ grapher/data page - it will pass on the URL params from the page to the thumbnail.
data-owid-populate-url-params={enablePopulatingUrlParams}
/>
)
}
1 change: 1 addition & 0 deletions site/GrapherPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ window.Grapher.renderSingleGrapherOnGrapherPage(jsonConfig)`
<GrapherImage
slug={grapher.slug}
alt={grapher.title}
enablePopulatingUrlParams
/>
)}
<p>Interactive visualization requires JavaScript</p>
Expand Down
11 changes: 10 additions & 1 deletion site/GrapherWithFallback.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ export const GrapherWithFallback = ({
slug,
className,
id,
enablePopulatingUrlParams,
}: {
grapher?: Grapher | undefined
slug?: string
className?: string
id?: string
enablePopulatingUrlParams?: boolean
}) => {
return (
<div
Expand All @@ -38,7 +40,14 @@ export const GrapherWithFallback = ({
"GrapherWithFallback__fallback"
)}
>
{slug && <GrapherImage slug={slug} />}
{slug && (
<GrapherImage
slug={slug}
enablePopulatingUrlParams={
enablePopulatingUrlParams
}
/>
)}
</figure>
)}
</>
Expand Down