nuxt-og-image 2.0.0-beta.4 → 2.0.0-beta.40

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.
Files changed (82) hide show
  1. package/README.md +245 -47
  2. package/dist/client/200.html +2 -2
  3. package/dist/client/404.html +2 -2
  4. package/dist/client/_nuxt/IconCSS.1dce7adc.js +1 -0
  5. package/dist/client/_nuxt/IconCSS.f16db5c2.css +1 -0
  6. package/dist/client/_nuxt/ImageLoader.163d23b3.js +1 -0
  7. package/dist/client/_nuxt/ImageLoader.7571516f.css +1 -0
  8. package/dist/client/_nuxt/entry.23646169.js +5 -0
  9. package/dist/client/_nuxt/entry.3d2654cc.css +1 -0
  10. package/dist/client/_nuxt/{error-404.1ff52902.js → error-404.c0f2dfff.js} +1 -1
  11. package/dist/client/_nuxt/{error-500.f7d30da5.js → error-500.6a9dcc3d.js} +1 -1
  12. package/dist/client/_nuxt/{error-component.cf7543e5.js → error-component.0db07bb1.js} +2 -2
  13. package/dist/client/_nuxt/index.403133d8.css +1 -0
  14. package/dist/client/_nuxt/index.531c9ed8.js +1 -0
  15. package/dist/client/_nuxt/{options.56a3e5f9.js → options.fd71c26a.js} +1 -1
  16. package/dist/client/_nuxt/{png.37f3e77b.js → png.5b6830ea.js} +1 -1
  17. package/dist/client/_nuxt/{shiki.3a930bb8.js → shiki.1be71764.js} +1 -1
  18. package/dist/client/_nuxt/{svg.186c6bd1.js → svg.165cf4fc.js} +1 -1
  19. package/dist/client/_nuxt/{vnodes.a799f183.js → vnodes.11ff70a7.js} +1 -1
  20. package/dist/client/index.html +2 -2
  21. package/dist/client/options/index.html +2 -2
  22. package/dist/client/png/index.html +2 -2
  23. package/dist/client/svg/index.html +2 -2
  24. package/dist/client/vnodes/index.html +2 -2
  25. package/dist/module.d.ts +21 -7
  26. package/dist/module.json +1 -1
  27. package/dist/module.mjs +283 -112
  28. package/dist/runtime/browserUtil.d.ts +1 -0
  29. package/dist/runtime/browserUtil.mjs +10 -5
  30. package/dist/runtime/composables/defineOgImage.mjs +15 -12
  31. package/dist/runtime/nitro/middleware/og.png.mjs +51 -7
  32. package/dist/runtime/nitro/middleware/playground.mjs +4 -3
  33. package/dist/runtime/nitro/plugins/prerender.d.ts +3 -0
  34. package/dist/runtime/nitro/plugins/prerender.mjs +26 -0
  35. package/dist/runtime/nitro/providers/browser/lambda.d.ts +1 -1
  36. package/dist/runtime/nitro/providers/browser/lambda.mjs +3 -3
  37. package/dist/runtime/nitro/providers/browser/{node.mjs → playwright.mjs} +0 -9
  38. package/dist/runtime/nitro/providers/browser/universal.d.ts +1 -0
  39. package/dist/runtime/nitro/providers/browser/universal.mjs +33 -0
  40. package/dist/runtime/nitro/providers/png/resvg-node.d.ts +5 -0
  41. package/dist/runtime/nitro/providers/png/resvg-node.mjs +6 -0
  42. package/dist/runtime/nitro/providers/png/resvg-wasm.d.ts +4 -0
  43. package/dist/runtime/nitro/providers/png/resvg-wasm.mjs +11 -0
  44. package/dist/runtime/nitro/providers/png/svg2png.mjs +11 -0
  45. package/dist/runtime/nitro/providers/satori/{webworker.mjs → yoga-wasm.mjs} +4 -5
  46. package/dist/runtime/nitro/renderers/browser.mjs +12 -6
  47. package/dist/runtime/nitro/renderers/satori/index.mjs +20 -14
  48. package/dist/runtime/nitro/renderers/satori/plugins/encoding.mjs +2 -1
  49. package/dist/runtime/nitro/renderers/satori/plugins/imageSrc.mjs +25 -3
  50. package/dist/runtime/nitro/renderers/satori/utils.d.ts +2 -2
  51. package/dist/runtime/nitro/renderers/satori/utils.mjs +25 -12
  52. package/dist/runtime/nitro/routes/debug.d.ts +4 -0
  53. package/dist/runtime/nitro/routes/debug.mjs +9 -0
  54. package/dist/runtime/nitro/routes/html.mjs +52 -14
  55. package/dist/runtime/nitro/routes/options.mjs +18 -15
  56. package/dist/runtime/nitro/routes/svg.mjs +3 -1
  57. package/dist/runtime/nitro/routes/vnode.mjs +3 -1
  58. package/dist/runtime/nitro/util-hostname.d.ts +2 -0
  59. package/dist/runtime/nitro/util-hostname.mjs +15 -0
  60. package/dist/runtime/nitro/utils-pure.d.ts +3 -2
  61. package/dist/runtime/nitro/utils-pure.mjs +9 -8
  62. package/dist/runtime/nitro/utils.d.ts +6 -8
  63. package/dist/runtime/nitro/utils.mjs +50 -47
  64. package/dist/runtime/public-assets/__nuxt_og_image__/browser-provider-not-supported.png +0 -0
  65. package/dist/runtime/public-assets-optional/resvg/resvg.wasm +0 -0
  66. package/dist/types.d.ts +6 -0
  67. package/package.json +32 -22
  68. package/dist/client/_nuxt/IconCSS.a041aca0.js +0 -1
  69. package/dist/client/_nuxt/ImageLoader.9bf39d71.js +0 -1
  70. package/dist/client/_nuxt/entry.74018bda.js +0 -5
  71. package/dist/client/_nuxt/entry.7a8c1ab2.css +0 -1
  72. package/dist/client/_nuxt/index.3f356409.js +0 -1
  73. package/dist/runtime/nitro/providers/svg2png/universal.mjs +0 -9
  74. /package/dist/runtime/nitro/providers/browser/{node.d.ts → playwright.d.ts} +0 -0
  75. /package/dist/runtime/nitro/providers/{svg2png/universal.d.ts → png/svg2png.d.ts} +0 -0
  76. /package/dist/runtime/nitro/providers/satori/{node.d.ts → default.d.ts} +0 -0
  77. /package/dist/runtime/nitro/providers/satori/{node.mjs → default.mjs} +0 -0
  78. /package/dist/runtime/nitro/providers/satori/{webworker.d.ts → yoga-wasm.d.ts} +0 -0
  79. /package/dist/runtime/{public-assets → public-assets-optional/inter-font}/inter-latin-ext-400-normal.woff +0 -0
  80. /package/dist/runtime/{public-assets → public-assets-optional/inter-font}/inter-latin-ext-700-normal.woff +0 -0
  81. /package/dist/runtime/{public-assets → public-assets-optional/svg2png}/svg2png.wasm +0 -0
  82. /package/dist/runtime/{public-assets → public-assets-optional/yoga}/yoga.wasm +0 -0
