1 import { getCloudflareContext } from "@opennextjs/cloudflare"
2
3 /**
4 * Fetches a static asset through the worker's ASSETS binding, avoiding the
5 * HTTP round trip (and extra worker invocation) that fetching the public URL
6 * requires. Returns null outside Cloudflare (next dev / build) or when the
7 * asset doesn't exist, so callers can fall back to fetching or reading from
8 * the filesystem.
9 */
10 export async function fetchFromAssetsBinding(
11 url: string
12 ): Promise<string | null> {
13 try {
14 // async mode so this also works in statically rendered contexts
15 const { env } = await getCloudflareContext({ async: true })
16 const res = await env.ASSETS?.fetch(url)
17 return res?.ok ? await res.text() : null
18 } catch {
19 return null
20 }
21 }