Skip to content

Commit

Permalink
Move the multi-dim logic to initGrapher
Browse files Browse the repository at this point in the history
This makes it more general and usable also for other routes, such as
data download.
  • Loading branch information
rakyi committed Jan 27, 2025
1 parent 4bef1e1 commit 3955aa2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 29 deletions.
27 changes: 3 additions & 24 deletions functions/_common/grapherRenderer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { svg2png, initialize as initializeSvg2Png } from "svg2png-wasm"
import { TimeLogger } from "./timeLogger.js"
import { png, StatusError } from "itty-router"
import { png } from "itty-router"

import svg2png_wasm from "../../node_modules/svg2png-wasm/svg2png_wasm_bg.wasm"

Expand All @@ -11,8 +11,7 @@ import LatoBold from "../_common/fonts/LatoLatin-Bold.ttf.bin"
import PlayfairSemiBold from "../_common/fonts/PlayfairDisplayLatin-SemiBold.ttf.bin"
import { Env } from "./env.js"
import { ImageOptions, extractOptions } from "./imageOptions.js"
import { GrapherIdentifier, initGrapher, MultiDimSlug } from "./grapherTools.js"
import { Grapher } from "@ourworldindata/grapher"
import { GrapherIdentifier, initGrapher } from "./grapherTools.js"

declare global {
// eslint-disable-next-line no-var
Expand Down Expand Up @@ -63,27 +62,7 @@ async function fetchAndRenderGrapherToSvg(
env: Env
) {
const grapherLogger = new TimeLogger("grapher")
let grapher: Grapher
try {
grapher = await initGrapher(identifier, options, searchParams, env)
} catch (e) {
if (
identifier.type === "slug" &&
e instanceof StatusError &&
e.status === 404
) {
// Normal graphers and multi-dims have the same URL namespace, but
// we have no way of knowing which of them was requested, so we try
// again with a multi-dim identifier.
const multiDimId: MultiDimSlug = {
type: "multi-dim-slug",
id: identifier.id,
}
grapher = await initGrapher(multiDimId, options, searchParams, env)
} else {
throw e
}
}
const grapher = await initGrapher(identifier, options, searchParams, env)

grapherLogger.log("initGrapher")
const promises = []
Expand Down
34 changes: 29 additions & 5 deletions functions/_common/grapherTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,35 @@ export async function initGrapher(
searchParams: URLSearchParams,
env: Env
): Promise<Grapher> {
const grapherConfigResponse = await fetchGrapherConfig({
identifier,
env,
searchParams,
})
let grapherConfigResponse: FetchGrapherConfigResult
try {
grapherConfigResponse = await fetchGrapherConfig({
identifier,
env,
searchParams,
})
} catch (e) {
if (
identifier.type === "slug" &&
e instanceof StatusError &&
e.status === 404
) {
// Normal graphers and multi-dims have the same URL namespace, but
// we have no way of knowing which of them was requested, so we try
// again with a multi-dim identifier.
const multiDimId: MultiDimSlug = {
type: "multi-dim-slug",
id: identifier.id,
}
grapherConfigResponse = await fetchGrapherConfig({
identifier: multiDimId,
env,
searchParams,
})
} else {
throw e
}
}

if (grapherConfigResponse.status === 404) {
// we throw 404 errors instad of returning a 404 response so that the router
Expand Down

0 comments on commit 3955aa2

Please sign in to comment.