package/dist/module.mjs CHANGED
@@ -1,29 +1,33 @@
1
1
  import { readFile, writeFile } from 'node:fs/promises';
2
- import { defineNuxtModule, createResolver, addTemplate, addServerHandler, addImports, addComponent } from '@nuxt/kit';
2
+ import { defineNuxtModule, useLogger, createResolver, addTemplate, addServerHandler, addImports, addComponent, addServerPlugin } from '@nuxt/kit';
3
3
  import { execa } from 'execa';
4
4
  import chalk from 'chalk';
5
5
  import defu from 'defu';
6
6
  import { toRouteMatcher, createRouter } from 'radix3';
7
- import { joinURL } from 'ufo';
8
- import { resolve, relative } from 'pathe';
7
+ import { withBase, joinURL } from 'ufo';
8
+ import { resolve, relative, dirname } from 'pathe';
9
9
  import { tinyws } from 'tinyws';
10
10
  import sirv from 'sirv';
11
11
  import { pathExists, copy, mkdirp } from 'fs-extra';
12
- import { provider } from 'std-env';
12
+ import { globby } from 'globby';
13
13
  import playwrightCore from 'playwright-core';
14
14
  import { existsSync } from 'node:fs';
15
15
  import { createBirpcGroup } from 'birpc';
16
16
  import { stringify, parse } from 'flatted';
17
+ import { addDependency } from 'nypm';
18
+ import { provider } from 'std-env';
17
19
 
