From 6478da8e8ee9c79f6046b6fd1bd50823dde57369 Mon Sep 17 00:00:00 2001 From: Daniel Bachler Date: Wed, 7 Aug 2024 09:31:51 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A8=20add=20fallback=20to=20branch=20n?= =?UTF-8?q?ame=20as=20directory=20in=20R2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- functions/_common/grapherRenderer.ts | 14 ++++++++++++-- functions/grapher/thumbnail/[slug].ts | 1 + 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/functions/_common/grapherRenderer.ts b/functions/_common/grapherRenderer.ts index 06fa9e853ed..ffe054581dc 100644 --- a/functions/_common/grapherRenderer.ts +++ b/functions/_common/grapherRenderer.ts @@ -150,12 +150,22 @@ async function fetchAndRenderGrapherToSvg({ const url = new URL(`/grapher/${slug}`, env.url) const slugOnly = url.pathname.split("/").pop() + + // The top level directory is either the bucket path (should be set in dev environments and production) + // or the branch name on preview staging environments + console.log("branch", env.CF_PAGES_BRANCH) + const topLevelDirectory = env.GRAPHER_CONFIG_R2_BUCKET_PATH + ? [env.GRAPHER_CONFIG_R2_BUCKET_PATH] + : ["by-branch", env.CF_PAGES_BRANCH] + const key = excludeUndefined([ - env.GRAPHER_CONFIG_R2_BUCKET_PATH, + ...topLevelDirectory, R2GrapherConfigDirectory.publishedGrapherBySlug, `${slugOnly}.json`, ]).join("/") + console.log("fetching grapher config from this key", key) + // Fetch grapher config const fetchResponse = await env.r2ChartConfigs.get(key) @@ -164,7 +174,7 @@ async function fetchAndRenderGrapherToSvg({ } const grapherConfig: GrapherInterface = await fetchResponse.json() - console.log("grapher interface", grapherConfig) + console.log("grapher title", grapherConfig.title) const bounds = new Bounds(0, 0, options.svgWidth, options.svgHeight) const grapher = new Grapher({ diff --git a/functions/grapher/thumbnail/[slug].ts b/functions/grapher/thumbnail/[slug].ts index ad5e34f61cd..081fe1ee4b0 100644 --- a/functions/grapher/thumbnail/[slug].ts +++ b/functions/grapher/thumbnail/[slug].ts @@ -10,6 +10,7 @@ export interface Env { } url: URL GRAPHER_CONFIG_R2_BUCKET_PATH: string + CF_PAGES_BRANCH: string } const router = Router()