|
1
|
import { getCloudflareContext } from "@opennextjs/cloudflare" |
|
2
|
|
|
3
|
/** |
|
4
|
* Reads a reference file through the R2 bucket binding, avoiding the HTTP |
|
5
|
* round trip that fetching the public bucket URL requires. Returns null |
|
6
|
* outside Cloudflare (next dev / build) or when the binding isn't |
|
7
|
* configured, so callers can fall back to fetching. |
|
8
|
*/ |
|
9
|
export async function loadReferenceFromBinding( |
|
10
|
key: string |
|
11
|
): Promise<string | null> { |
|
12
|
try { |
|
13
|
// async mode so this also works in statically rendered contexts |
|
14
|
const { env } = await getCloudflareContext({ async: true }) |
|
15
|
const object = await env.REFERENCES_R2_BUCKET?.get(key) |
|
16
|
return object ? await object.text() : null |
|
17
|
} catch { |
|
18
|
return null |
|
19
|
} |
|
20
|
} |