18
20
  async function createBrowser() {
19
- try {
20
- const { Launcher } = await import(String("chrome-launcher"));
21
- const chromePath = Launcher.getFirstInstallation();
22
- return await playwrightCore.chromium.launch({
23
- headless: true,
24
- executablePath: chromePath
25
- });
26
- } catch (e) {
21
+ if (process.dev || process.env.prerender) {
22
+ try {
23
+ const { Launcher } = await import(String("chrome-launcher"));
24
+ const chromePath = Launcher.getFirstInstallation();
25
+ return await playwrightCore.chromium.launch({
26
+ headless: true,
27
+ executablePath: chromePath
28
+ });
29
+ } catch (e) {
30
+ }
27
31
  }
28
32
  try {
29
33
  return await playwrightCore.chromium.launch({
@@ -54,9 +58,9 @@ async function screenshot(browser, options) {
54
58
  width: options.width || 1200,
55
59
  height: options.height || 630
56
60
  });
57
- const isHtml = options.path.startsWith("html:") || options.html;
61
+ const isHtml = options.html || options.path?.startsWith("html:");
58
62
  if (isHtml) {
59
- const html = options.html || options.path.substring(5);
63
+ const html = options.html || options.path?.substring(5);
60
64
  await page.evaluate((html2) => {
61
65
  document.open("text/html");
62
66
  document.write(html2);
@@ -65,10 +69,13 @@ async function screenshot(browser, options) {
65
69
  await page.waitForLoadState("networkidle");
66
70
  } else {
67
71
  await page.goto(`${options.host}${options.path}`, {
68
- timeout: 1e4,
72
+ timeout: process.env.prerender || process.dev ? 1e4 : 3500,
69
73
  waitUntil: "networkidle"
70
74
  });
71
75
  }
76
+ const screenshotOptions = {
77
+ timeout: process.env.prerender || process.dev ? 1e4 : 3500
78
+ };
72
79
  if (options.delay)
73
80
  await page.waitForTimeout(options.delay);
74
81
  if (options.mask) {
@@ -78,8 +85,10 @@ async function screenshot(browser, options) {
78
85
  }, options.mask);
79
86
  }
80
87
  if (options.selector)
81
- return await page.locator(options.selector).screenshot();
82
- return await page.screenshot();
88
+ return await page.locator(options.selector).screenshot(screenshotOptions);
89
+ const screenshot2 = await page.screenshot(screenshotOptions);
90
+ await page.close();
91
+ return screenshot2;
83
92
  }
84
93
 
85
94
  function setupPlaygroundRPC(nuxt, config) {
@@ -113,7 +122,7 @@ function setupPlaygroundRPC(nuxt, config) {
113
122
  const birpc = createBirpcGroup(serverFunctions, []);
114
123
  nuxt.hook("builder:watch", (e, path) => {
115
124
  if (e === "change")
116
- birpc.boardcast.refresh.asEvent(path);
125
+ birpc.broadcast.refresh.asEvent(path);
117
126
  });
118
127
  const middleware = async (req, res) => {
119
128
  if (req.ws) {
@@ -164,11 +173,15 @@ function getBodyJson(req) {
164
173
  });
165
174
  }
166
175
 
167
- function decodeHtmlEntities(obj) {
176
+ function decodeHtml(html) {
177
+ return html.replace(/&lt;/g, "<").replace(/&gt;/g, ">").replace(/&amp;/g, "&").replace(/&cent;/g, "\xA2").replace(/&pound;/g, "\xA3").replace(/&yen;/g, "\xA5").replace(/&euro;/g, "\u20AC").replace(/&copy;/g, "\xA9").replace(/&reg;/g, "\xAE").replace(/&quot;/g, '"').replace(/&#39;/g, "'").replace(/&#x27;/g, "'").replace(/&#x2F;/g, "/").replace(/&#([0-9]+);/g, (full, int) => {
178
+ return String.fromCharCode(parseInt(int));
179
+ });
180
+ }
181
+ function decodeObjectHtmlEntities(obj) {
168
182
  Object.entries(obj).forEach(([key, value]) => {
169
- if (typeof value === "string") {
170
- obj[key] = value.replace(/&lt;/g, "<").replace(/&gt;/g, ">").replace(/&amp;/g, "&").replace(/&quot;/g, '"').replace(/&#x27;/g, "'").replace(/&#x2F;/g, "/");
171
- }
183
+ if (typeof value === "string")
184
+ obj[key] = decodeHtml(value);
172
185
  });
173
186
  return obj;
174
187
  }
@@ -192,22 +205,120 @@ function extractOgImageOptions(html) {
192
205
  else
193
206
  options.description = html.match(/<meta name="description" content="(.*?)">/)?.[1];
194
207
  }
195
- return decodeHtmlEntities(options);
208
+ return decodeObjectHtmlEntities(options);
196
209
  }
197
210
  return false;
198
211
  }
199
- function stripOgImageOptions(html) {
200
- return html.replace(/<script id="nuxt-og-image-options" type="application\/json">(.*?)<\/script>/, "");
212
+
213
+ const SVG2PNGWasmPlaceholder = '"/* NUXT_OG_IMAGE_SVG2PNG_WASM */"';
214
+ const YogaWasmPlaceholder = '"/* NUXT_OG_IMAGE_YOGA_WASM */"';
215
+ const ReSVGWasmPlaceholder = '"/* NUXT_OG_IMAGE_RESVG_WASM */"';
216
+ const Wasms = [
217
+ {
218
+ placeholder: SVG2PNGWasmPlaceholder,
219
+ path: "svg2png/svg2png.wasm",
220
+ file: "svg2png.wasm"
221
+ },
222
+ {
223
+ placeholder: ReSVGWasmPlaceholder,
224
+ path: "resvg/resvg.wasm",
225
+ file: "resvg.wasm"
226
+ },
227
+ {
228
+ placeholder: YogaWasmPlaceholder,
229
+ path: "yoga/yoga.wasm",
230
+ file: "yoga.wasm"
231
+ }
232
+ ];
233
+ const DefaultRuntimeCompatibility = {
234
+ // node-server runtime
235
+ browser: "playwright",
236
+ satori: "default",
237
+ wasm: "fetch",
238
+ png: "resvg-node"
239
+ };
240
+ const RuntimeCompatibility = {
241
+ "nitro-dev": {
242
+ wasm: "fetch",
243
+ browser: "universal"
244
+ },
245
+ "stackblitz": {
246
+ browser: false,
247
+ satori: "yoga-wasm",
248
+ wasm: "inline",
249
+ png: "resvg-wasm"
250
+ },
251
+ "netlify": {
252
+ browser: "lambda",
253
+ wasm: "inline"
254
+ },
255
+ "netlify-edge": {
256
+ wasm: "inline",
257
+ png: "resvg-wasm"
258
+ },
259
+ "vercel": {
260
+ // exceeds 50mb limit
261
+ browser: false
262
+ },
263
+ "vercel-edge": {
264
+ browser: false,
265
+ wasm: "import",
266
+ wasmImportQuery: "?module",
267
+ png: "resvg-wasm"
268
+ },
269
+ "cloudflare-pages": {
270
+ browser: false,
271
+ wasm: "import",
272
+ png: "resvg-wasm"
273
+ },
274
+ "cloudflare": {
275
+ browser: false,
276
+ wasm: "import"
277
+ }
278
+ };
279
+
280
+ const autodetectableProviders = {
281
+ azure_static: "azure",
282
+ cloudflare_pages: "cloudflare-pages",
283
+ netlify: "netlify",
284
+ stormkit: "stormkit",
285
+ vercel: "vercel",
286
+ cleavr: "cleavr"
287
+ };
288
+ const autodetectableStaticProviders = {
289
+ netlify: "netlify-static",
290
+ vercel: "vercel-static"
291
+ };
292
+ function detectTarget(options = {}) {
293
+ return options?.static ? autodetectableStaticProviders[provider] : autodetectableProviders[provider];
294
+ }
295
+ function getNitroPreset(nuxt) {
296
+ return process.env.NITRO_PRESET || nuxt.options.nitro.preset || detectTarget() || "node-server";
297
+ }
298
+ function getNitroProviderCompatibility(nuxt) {
299
+ if (nuxt.options.dev || nuxt.options._prepare || nuxt.options._generate) {
300
+ return defu({
301
+ wasm: "fetch",
302
+ browser: "universal"
303
+ }, DefaultRuntimeCompatibility);
304
+ }
305
+ if (provider === "stackblitz")
306
+ return defu(RuntimeCompatibility.stackblitz, DefaultRuntimeCompatibility);
307
+ const target = getNitroPreset(nuxt);
308
+ const compatibility = RuntimeCompatibility[target];
309
+ if (compatibility === false)
310
+ return false;
311
+ return defu(compatibility || {}, DefaultRuntimeCompatibility);
312
+ }
313
+ function ensureDependencies(nuxt, dep) {
314
+ return Promise.all(dep.map((d) => {
315
+ return addDependency(d, { cwd: nuxt.options.rootDir });
316
+ }));
201
317
  }
202
318
 
203
319
  const PATH = "/__nuxt_og_image__";
204
320
  const PATH_ENTRY = `${PATH}/entry`;
205
321
  const PATH_PLAYGROUND = `${PATH}/client`;
206
- const edgeProvidersSupported = [
207
- "cloudflare",
208
- "vercel-edge",
209
- "netlify-edge"
210
- ];
211
322
  const module = defineNuxtModule({
212
323
  meta: {
213
324
  name: "nuxt-og-image",
@@ -220,26 +331,54 @@ const module = defineNuxtModule({
220
331
  defaults(nuxt) {
221
332
  const siteUrl = process.env.NUXT_PUBLIC_SITE_URL || process.env.NUXT_SITE_URL || nuxt.options.runtimeConfig.public?.siteUrl || nuxt.options.runtimeConfig.siteUrl;
222
333
  return {
223
- // when we run `nuxi generate` we need to force prerendering
224
- forcePrerender: !nuxt.options.dev && nuxt.options._generate,
225
334
  siteUrl,
226
335
  defaults: {
227
336
  component: "OgImageBasic",
228
337
  width: 1200,
229
- height: 630
338
+ height: 630,
339
+ cacheTtl: 24 * 60 * 60 * 1e3
340
+ // default is to cache the image for 24 hours
230
341
  },
231
- satoriProvider: true,
232
- browserProvider: true,
342
+ runtimeSatori: true,
343
+ runtimeBrowser: nuxt.options.dev,
233
344
  fonts: [],
345
+ runtimeCacheStorage: false,
234
346
  satoriOptions: {},
235
- experimentalInlineWasm: process.env.NITRO_PRESET === "netlify-edge" || nuxt.options.nitro.preset === "netlify-edge" || false,
236
- experimentalRuntimeBrowser: false,
237
- playground: process.env.NODE_ENV === "development" || nuxt.options.dev
347
+ playground: process.env.NODE_ENV === "development" || nuxt.options.dev,
348
+ debug: false
238
349
  };
239
350
  },
240
351
  async setup(config, nuxt) {
352
+ const logger = useLogger("nuxt-og-image");
353
+ logger.level = config.debug ? 4 : 3;
241
354
  const { resolve } = createResolver(import.meta.url);
355
+ logger.debug("Using Nitro preset", getNitroPreset(nuxt));
356
+ const nitroCompatibility = getNitroProviderCompatibility(nuxt);
357
+ logger.debug("Nitro compatibility", nitroCompatibility);
358
+ if (!nitroCompatibility) {
359
+ const target = process.env.NITRO_PRESET || nuxt.options.nitro.preset;
360
+ logger.warn(`It looks like the nitro target ${target} doesn't support \`nuxt-og-image\`.`);
361
+ return;
362
+ }
363
+ if (!nitroCompatibility.browser && config.runtimeBrowser) {
364
+ config.runtimeBrowser = false;
365
+ logger.warn("It looks like you're using a nitro target that does not support the browser provider, disabling `runtimeBrowser`.");
366
+ }
367
+ if (config.runtimeBrowser && nitroCompatibility.browser === "lambda") {
368
+ logger.info("It looks like you're deploying to an environment that has extra requirements, checking for dependencies...");
369
+ await ensureDependencies(nuxt, ["puppeteer-core@14.1.1", "@sparticuz/chrome-aws-lambda@14.1.1"]);
370
+ }
242
371
  config.siteUrl = config.siteUrl || config.host;
372
+ if (!nuxt.options.dev && nuxt.options._generate && !config.siteUrl)
373
+ logger.warn("Missing `ogImage.siteUrl` and site is being prerendered. This will result in broken og images.");
374
+ nuxt.options.nitro.storage = nuxt.options.nitro.storage || {};
375
+ if (nuxt.options._generate) {
376
+ nuxt.options.nitro.storage["og-image"] = {
377
+ driver: "memory"
378
+ };
379
+ } else if (config.runtimeCacheStorage && !nuxt.options.dev && typeof config.runtimeCacheStorage === "object") {
380
+ nuxt.options.nitro.storage["og-image"] = config.runtimeCacheStorage;
381
+ }
243
382
  if (!config.fonts.length)
244
383
  config.fonts = ["Inter:400", "Inter:700"];
245
384
  const distResolve = (p) => {
@@ -271,12 +410,14 @@ export {}
271
410
  lazy: true,
272
411
  handler: resolve("./runtime/nitro/middleware/og.png")
273
412
  });
274
- ["html", "options", "svg", "vnode", "font"].forEach((type) => {
275
- addServerHandler({
276
- lazy: true,
277
- route: `/api/og-image-${type}`,
278
- handler: resolve(`./runtime/nitro/routes/${type}`)
279
- });
413
+ ["html", "options", "svg", "vnode", "font", "debug"].forEach((type) => {
414
+ if (type !== "debug" || config.debug) {
415
+ addServerHandler({
416
+ lazy: true,
417
+ route: `/api/og-image-${type}`,
418
+ handler: resolve(`./runtime/nitro/routes/${type}`)
419
+ });
420
+ }
280
421
  });
281
422
  nuxt.hook("devtools:customTabs", (iframeTabs) => {
282
423
  iframeTabs.push({
@@ -325,92 +466,123 @@ export {}
325
466
  });
326
467
  const runtimeDir = resolve("./runtime");
327
468
  nuxt.options.build.transpile.push(runtimeDir);
328
- const moduleAssetDir = resolve("./runtime/public-assets");
329
- const assetDirs = [
330
- resolve(nuxt.options.rootDir, nuxt.options.dir.public),
331
- moduleAssetDir
469
+ addServerPlugin(resolve("./runtime/nitro/plugins/prerender"));
470
+ const customAssetDirs = [
471
+ // allows us to show custom error images
472
+ resolve("./runtime/public-assets")
332
473
  ];
474
+ if (config.runtimeSatori) {
475
+ if (config.fonts.includes("Inter:400"))
476
+ customAssetDirs.push(resolve("./runtime/public-assets-optional/inter-font"));
477
+ if (nitroCompatibility.png === "resvg-wasm" && nitroCompatibility.wasm === "fetch")
478
+ customAssetDirs.push(resolve("./runtime/public-assets-optional/resvg"));
479
+ else if (nitroCompatibility.png === "svg2png" && nitroCompatibility.wasm === "fetch")
480
+ customAssetDirs.push(resolve("./runtime/public-assets-optional/svg2png"));
481
+ if (nitroCompatibility.satori === "yoga-wasm")
482
+ customAssetDirs.push(resolve("./runtime/public-assets-optional/yoga"));
483
+ }
333
484
  nuxt.hooks.hook("modules:done", async () => {
334
485
  nuxt.hooks.callHook("og-image:config", config);
335
- nuxt.options.runtimeConfig["nuxt-og-image"] = { ...config, assetDirs };
486
+ nuxt.options.runtimeConfig["nuxt-og-image"] = {
487
+ ...config,
488
+ // avoid adding credentials
489
+ runtimeCacheStorage: Boolean(config.runtimeCacheStorage),
490
+ assetDirs: [
491
+ resolve(nuxt.options.rootDir, nuxt.options.dir.public),
492
+ ...customAssetDirs,
493
+ // always add runtime dirs for prerendering to work, these are just used as scan roots
494
+ resolve("./runtime/public-assets-optional/inter-font"),
495
+ resolve("./runtime/public-assets-optional/resvg"),
496
+ resolve("./runtime/public-assets-optional/yoga"),
497
+ resolve("./runtime/public-assets-optional/svg2png")
498
+ ]
499
+ };
336
500
  });
337
- const useSatoriWasm = provider === "stackblitz";
338
501
  nuxt.hooks.hook("nitro:config", async (nitroConfig) => {
339
502
  nitroConfig.externals = defu(nitroConfig.externals || {}, {
340
503
  inline: [runtimeDir]
341
504
  });
342
- if (config.experimentalRuntimeBrowser) {
505
+ if (config.runtimeBrowser) {
343
506
  nitroConfig.alias = nitroConfig.alias || {};
344
507
  nitroConfig.alias.electron = "unenv/runtime/mock/proxy-cjs";
345
508
  nitroConfig.alias.bufferutil = "unenv/runtime/mock/proxy-cjs";
346
509
  nitroConfig.alias["utf-8-validate"] = "unenv/runtime/mock/proxy-cjs";
347
510
  }
348
511
  nitroConfig.publicAssets = nitroConfig.publicAssets || [];
349
- nitroConfig.publicAssets.push({ dir: moduleAssetDir, maxAge: 31536e3 });
512
+ customAssetDirs.forEach((dir) => {
513
+ nitroConfig.publicAssets.push({ dir, maxAge: 31536e3 });
514
+ });
350
515
  const providerPath = `${runtimeDir}/nitro/providers`;
351
- if (config.browserProvider) {
352
- nitroConfig.virtual["#nuxt-og-image/browser"] = nuxt.options.dev || config.experimentalRuntimeBrowser ? `
353
- import node from '${providerPath}/browser/node'
354
-
516
+ if (config.runtimeBrowser) {
517
+ nitroConfig.virtual["#nuxt-og-image/browser"] = `
518
+ let browser
355
519
  export default async function() {
356
- return node
357
- }
358
- ` : `export default async function() {
359
- return () => {}
520
+ browser = browser || await import('${providerPath}/browser/${nitroCompatibility.browser}').then((m) => m.default || m)
521
+ return browser
360
522
  }
361
523
  `;
362
524
  }
363
- if (config.satoriProvider) {
364
- nitroConfig.virtual["#nuxt-og-image/satori"] = `import satori from '${providerPath}/satori/${useSatoriWasm ? "webworker" : "node"}'
525
+ if (config.runtimeSatori) {
526
+ nitroConfig.virtual["#nuxt-og-image/satori"] = `import satori from '${providerPath}/satori/${nitroCompatibility.satori}'
365
527
  export default async function() {
366
528
  return satori
367
529
  }`;
368
- nitroConfig.virtual["#nuxt-og-image/svg2png"] = `
369
- import svg2png from '${providerPath}/svg2png/universal'
530
+ nitroConfig.virtual["#nuxt-og-image/png"] = `import png from '${providerPath}/png/${nitroCompatibility.png}'
370
531
  export default async function() {
371
- return svg2png
372
- }`;
532
+ return png
533
+ }
534
+ `;
373
535
  }
374
536
  nitroConfig.virtual["#nuxt-og-image/provider"] = `
375
- import satori from '${relative(nuxt.options.rootDir, resolve("./runtime/nitro/renderers/satori"))}'
376
- import browser from '${relative(nuxt.options.rootDir, resolve("./runtime/nitro/renderers/browser"))}'
537
+ ${config.runtimeSatori ? `import satori from '${relative(nuxt.options.rootDir, resolve("./runtime/nitro/renderers/satori"))}'` : ""}
538
+ ${config.runtimeBrowser ? `import browser from '${relative(nuxt.options.rootDir, resolve("./runtime/nitro/renderers/browser"))}'` : ""}
377
539
 
378
540
  export async function useProvider(provider) {
379
541
  if (provider === 'satori')
380
- return satori
542
+ return ${config.runtimeSatori ? "satori" : "null"}
381
543
  if (provider === 'browser')
382
- return browser
544
+ return ${config.runtimeBrowser ? "browser" : "null"}
545
+ return null
383
546
  }
384
547
  `;
385
548
  });
386
549
  nuxt.hooks.hook("nitro:init", async (nitro) => {
387
550
  let screenshotQueue = [];
388
551
  nitro.hooks.hook("compiled", async (_nitro) => {
389
- if (edgeProvidersSupported.includes(_nitro.options.preset)) {
390
- await copy(resolve("./runtime/public-assets/inter-latin-ext-400-normal.woff"), resolve(_nitro.options.output.publicDir, "inter-latin-ext-400-normal.woff"));
391
- await copy(resolve("./runtime/public-assets/inter-latin-ext-700-normal.woff"), resolve(_nitro.options.output.publicDir, "inter-latin-ext-700-normal.woff"));
392
- if (!config.experimentalInlineWasm) {
393
- await copy(resolve("./runtime/public-assets/svg2png.wasm"), resolve(_nitro.options.output.serverDir, "svg2png.wasm"));
394
- if (useSatoriWasm)
395
- await copy(resolve("./runtime/public-assets/yoga.wasm"), resolve(_nitro.options.output.serverDir, "yoga.wasm"));
552
+ if (!config.runtimeSatori || nuxt.options.dev)
553
+ return;
554
+ if (config.fonts.includes("Inter:400"))
555
+ await copy(resolve("./runtime/public-assets-optional/inter-font/inter-latin-ext-400-normal.woff"), resolve(_nitro.options.output.publicDir, "inter-latin-ext-400-normal.woff"));
556
+ if (config.fonts.includes("Inter:700"))
557
+ await copy(resolve("./runtime/public-assets-optional/inter-font/inter-latin-ext-700-normal.woff"), resolve(_nitro.options.output.publicDir, "inter-latin-ext-700-normal.woff"));
558
+ const configuredEntry = nitro.options.rollupConfig?.output.entryFileNames;
559
+ const wasmProviderPath = resolve(_nitro.options.output.serverDir, typeof configuredEntry === "string" ? configuredEntry : "index.mjs");
560
+ const paths = [wasmProviderPath];
561
+ const chunks = await globby([`${_nitro.options.output.serverDir}/chunks/**/*.mjs`], { absolute: true });
562
+ paths.push(...chunks);
563
+ for (const path of paths) {
564
+ if (!await pathExists(path))
565
+ continue;
566
+ let contents = await readFile(path, "utf-8");
567
+ let updated = false;
568
+ if (_nitro.options.preset.includes("vercel") && path === wasmProviderPath) {
569
+ contents = contents.replace(".cwd(),", '?.cwd || "/",');
570
+ updated = true;
396
571
  }
397
- const indexFile = resolve(_nitro.options.output.serverDir, _nitro.options.preset === "netlify-edge" ? "server.mjs" : "index.mjs");
398
- if (await pathExists(indexFile)) {
399
- const indexContents = (await readFile(indexFile, "utf-8")).replace(".cwd(),", '?.cwd || "/",');
400
- if (!config.experimentalInlineWasm) {
401
- await writeFile(
402
- indexFile,
403
- indexContents.replace('"/* NUXT_OG_IMAGE_SVG2PNG_WASM */"', 'import("./svg2png.wasm").then(m => m.default || m)').replace('"/* NUXT_OG_IMAGE_YOGA_WASM */"', 'import("./yoga.wasm").then(m => m.default || m)')
404
- );
405
- } else {
406
- const svg2pngWasm = await readFile(resolve("./runtime/public-assets/svg2png.wasm"), "base64");
407
- const yogaWasm = await readFile(resolve("./runtime/public-assets/yoga.wasm"), "base64");
408
- await writeFile(
409
- indexFile,
410
- indexContents.replace('"/* NUXT_OG_IMAGE_SVG2PNG_WASM */"', `Buffer.from("${svg2pngWasm}", "base64")`).replace('"/* NUXT_OG_IMAGE_YOGA_WASM */"', `Buffer.from("${yogaWasm}", "base64")`)
411
- );
572
+ for (const wasm of Wasms) {
573
+ if (contents.includes(wasm.placeholder)) {
574
+ if (nitroCompatibility.wasm === "import") {
575
+ contents = contents.replace(wasm.placeholder, `import("./${wasm.file}${nitroCompatibility.wasmImportQuery || ""}").then(m => m.default || m)`);
576
+ await copy(resolve(`./runtime/public-assets-optional/${wasm.path}`), resolve(dirname(path), wasm.file));
577
+ } else if (nitroCompatibility.wasm === "inline") {
578
+ const wasmBuffer = await readFile(resolve(`./runtime/public-assets-optional/${wasm.path}`));
579
+ contents = contents.replace(wasm.placeholder, `Buffer.from("${wasmBuffer}", "base64")`);
580
+ }
581
+ updated = true;
412
582
  }
413
583
  }
584
+ if (updated)
585
+ await writeFile(path, contents, { encoding: "utf-8" });
414
586
  }
415
587
  });
416
588
  const _routeRulesMatcher = toRouteMatcher(
@@ -423,7 +595,6 @@ export async function useProvider(provider) {
423
595
  if (!html)
424
596
  return;
425
597
  const extractedOptions = extractOgImageOptions(html);
426
- ctx.contents = stripOgImageOptions(html);
427
598
  const routeRules = defu({}, ..._routeRulesMatcher.matchAll(ctx.route).reverse());
428
599
  if (!extractedOptions || routeRules.ogImage === false)
429
600
  return;
@@ -442,21 +613,6 @@ export async function useProvider(provider) {
442
613
  await nuxt.callHook("og-image:prerenderScreenshots", screenshotQueue);
443
614
  if (screenshotQueue.length === 0)
444
615
  return;
445
- for (const entry of screenshotQueue) {
446
- if (entry.route && Object.keys(entry).length === 1) {
447
- const html = await $fetch(entry.route);
448
- const extractedOptions = extractOgImageOptions(html);
449
- const routeRules = defu({}, ..._routeRulesMatcher.matchAll(entry.route).reverse());
450
- Object.assign(entry, {
451
- // @ts-expect-error runtime
452
- path: extractedOptions.component ? `/api/og-image-html?path=${entry.route}` : entry.route,
453
- ...extractedOptions,
454
- ...routeRules.ogImage || {}
455
- });
456
- }
457
- if (entry.component)
458
- entry.html = await $fetch(entry.path);
459
- }
460
616
  nitro.logger.info("Ensuring chromium install for og:image generation...");
461
617
  const installChromeProcess = execa("npx", ["playwright", "install", "chromium"], {
462
618
  stdio: "inherit"
@@ -483,12 +639,27 @@ export async function useProvider(provider) {
483
639
  browser = await createBrowser();
484
640
  if (browser) {
485
641
  nitro.logger.info(`Prerendering ${screenshotQueue.length} og:image screenshots...`);
642
+ for (const entry of screenshotQueue) {
643
+ if (entry.route && Object.keys(entry).length === 1) {
644
+ const html = await $fetch(entry.route, { baseURL: withBase(nuxt.options.app.baseURL, host) });
645
+ const extractedOptions = extractOgImageOptions(html);
646
+ const routeRules = defu({}, ..._routeRulesMatcher.matchAll(entry.route).reverse());
647
+ Object.assign(entry, {
648
+ // @ts-expect-error runtime
649
+ path: extractedOptions.component ? `/api/og-image-html?path=${entry.route}` : entry.route,
650
+ ...extractedOptions,
651
+ ...routeRules.ogImage || {}
652
+ });
653
+ }
654
+ if (entry.component)
655
+ entry.html = await globalThis.$fetch(entry.path);
656
+ }
486
657
  for (const k in screenshotQueue) {
487
658
  const entry = screenshotQueue[k];
488
659
  const start = Date.now();
489
660
  let hasError = false;
490
- const dirname = joinURL(nitro.options.output.publicDir, entry.route, "/__og_image__/");
491
- const filename = joinURL(dirname, "/og.png");
661
+ const dirname2 = joinURL(nitro.options.output.publicDir, entry.route, "/__og_image__/");
662
+ const filename = joinURL(dirname2, "/og.png");
492
663
  try {
493
664
  const imgBuffer = await screenshot(browser, {
494
665
  ...config.defaults || {},
@@ -496,7 +667,7 @@ export async function useProvider(provider) {
496
667
  host
497
668
  });
498
669
  try {
499
- await mkdirp(dirname);
670
+ await mkdirp(dirname2);
500
671
  } catch (e) {
501
672
  }
502
673
  await writeFile(filename, imgBuffer);
@@ -520,7 +691,7 @@ export async function useProvider(provider) {
520
691
  }
521
692
  screenshotQueue = [];
522
693
  };
523
- if (!nuxt.options._prepare) {
694
+ if (nuxt.options._generate) {
524
695
  nitro.hooks.hook("rollup:before", async () => {
525
696
  await captureScreenshots();
526
697
  });
@@ -1,4 +1,5 @@
1
1
  /// <reference types="node" />
2
+ import type { Buffer } from 'node:buffer';
2
3
  import type { Browser } from 'playwright-core';
3
4
  import type { ScreenshotOptions } from '../types';
4
5
  export declare function screenshot(browser: Browser, options: Partial<ScreenshotOptions> & Record<string, any>): Promise<Buffer>;
@@ -6,9 +6,9 @@ export async function screenshot(browser, options) {
6
6
  width: options.width || 1200,
7
7
  height: options.height || 630
8
8
  });
9
- const isHtml = options.path.startsWith("html:") || options.html;
9
+ const isHtml = options.html || options.path?.startsWith("html:");
10
10
  if (isHtml) {
11
- const html = options.html || options.path.substring(5);
11
+ const html = options.html || options.path?.substring(5);
12
12
  await page.evaluate((html2) => {
13
13
  document.open("text/html");
14
14
  document.write(html2);
@@ -17,10 +17,13 @@ export async function screenshot(browser, options) {
17
17
  await page.waitForLoadState("networkidle");
18
18
  } else {
19
19
  await page.goto(`${options.host}${options.path}`, {
20
- timeout: 1e4,
20
+ timeout: process.env.prerender || process.dev ? 1e4 : 3500,
21
21
  waitUntil: "networkidle"
22
22
  });
23
23
  }
24
+ const screenshotOptions = {
25
+ timeout: process.env.prerender || process.dev ? 1e4 : 3500
26
+ };
24
27
  if (options.delay)
25
28
  await page.waitForTimeout(options.delay);
26
29
  if (options.mask) {
@@ -30,6 +33,8 @@ export async function screenshot(browser, options) {
30
33
  }, options.mask);
31
34
  }
32
35
  if (options.selector)
33
- return await page.locator(options.selector).screenshot();
34
- return await page.screenshot();
36
+ return await page.locator(options.selector).screenshot(screenshotOptions);
37
+ const screenshot2 = await page.screenshot(screenshotOptions);
38
+ await page.close();
39
+ return screenshot2;
35
40
  }