nuxt-og-image 2.0.0-beta.34 → 2.0.0-beta.36

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -52,26 +52,28 @@ Both Satori and Browser will work in Node based environments. Prerendering is fu
52
52
 
53
53
  When you want to generate dynamic images at runtime there are certain Nitro runtime limitations.
54
54
 
55
- | Provider | Satori | Browser |
56
- |------------------------------------|----------------| -- |
57
- | StackBlitz | ✅ | |
58
- | Node | ✅ | |
59
- | Vercel | ✅ | |
60
- | Vercel Edge | ✅ | ❌ |
61
- | Cloudflare Workers (requires paid) | ✅ | ❌ |
62
- | Cloudflare Pages (requires paid) | | ❌ |
63
- | Netlify | | ❌ |
64
- | Netlify Edge (TBC) | | ❌ |
65
-
66
- Other providers are yet to be tested. Please create an issue if you have tested one.
55
+ | Provider | Satori | Browser |
56
+ |---------------------------------------------------------------------------------|--------|---------|
57
+ | Node | ✅ | |
58
+ | [Vercel](https://nuxt-og-image-playground.vercel.app/) | ✅ | Soon |
59
+ | [Vercel Edge](https://nuxt-og-image-playground-gkdt.vercel.app/) | ✅ | |
60
+ | Cloudflare Workers (requires paid) | ✅ | ❌ |
61
+ | [Cloudflare Pages](https://nuxt-og-image-playground.pages.dev/) | ✅ | ❌ |
62
+ | [Netlify](https://nuxt-og-image-playground-netlify.netlify.app/) | Soon | ❌ |
63
+ | [Netlify Edge](https://nuxt-og-image-playground-netlify-edge.netlify.app/) | Soon | ❌ |
64
+ | [StackBlitz](https://stackblitz.com/edit/nuxt-starter-pxs3wk?file=package.json) | | ❌ |
65
+
66
+ Other providers are yet to be tested. Please create an issue if your nitro preset is not listed.
67
67
 
68
68
  ## Install
69
69
 
70
+ Note: The `main` branch is documentation for the beta version, it's recommended to use this version.
71
+
70
72
  ```bash
71
73
  # Install module
72
- npm install --save-dev nuxt-og-image
74
+ npm install --save-dev nuxt-og-image@beta
73
75
  # Using yarn
74
- yarn add --dev nuxt-og-image
76
+ yarn add --dev nuxt-og-image@beta
75
77
  ```
76
78
 
77
79
  ## Setup
@@ -414,12 +416,14 @@ Make sure to set the font family in your component to match the font name.
414
416
  </template>
415
417
  ```
416
418
 
417
- ## Caching
419
+ ## Runtime Caching
420
+
421
+ When images are generated at runtime, caching is enabled by default to reduce the load on your server.
418
422
 
419
- It's recommended to cache your images to reduce the load on your server. This can be achieved
420
- by providing a `runtimeCacheStorage` option to the `ogImage` config.
423
+ By default, it will use the default storage engine for your Nitro preset.
424
+ You can customise the storage engine by providing a `runtimeCacheStorage` option to the `ogImage` config.
421
425
 
422
- This `runtimeCacheStorage` option takes the same configuration as the Nuxt `nitro.storage` option.
426
+ The option takes the same configuration as the Nuxt `nitro.storage` option.
423
427
  See the [Nitro Storage Layer](https://nitro.unjs.io/guide/storage) documentation for more details.
424
428
 
425
429
  For example:
@@ -436,7 +440,7 @@ export default defineNuxtConfig({
436
440
  })
437
441
  ````
438
442
 
439
- By default, images will be cached for 24 hours. You can change the image TTL by providing `cacheTtl` when defining the image.
443
+ By default, static images will be cached for 24 hours. You can change the image TTL by providing `cacheTtl` when defining the image.
440
444
 
441
445
  ```ts
442
446
  defineOgImageStatic({
@@ -466,8 +470,18 @@ defineOgImageStatic({
466
470
  cacheKey: `${myData.id}:${myData.updatedAt}`,
467
471
  })
468
472
  </script>
469
- ````
473
+ ```
474
+
475
+ If you prefer not to cache your images you can always disable them by providing a `false` value.
470
476
 
477
+ ```ts
478
+ export default defineNuxtConfig({
479
+ ogImage: {
480
+ // no runtime cache
481
+ runtimeCacheStorage: false
482
+ }
483
+ })
484
+ ````
471
485
 
472
486
  # API
473
487
 
package/dist/module.d.ts CHANGED
@@ -59,7 +59,7 @@ interface ModuleOptions {
59
59
  * Enables debug logs and a debug endpoint.
60
60
  */
61
61
  debug: boolean;
62
- runtimeCacheStorage: false | (Record<string, any> & {
62
+ runtimeCacheStorage: boolean | (Record<string, any> & {
63
63
  driver: string;
64
64
  });
65
65
  }
package/dist/module.json CHANGED
@@ -5,5 +5,5 @@
5
5
  "bridge": false
6
6
  },
7
7
  "configKey": "ogImage",
8
- "version": "2.0.0-beta.34"
8
+ "version": "2.0.0-beta.36"
9
9
  }
package/dist/module.mjs CHANGED
@@ -238,6 +238,10 @@ const DefaultRuntimeCompatibility = {
238
238
  png: "resvg"
239
239
  };
240
240
  const RuntimeCompatibility = {
241
+ "nitro-dev": {
242
+ wasm: "fetch",
243
+ browser: "universal"
244
+ },
241
245
  "stackblitz": {
242
246
  browser: false,
243
247
  satori: "yoga-wasm",
@@ -247,6 +251,9 @@ const RuntimeCompatibility = {
247
251
  browser: "lambda",
248
252
  wasm: "inline"
249
253
  },
254
+ "netlify-edge": {
255
+ wasm: "inline"
256
+ },
250
257
  "vercel": {
251
258
  browser: "lambda"
252
259
  },
@@ -265,6 +272,24 @@ const RuntimeCompatibility = {
265
272
  }
266
273
  };
267
274
 
275
+ const autodetectableProviders = {
276
+ azure_static: "azure",
277
+ cloudflare_pages: "cloudflare-pages",
278
+ netlify: "netlify",
279
+ stormkit: "stormkit",
280
+ vercel: "vercel",
281
+ cleavr: "cleavr"
282
+ };
283
+ const autodetectableStaticProviders = {
284
+ netlify: "netlify-static",
285
+ vercel: "vercel-static"
286
+ };
287
+ function detectTarget(options = {}) {
288
+ return options?.static ? autodetectableStaticProviders[provider] : autodetectableProviders[provider];
289
+ }
290
+ function getNitroPreset(nuxt) {
291
+ return process.env.NITRO_PRESET || nuxt.options.nitro.preset || detectTarget() || "node-server";
292
+ }
268
293
  function getNitroProviderCompatibility(nuxt) {
269
294
  if (nuxt.options.dev || nuxt.options._prepare || nuxt.options._generate) {
270
295
  return defu({
@@ -274,7 +299,7 @@ function getNitroProviderCompatibility(nuxt) {
274
299
  }
275
300
  if (provider === "stackblitz")
276
301
  return defu(RuntimeCompatibility.stackblitz, DefaultRuntimeCompatibility);
277
- const target = process.env.NITRO_PRESET || nuxt.options.nitro.preset;
302
+ const target = getNitroPreset(nuxt);
278
303
  const compatibility = RuntimeCompatibility[target];
279
304
  if (compatibility === false)
280
305
  return false;
@@ -318,8 +343,11 @@ const module = defineNuxtModule({
318
343
  },
319
344
  async setup(config, nuxt) {
320
345
  const logger = useLogger("nuxt-og-image");
346
+ logger.level = config.debug ? 4 : 3;
321
347
  const { resolve } = createResolver(import.meta.url);
348
+ logger.debug("Using Nitro preset", getNitroPreset(nuxt));
322
349
  const nitroCompatibility = getNitroProviderCompatibility(nuxt);
350
+ logger.debug("Nitro compatibility", nitroCompatibility);
323
351
  if (!nitroCompatibility) {
324
352
  const target = process.env.NITRO_PRESET || nuxt.options.nitro.preset;
325
353
  logger.warn(`It looks like the nitro target ${target} doesn't support \`nuxt-og-image\`.`);
@@ -341,7 +369,7 @@ const module = defineNuxtModule({
341
369
  nuxt.options.nitro.storage["og-image"] = {
342
370
  driver: "memory"
343
371
  };
344
- } else if (config.runtimeCacheStorage && !nuxt.options.dev) {
372
+ } else if (config.runtimeCacheStorage && !nuxt.options.dev && typeof config.runtimeCacheStorage === "object") {
345
373
  nuxt.options.nitro.storage["og-image"] = config.runtimeCacheStorage;
346
374
  }
347
375
  if (!config.fonts.length)
@@ -18,6 +18,7 @@ export function defineOgImageDynamic(options = {}) {
18
18
  defineOgImage({
19
19
  provider: runtimeSatori ? "satori" : "browser",
20
20
  static: false,
21
+ cacheTtl: 0,
21
22
  ...options
22
23
  });
23
24
  }
@@ -23,10 +23,11 @@ export default defineEventHandler(async (e) => {
23
23
  statusMessage: `Provider ${options.provider} is missing.`
24
24
  });
25
25
  }
26
+ const useCache = runtimeCacheStorage && !process.dev && options.cacheTtl && options.cacheTtl > 0;
26
27
  const cache = prefixStorage(useStorage(), "og-image-cache:images");
27
28
  const key = options.cacheKey || e.node.req.url;
28
29
  let png;
29
- if (runtimeCacheStorage && await cache.hasItem(key)) {
30
+ if (useCache && await cache.hasItem(key)) {
30
31
  const { value, expiresAt } = await cache.getItem(key);
31
32
  if (expiresAt > Date.now()) {
32
33
  setHeader(e, "Cache-Control", "public, max-age=31536000");
@@ -39,7 +40,7 @@ export default defineEventHandler(async (e) => {
39
40
  if (!png) {
40
41
  try {
41
42
  png = await provider.createPng(withBase(basePath, useHostname(e)), options);
42
- if (png && runtimeCacheStorage && options.static) {
43
+ if (useCache && png) {
43
44
  const base64png = Buffer.from(png).toString("base64");
44
45
  await cache.setItem(key, { value: base64png, expiresAt: Date.now() + (options.cacheTtl || 0) });
45
46
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "nuxt-og-image",
3
3
  "type": "module",
4
- "version": "2.0.0-beta.34",
4
+ "version": "2.0.0-beta.36",
5
5
  "packageManager": "pnpm@8.1.0",
6
6
  "license": "MIT",
7
7
  "funding": "https://github.com/sponsors/harlan-zw",