|
| 1 | +--- |
| 2 | +title: Cloudflare Deployment |
| 3 | +description: Deploy Nuxt OG Image to Cloudflare Workers or Pages. |
| 4 | +navigation: |
| 5 | + title: 'Cloudflare' |
| 6 | +--- |
| 7 | + |
| 8 | +Nuxt OG Image works on Cloudflare Workers and Pages with the [Takumi](/docs/og-image/renderers/takumi) (recommended) or [Satori](/docs/og-image/renderers/satori) renderer using Wasm bindings. |
| 9 | + |
| 10 | +## Static Assets (ASSETS Binding) |
| 11 | + |
| 12 | +OG image generation requires loading font files at runtime. On [Cloudflare](https://cloudflare.com), fonts are served as static assets through the [`ASSETS` binding](https://developers.cloudflare.com/workers/static-assets/binding/). Without this binding, the worker cannot access fonts and all text will be invisible. |
| 13 | + |
| 14 | +### Cloudflare Pages |
| 15 | + |
| 16 | +No extra configuration needed. The `wrangler pages deploy` command configures the `ASSETS` binding automatically. |
| 17 | + |
| 18 | +### Cloudflare Workers (Module) |
| 19 | + |
| 20 | +Enable `deployConfig` so Nitro generates a `wrangler.json` with the `ASSETS` binding: |
| 21 | + |
| 22 | +```ts [nuxt.config.ts] |
| 23 | +export default defineNuxtConfig({ |
| 24 | + nitro: { |
| 25 | + preset: 'cloudflare-module', |
| 26 | + cloudflare: { |
| 27 | + deployConfig: true, |
| 28 | + }, |
| 29 | + }, |
| 30 | +}) |
| 31 | +``` |
| 32 | + |
| 33 | +Then build and deploy: |
| 34 | + |
| 35 | +```bash |
| 36 | +nuxt build |
| 37 | +npx wrangler --cwd .output deploy |
| 38 | +``` |
| 39 | + |
| 40 | +::warning |
| 41 | +Do **not** deploy with `wrangler deploy --assets .output/public`. This legacy command does not configure the `ASSETS` binding. Use `npx wrangler --cwd .output deploy` which reads the generated `wrangler.json`. |
| 42 | +:: |
| 43 | + |
| 44 | +You can verify the binding exists in the deploy output: |
| 45 | + |
| 46 | +``` |
| 47 | +Your Worker has access to the following bindings: |
| 48 | +Binding Resource |
| 49 | +env.ASSETS Assets |
| 50 | +``` |
| 51 | + |
| 52 | +### Custom Wrangler Config |
| 53 | + |
| 54 | +If you manage your own `wrangler.toml`, add the `[assets]` section: |
| 55 | + |
| 56 | +```toml [wrangler.toml] |
| 57 | +[assets] |
| 58 | +binding = "ASSETS" |
| 59 | +directory = ".output/public" |
| 60 | +``` |
| 61 | + |
| 62 | +## Runtime Cache with KV |
| 63 | + |
| 64 | +Rendered OG images are cached in memory by default, which resets on every deploy. For persistent caching, use [Cloudflare KV](https://developers.cloudflare.com/kv/). |
| 65 | + |
| 66 | +### 1. Create a KV Namespace |
| 67 | + |
| 68 | +```bash |
| 69 | +npx wrangler kv namespace create OG_IMAGE_CACHE |
| 70 | +``` |
| 71 | + |
| 72 | +### 2. Add the Binding |
| 73 | + |
| 74 | +::code-group |
| 75 | + |
| 76 | +```toml [wrangler.toml] |
| 77 | +[[kv_namespaces]] |
| 78 | +binding = "OG_IMAGE_CACHE" |
| 79 | +id = "<your-namespace-id>" |
| 80 | +``` |
| 81 | + |
| 82 | +```ts [nuxt.config.ts] |
| 83 | +export default defineNuxtConfig({ |
| 84 | + nitro: { |
| 85 | + cloudflare: { |
| 86 | + wrangler: { |
| 87 | + kv_namespaces: [ |
| 88 | + { binding: 'OG_IMAGE_CACHE', id: '<your-namespace-id>' }, |
| 89 | + ], |
| 90 | + }, |
| 91 | + }, |
| 92 | + }, |
| 93 | +}) |
| 94 | +``` |
| 95 | + |
| 96 | +:: |
| 97 | + |
| 98 | +### 3. Configure Cache Storage |
| 99 | + |
| 100 | +```ts [nuxt.config.ts] |
| 101 | +export default defineNuxtConfig({ |
| 102 | + ogImage: { |
| 103 | + runtimeCacheStorage: { |
| 104 | + driver: 'cloudflare-kv-binding', |
| 105 | + binding: 'OG_IMAGE_CACHE', |
| 106 | + }, |
| 107 | + }, |
| 108 | +}) |
| 109 | +``` |
| 110 | + |
| 111 | +See the [Cache guide](/docs/og-image/guides/cache) for more caching options. |
| 112 | + |
| 113 | +## Browser Rendering |
| 114 | + |
| 115 | +[Cloudflare Browser Rendering](https://developers.cloudflare.com/browser-rendering/) enables runtime screenshots for `.browser.vue` templates. This is not needed for Takumi or Satori templates. |
| 116 | + |
| 117 | +### 1. Install the Dependency |
| 118 | + |
| 119 | +```bash |
| 120 | +pnpm add @cloudflare/puppeteer |
| 121 | +``` |
| 122 | + |
| 123 | +### 2. Add the Binding |
| 124 | + |
| 125 | +::code-group |
| 126 | + |
| 127 | +```toml [wrangler.toml] |
| 128 | +[browser] |
| 129 | +binding = "BROWSER" |
| 130 | +``` |
| 131 | + |
| 132 | +```ts [nuxt.config.ts] |
| 133 | +export default defineNuxtConfig({ |
| 134 | + ogImage: { |
| 135 | + browser: { |
| 136 | + provider: 'cloudflare', |
| 137 | + binding: 'BROWSER', |
| 138 | + }, |
| 139 | + }, |
| 140 | +}) |
| 141 | +``` |
| 142 | + |
| 143 | +:: |
| 144 | + |
| 145 | +### Rate Limits |
| 146 | + |
| 147 | +| Plan | New browsers per minute | |
| 148 | +|------|------------------------| |
| 149 | +| Free | 3 | |
| 150 | +| Paid | 30 | |
| 151 | + |
| 152 | +Sessions have a 60 second idle timeout, handled automatically by the module. See [Cloudflare Browser Rendering limits](https://developers.cloudflare.com/browser-rendering/limits/) for details. |
| 153 | + |
| 154 | +## Troubleshooting |
| 155 | + |
| 156 | +### Text missing from OG images |
| 157 | + |
| 158 | +If the background and images render but text is invisible, the `ASSETS` binding is missing. Verify your deployment follows the [setup above](#static-assets-assets-binding). |
| 159 | + |
| 160 | +To diagnose, enable `ogImage.debug` and check the `.json` variant of your OG image URL. If `fonts` is an empty array, fonts failed to load. |
| 161 | + |
| 162 | +### Fonts load locally but not in production |
| 163 | + |
| 164 | +`wrangler dev` injects the `ASSETS` binding automatically, so fonts work in local preview. Production deployments need the binding configured explicitly via `deployConfig: true` or a `wrangler.toml` with `[assets]`. |
0 commit comments