Skip to content

Commit 141d073

Browse files
committed
enhance: also handle old-style grapher pages
1 parent 90daaf7 commit 141d073

File tree

4 files changed

+50
-20
lines changed

4 files changed

+50
-20
lines changed

baker/GrapherBaker.tsx

+11-2
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ export const renderDataPageOrGrapherPage = async (
121121
runtimeAssetMap,
122122
})
123123
if (datapage) return datapage
124-
return renderGrapherPage(grapher, knex)
124+
return renderGrapherPage(grapher, knex, { staticAssetMap, runtimeAssetMap })
125125
}
126126

127127
export async function renderDataPageV2(
@@ -261,7 +261,14 @@ export const renderPreviewDataPageOrGrapherPage = async (
261261

262262
const renderGrapherPage = async (
263263
grapher: GrapherInterface,
264-
knex: db.KnexReadonlyTransaction
264+
knex: db.KnexReadonlyTransaction,
265+
{
266+
staticAssetMap,
267+
runtimeAssetMap,
268+
}: {
269+
staticAssetMap?: AssetMap
270+
runtimeAssetMap?: AssetMap
271+
} = {}
265272
) => {
266273
const postSlug = urlToSlug(grapher.originUrl || "") as string | undefined
267274
// TODO: update this to use gdocs posts
@@ -282,6 +289,8 @@ const renderGrapherPage = async (
282289
relatedArticles={relatedArticles}
283290
baseUrl={BAKED_BASE_URL}
284291
baseGrapherUrl={BAKED_GRAPHER_URL}
292+
staticAssetMap={staticAssetMap}
293+
runtimeAssetMap={runtimeAssetMap}
285294
/>
286295
)
287296
}

baker/buildLocalArchivalBake.ts

+18-14
Original file line numberDiff line numberDiff line change
@@ -188,31 +188,35 @@ const bakeDomainToFolder = async (
188188

189189
console.log(`Baking site locally with baseUrl '${baseUrl}' to dir '${dir}'`)
190190

191-
const SLUG = "life-expectancy"
191+
const SLUGS = ["life-expectancy", "environmental-footprint-milks"]
192192

193193
const commonRuntimeFiles = await bakeDods()
194194

195195
await db.knexReadonlyTransaction(async (trx) => {
196196
const imageMetadataDictionary = await getAllImages(trx).then((images) =>
197197
keyBy(images, "filename")
198198
)
199-
const chart = await getChartConfigBySlug(trx, SLUG)
200199

201-
const runtimeFiles = { ...commonRuntimeFiles }
200+
for (const chartSlug of SLUGS) {
201+
const chart = await getChartConfigBySlug(trx, chartSlug)
202202

203-
for (const dim of chart.config.dimensions ?? []) {
204-
if (dim.variableId) {
205-
const variableId = dim.variableId
206-
const variableFiles = await bakeVariableDataFiles(variableId)
207-
Object.assign(runtimeFiles, variableFiles)
203+
const runtimeFiles = { ...commonRuntimeFiles }
204+
205+
for (const dim of chart.config.dimensions ?? []) {
206+
if (dim.variableId) {
207+
const variableId = dim.variableId
208+
const variableFiles =
209+
await bakeVariableDataFiles(variableId)
210+
Object.assign(runtimeFiles, variableFiles)
211+
}
208212
}
209-
}
210213

211-
await bakeSingleGrapherPageForArchival(dir, chart.config, trx, {
212-
imageMetadataDictionary,
213-
staticAssetMap,
214-
runtimeAssetMap: runtimeFiles,
215-
})
214+
await bakeSingleGrapherPageForArchival(dir, chart.config, trx, {
215+
imageMetadataDictionary,
216+
staticAssetMap,
217+
runtimeAssetMap: runtimeFiles,
218+
})
219+
}
216220
}, db.TransactionCloseMode.Close)
217221

218222
if (copyToLatestDir) {

packages/@ourworldindata/grapher/src/core/Grapher.tsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -2435,7 +2435,8 @@ export class Grapher
24352435
}
24362436

24372437
static renderSingleGrapherOnGrapherPage(
2438-
jsonConfig: GrapherInterface
2438+
jsonConfig: GrapherInterface,
2439+
{ runtimeAssetMap }: { runtimeAssetMap?: AssetMap } = {}
24392440
): void {
24402441
const container = document.getElementsByTagName("figure")[0]
24412442
try {
@@ -2445,6 +2446,7 @@ export class Grapher
24452446
bindUrlToWindow: true,
24462447
enableKeyboardShortcuts: true,
24472448
queryStr: window.location.search,
2449+
runtimeAssetMap,
24482450
},
24492451
container
24502452
)

site/GrapherPage.tsx

+18-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
} from "@ourworldindata/utils"
1616
import { MarkdownTextWrap } from "@ourworldindata/components"
1717
import {
18+
AssetMap,
1819
HIDE_IF_JS_DISABLED_CLASSNAME,
1920
HIDE_IF_JS_ENABLED_CLASSNAME,
2021
} from "@ourworldindata/types"
@@ -39,6 +40,8 @@ export const GrapherPage = (props: {
3940
relatedArticles?: PostReference[]
4041
baseUrl: string
4142
baseGrapherUrl: string
43+
staticAssetMap?: AssetMap
44+
runtimeAssetMap?: AssetMap
4245
}) => {
4346
const { grapher, relatedCharts, relatedArticles, baseGrapherUrl, baseUrl } =
4447
props
@@ -73,7 +76,8 @@ export const GrapherPage = (props: {
7376
bakedGrapherURL: BAKED_GRAPHER_URL,
7477
dataApiUrl: DATA_API_URL,
7578
})}
76-
window.Grapher.renderSingleGrapherOnGrapherPage(jsonConfig)`
79+
const runtimeAssetMap = (typeof window !== "undefined" && window._OWID_RUNTIME_ASSET_MAP) || undefined;
80+
window.Grapher.renderSingleGrapherOnGrapherPage(jsonConfig, { runtimeAssetMap: runtimeAssetMap })`
7781

7882
const variableIds = uniq(grapher.dimensions!.map((d) => d.variableId))
7983

@@ -85,15 +89,24 @@ window.Grapher.renderSingleGrapherOnGrapherPage(jsonConfig)`
8589
pageDesc={pageDesc}
8690
imageUrl={imageUrl}
8791
baseUrl={baseUrl}
92+
staticAssetMap={props.staticAssetMap}
8893
>
8994
<meta property="og:image:width" content={imageWidth} />
9095
<meta property="og:image:height" content={imageHeight} />
9196
<IFrameDetector />
9297
<link rel="preconnect" href={dataApiOrigin} />
9398
{variableIds.flatMap((variableId) =>
9499
[
95-
getVariableDataRoute(DATA_API_URL, variableId),
96-
getVariableMetadataRoute(DATA_API_URL, variableId),
100+
getVariableDataRoute(
101+
DATA_API_URL,
102+
variableId,
103+
props.runtimeAssetMap
104+
),
105+
getVariableMetadataRoute(
106+
DATA_API_URL,
107+
variableId,
108+
props.runtimeAssetMap
109+
),
97110
].map((href) => (
98111
<link
99112
key={href}
@@ -167,6 +180,8 @@ window.Grapher.renderSingleGrapherOnGrapherPage(jsonConfig)`
167180
<SiteFooter
168181
baseUrl={baseUrl}
169182
context={SiteFooterContext.grapherPage}
183+
staticAssetMap={props.staticAssetMap}
184+
runtimeAssetMap={props.runtimeAssetMap}
170185
/>
171186
<script
172187
type="module"

0 commit comments

Comments
 (0)