Skip to content

Commit f4dcfd4

Browse files
committed
refactor: make runtimeAssetMap available under window._OWID_RUNTIME_ASSET_MAP
1 parent 2716f71 commit f4dcfd4

6 files changed

+30
-26
lines changed

site/DataPageV2.tsx

-1
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,6 @@ export const DataPageV2 = (props: {
175175
faqEntries={faqEntries}
176176
canonicalUrl={canonicalUrl}
177177
tagToSlugMap={tagToSlugMap}
178-
runtimeAssetMap={runtimeAssetMap}
179178
/>
180179
</DebugProvider>
181180
</div>

site/DataPageV2Content.tsx

+1-5
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import {
1111
GrapherInterface,
1212
joinTitleFragments,
1313
ImageMetadata,
14-
AssetMap,
1514
} from "@ourworldindata/utils"
1615
import { DocumentContext } from "./gdocs/DocumentContext.js"
1716
import { AttachmentsContext } from "./gdocs/AttachmentsContext.js"
@@ -38,11 +37,9 @@ export const DataPageV2Content = ({
3837
canonicalUrl = "{URL}", // when we bake pages to their proper url this will be set correctly but on preview pages we leave this undefined
3938
tagToSlugMap,
4039
imageMetadata,
41-
runtimeAssetMap,
4240
}: DataPageV2ContentFields & {
4341
grapherConfig: GrapherInterface
4442
imageMetadata: Record<string, ImageMetadata>
45-
runtimeAssetMap?: AssetMap
4643
}) => {
4744
const [grapher, setGrapher] = useState<Grapher | undefined>(undefined)
4845

@@ -57,9 +54,8 @@ export const DataPageV2Content = ({
5754
...grapherConfig,
5855
isEmbeddedInADataPage: true,
5956
bindUrlToWindow: true,
60-
runtimeAssetMap,
6157
}),
62-
[grapherConfig, runtimeAssetMap]
58+
[grapherConfig]
6359
)
6460

6561
useEffect(() => {

site/GrapherFigureView.tsx

+10-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ import {
77
DATA_API_URL,
88
} from "../settings/clientSettings.js"
99
import { useElementBounds } from "./hooks.js"
10+
import { AssetMap } from "@ourworldindata/types"
11+
12+
declare const window: Window & {
13+
_OWID_RUNTIME_ASSET_MAP?: AssetMap
14+
}
1015

1116
// Wrapper for Grapher that uses css on figure element to determine the bounds
1217
export const GrapherFigureView = ({
@@ -19,14 +24,18 @@ export const GrapherFigureView = ({
1924
const base = useRef<HTMLDivElement>(null)
2025
const bounds = useElementBounds(base)
2126

27+
const runtimeAssetMap =
28+
(typeof window !== "undefined" && window._OWID_RUNTIME_ASSET_MAP) ||
29+
undefined
30+
2231
const grapherProps: GrapherProgrammaticInterface = {
2332
...grapher.toObject(),
2433
isEmbeddedInADataPage: grapher.isEmbeddedInADataPage,
2534
bindUrlToWindow: grapher.props.bindUrlToWindow,
2635
queryStr: grapher.props.bindUrlToWindow
2736
? window.location.search
2837
: undefined,
29-
runtimeAssetMap: grapher.props.runtimeAssetMap,
38+
runtimeAssetMap,
3039
bounds,
3140
dataApiUrl: DATA_API_URL,
3241
enableKeyboardShortcuts: true,

site/SiteFooter.tsx

+5-2
Original file line numberDiff line numberDiff line change
@@ -286,12 +286,15 @@ export const SiteFooter = (props: SiteFooterProps) => (
286286
<script
287287
type="module"
288288
dangerouslySetInnerHTML={{
289-
__html: `window.runSiteFooterScripts(${JSON.stringify({
289+
__html: `
290+
window._OWID_RUNTIME_ASSET_MAP = ${JSON.stringify(
291+
props.runtimeAssetMap
292+
)};
293+
window.runSiteFooterScripts(${JSON.stringify({
290294
context: props.context,
291295
debug: props.debug,
292296
isPreviewing: props.isPreviewing,
293297
hideDonationFlag: props.hideDonationFlag,
294-
runtimeAssetMap: props.runtimeAssetMap,
295298
})})`, // todo: gotta be a better way.
296299
}}
297300
/>

site/detailsOnDemand.tsx

+6-5
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,17 @@ type Tippyfied<E> = E & {
1818
declare global {
1919
interface Window {
2020
details?: DetailDictionary
21+
_OWID_RUNTIME_ASSET_MAP?: AssetMap
2122
}
2223
}
2324

2425
const siteAnalytics = new SiteAnalytics()
2526

26-
export async function runDetailsOnDemand({
27-
runtimeAssetMap,
28-
}: {
29-
runtimeAssetMap?: AssetMap
30-
} = {}) {
27+
export async function runDetailsOnDemand() {
28+
const runtimeAssetMap =
29+
(typeof window !== "undefined" && window._OWID_RUNTIME_ASSET_MAP) ||
30+
undefined
31+
3132
const dodFetchUrl = readFromAssetMap(runtimeAssetMap, {
3233
path: "dods.json",
3334
fallback: `${BAKED_BASE_URL}/dods.json`,

site/runSiteFooterScripts.tsx

+8-12
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,7 @@ function hydrateDataInsightsIndexPage() {
7979

8080
function hydrateDataPageV2Content({
8181
isPreviewing,
82-
runtimeAssetMap,
83-
}: { isPreviewing?: boolean; runtimeAssetMap?: AssetMap } = {}) {
82+
}: { isPreviewing?: boolean } = {}) {
8483
const wrapper = document.querySelector(`#${OWID_DATAPAGE_CONTENT_ROOT_ID}`)
8584
const props: DataPageV2ContentFields = window._OWID_DATAPAGEV2_PROPS
8685
const grapherConfig = window._OWID_GRAPHER_CONFIG
@@ -91,7 +90,6 @@ function hydrateDataPageV2Content({
9190
{...props}
9291
grapherConfig={grapherConfig}
9392
isPreviewing={isPreviewing}
94-
runtimeAssetMap={runtimeAssetMap}
9593
/>
9694
</DebugProvider>,
9795
wrapper
@@ -272,25 +270,23 @@ export const runSiteFooterScripts = (
272270
context?: SiteFooterContext
273271
container?: HTMLElement
274272
hideDonationFlag?: boolean
275-
runtimeAssetMap?: AssetMap
276273
}
277274
| undefined
278275
) => {
279276
// We used to destructure this in the function signature, but that caused
280277
// a weird issue reported by bugsnag: https://app.bugsnag.com/our-world-in-data/our-world-in-data-website/errors/63ca39b631e8660009464eb4?event_id=63d384c500acc25fc0810000&i=sk&m=ef
281278
// So now we define the object as potentially undefined and then destructure it here.
282-
const { debug, context, isPreviewing, hideDonationFlag, runtimeAssetMap } =
283-
args || {}
279+
const { debug, context, isPreviewing, hideDonationFlag } = args || {}
284280

285281
switch (context) {
286282
case SiteFooterContext.dataPageV2:
287-
hydrateDataPageV2Content({ isPreviewing, runtimeAssetMap })
283+
hydrateDataPageV2Content({ isPreviewing })
288284
runAllGraphersLoadedListener()
289285
runLightbox()
290286
runSiteNavigation(BAKED_BASE_URL, hideDonationFlag)
291287
runSiteTools()
292288
runCookiePreferencesManager()
293-
void runDetailsOnDemand({ runtimeAssetMap })
289+
void runDetailsOnDemand()
294290
break
295291
case SiteFooterContext.multiDimDataPage:
296292
hydrateMultiDimDataPageContent(isPreviewing)
@@ -299,15 +295,15 @@ export const runSiteFooterScripts = (
299295
runSiteNavigation(BAKED_BASE_URL, hideDonationFlag)
300296
runSiteTools()
301297
runCookiePreferencesManager()
302-
void runDetailsOnDemand({ runtimeAssetMap })
298+
void runDetailsOnDemand()
303299
break
304300
case SiteFooterContext.grapherPage:
305301
case SiteFooterContext.explorerPage:
306302
runSiteNavigation(BAKED_BASE_URL, hideDonationFlag)
307303
runAllGraphersLoadedListener()
308304
runSiteTools()
309305
runCookiePreferencesManager()
310-
void runDetailsOnDemand({ runtimeAssetMap })
306+
void runDetailsOnDemand()
311307
break
312308
case SiteFooterContext.explorerIndexPage:
313309
hydrateExplorerIndex()
@@ -320,7 +316,7 @@ export const runSiteFooterScripts = (
320316
runAllGraphersLoadedListener()
321317
runSiteNavigation(BAKED_BASE_URL, hideDonationFlag)
322318
runFootnotes()
323-
void runDetailsOnDemand({ runtimeAssetMap })
319+
void runDetailsOnDemand()
324320
runLightbox()
325321
runSiteTools()
326322
runCookiePreferencesManager()
@@ -356,7 +352,7 @@ export const runSiteFooterScripts = (
356352
runFootnotes()
357353
runSiteTools()
358354
runCookiePreferencesManager()
359-
void runDetailsOnDemand({ runtimeAssetMap })
355+
void runDetailsOnDemand()
360356
break
361357
}
362358
}

0 commit comments

Comments
 (0)