Skip to content

Commit

Permalink
🔨 add fallback to branch name as directory in R2
Browse files Browse the repository at this point in the history
  • Loading branch information
danyx23 committed Aug 9, 2024
1 parent a557c15 commit ef42cf3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
14 changes: 12 additions & 2 deletions functions/_common/grapherRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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({
Expand Down
1 change: 1 addition & 0 deletions functions/grapher/thumbnail/[slug].ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface Env {
}
url: URL
GRAPHER_CONFIG_R2_BUCKET_PATH: string
CF_PAGES_BRANCH: string
}

const router = Router<IRequestStrict, [URL, Env, ExecutionContext]>()
Expand Down

0 comments on commit ef42cf3

Please sign in to comment.