nuxt-og-image 1.4.6 → 1.4.8

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/dist/module.d.ts CHANGED
@@ -27,7 +27,7 @@ interface OgImageOptions extends Partial<ScreenshotOptions> {
27
27
  static?: boolean;
28
28
  title?: string;
29
29
  description?: string;
30
- component?: string;
30
+ component?: string | null;
31
31
  alt?: string;
32
32
  [key: string]: any;
33
33
  }
package/dist/module.json CHANGED
@@ -5,5 +5,5 @@
5
5
  "bridge": false
6
6
  },
7
7
  "configKey": "ogImage",
8
- "version": "1.4.6"
8
+ "version": "1.4.8"
9
9
  }
package/dist/module.mjs CHANGED
@@ -10,13 +10,13 @@ import { tinyws } from 'tinyws';
10
10
  import sirv from 'sirv';
11
11
  import { pathExists, copy, mkdirp } from 'fs-extra';
12
12
  import { isCI, provider } from 'std-env';
13
+ import playwrightCore from 'playwright-core';
13
14
  import { existsSync } from 'fs';
14
15
  import { createBirpcGroup } from 'birpc';
15
16
  import { stringify, parse } from 'flatted';
16
17
 
17
18
  async function createBrowser() {
18
19
  try {
19
- const playwrightCore = await import(String("playwright-core"));
20
20
  const { Launcher } = await import(String("chrome-launcher"));
21
21
  const chromePath = Launcher.getFirstInstallation();
22
22
  return await playwrightCore.chromium.launch({
@@ -25,6 +25,12 @@ async function createBrowser() {
25
25
  });
26
26
  } catch (e) {
27
27
  }
28
+ try {
29
+ return await playwrightCore.chromium.launch({
30
+ headless: true
31
+ });
32
+ } catch (e) {
33
+ }
28
34
  try {
29
35
  const playwright = await import(String("playwright"));
30
36
  return await playwright.chromium.launch({
@@ -40,18 +46,27 @@ async function createBrowser() {
40
46
  }
41
47
  }
42
48
 
43
- async function screenshot(browser, url, options) {
49
+ async function screenshot(browser, options) {
44
50
  const page = await browser.newPage({
45
51
  colorScheme: options.colorScheme
46
52
  });
47
53
  await page.setViewportSize({
48
- width: options.width,
49
- height: options.height
50
- });
51
- await page.goto(url, {
52
- timeout: 1e4,
53
- waitUntil: "networkidle"
54
+ width: options.width || 1200,
55
+ height: options.height || 630
54
56
  });
57
+ if (options.path.startsWith("html:")) {
58
+ await page.evaluate((html) => {
59
+ document.open("text/html");
60
+ document.write(html);
61
+ document.close();
62
+ }, options.path.substring(5));
63
+ await page.waitForLoadState("networkidle");
64
+ } else {
65
+ await page.goto(`${options.host}${options.path}`, {
66
+ timeout: 1e4,
67
+ waitUntil: "networkidle"
68
+ });
69
+ }
55
70
  if (options.delay)
56
71
  await page.waitForTimeout(options.delay);
57
72
  if (options.mask) {
@@ -401,14 +416,16 @@ export default function() {
401
416
  const routeRules = defu({}, ..._routeRulesMatcher.matchAll(ctx.route).reverse());
402
417
  if (!extractedOptions || routeRules.ogImage === false)
403
418
  return;
404
- const options = {
405
- path: ctx.route,
419
+ const entry = {
420
+ path: extractedOptions.component ? `/api/og-image-html?path=${ctx.route}` : ctx.route,
406
421
  ...extractedOptions,
407
422
  ...routeRules.ogImage || {},
408
423
  ctx
409
424
  };
410
- if ((nuxt.options._generate || options.static) && options.provider === "browser")
411
- screenshotQueue.push(options);
425
+ if (entry.component)
426
+ entry.path = `html:${await $fetch(entry.path)}`;
427
+ if ((nuxt.options._generate || entry.static) && entry.provider === "browser")
428
+ screenshotQueue.push(entry);
412
429
  });
413
430
  if (nuxt.options.dev)
414
431
  return;
@@ -440,7 +457,7 @@ export default function() {
440
457
  })).trim();
441
458
  browser = await createBrowser();
442
459
  if (browser) {
443
- nitro.logger.info(`Pre-rendering ${screenshotQueue.length} og:image screenshots...`);
460
+ nitro.logger.info(`Prerendering ${screenshotQueue.length} og:image screenshots...`);
444
461
  for (const k in screenshotQueue) {
445
462
  const entry = screenshotQueue[k];
446
463
  const start = Date.now();
@@ -448,9 +465,10 @@ export default function() {
448
465
  const dirname = joinURL(nitro.options.output.publicDir, `${entry.ctx.fileName.replace("index.html", "")}__og_image__/`);
449
466
  const filename = joinURL(dirname, "/og.png");
450
467
  try {
451
- const imgBuffer = await screenshot(browser, `${host}${entry.path}`, {
468
+ const imgBuffer = await screenshot(browser, {
452
469
  ...config.defaults || {},
453
- ...entry || {}
470
+ ...entry || {},
471
+ host
454
472
  });
455
473
  try {
456
474
  await mkdirp(dirname);
@@ -1,4 +1,4 @@
1
1
  /// <reference types="node" />
2
2
  import type { Browser } from 'playwright-core';
3
3
  import type { ScreenshotOptions } from '../types';
4
- export declare function screenshot(browser: Browser, url: string, options: ScreenshotOptions): Promise<Buffer>;
4
+ export declare function screenshot(browser: Browser, options: Partial<ScreenshotOptions> & Record<string, any>): Promise<Buffer>;
@@ -1,15 +1,24 @@
1
- export async function screenshot(browser, url, options) {
1
+ export async function screenshot(browser, options) {
2
2
  const page = await browser.newPage({
3
3
  colorScheme: options.colorScheme
4
4
  });
5
5
  await page.setViewportSize({
6
- width: options.width,
7
- height: options.height
8
- });
9
- await page.goto(url, {
10
- timeout: 1e4,
11
- waitUntil: "networkidle"
6
+ width: options.width || 1200,
7
+ height: options.height || 630
12
8
  });
9
+ if (options.path.startsWith("html:")) {
10
+ await page.evaluate((html) => {
11
+ document.open("text/html");
12
+ document.write(html);
13
+ document.close();
14
+ }, options.path.substring(5));
15
+ await page.waitForLoadState("networkidle");
16
+ } else {
17
+ await page.goto(`${options.host}${options.path}`, {
18
+ timeout: 1e4,
19
+ waitUntil: "networkidle"
20
+ });
21
+ }
13
22
  if (options.delay)
14
23
  await page.waitForTimeout(options.delay);
15
24
  if (options.mask) {
@@ -9,6 +9,7 @@ export function defineOgImageScreenshot(options = {}) {
9
9
  defineOgImage({
10
10
  alt: `Web page screenshot${route ? ` of ${route}` : ""}.`,
11
11
  provider: "browser",
12
+ component: null,
12
13
  static: true,
13
14
  ...options
14
15
  });
@@ -1,7 +1,7 @@
1
1
  import { isCI } from "std-env";
2
+ import playwrightCore from "playwright-core";
2
3
  export default async function createBrowser() {
3
4
  try {
4
- const playwrightCore = await import(String("playwright-core"));
5
5
  const { Launcher } = await import(String("chrome-launcher"));
6
6
  const chromePath = Launcher.getFirstInstallation();
7
7
  return await playwrightCore.chromium.launch({
@@ -10,6 +10,12 @@ export default async function createBrowser() {
10
10
  });
11
11
  } catch (e) {
12
12
  }
13
+ try {
14
+ return await playwrightCore.chromium.launch({
15
+ headless: true
16
+ });
17
+ } catch (e) {
18
+ }
13
19
  try {
14
20
  const playwright = await import(String("playwright"));
15
21
  return await playwright.chromium.launch({
@@ -9,10 +9,16 @@ export default {
9
9
  throw new Error("Browser provider can't create VNodes.");
10
10
  },
11
11
  createPng: async function createPng(basePath, options) {
12
+ const url = new URL(basePath);
12
13
  const createBrowser = await loadBrowser();
13
14
  const browser = await createBrowser();
14
- if (browser)
15
- return screenshot(browser, basePath, options);
15
+ if (browser) {
16
+ return screenshot(browser, {
17
+ ...options,
18
+ host: url.origin,
19
+ path: `/api/og-image-html?path=${url.pathname}`
20
+ });
21
+ }
16
22
  return null;
17
23
  }
18
24
  };
@@ -13,7 +13,7 @@ export default defineEventHandler(async (e) => {
13
13
  options = JSON.parse(getQuery(e).options);
14
14
  if (!options)
15
15
  options = await fetchOptions(e, path);
16
- if (options.provider === "browser")
16
+ if (options.provider === "browser" && !options.component)
17
17
  return sendRedirect(e, withBase(path, useHostname(e)));
18
18
  const island = await renderIsland(options);
19
19
  const head = createHeadCore();
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "nuxt-og-image",
3
3
  "type": "module",
4
- "version": "1.4.6",
4
+ "version": "1.4.8",
5
5
  "packageManager": "pnpm@7.8.0",
6
6
  "license": "MIT",
7
7
  "funding": "https://github.com/sponsors/harlan-zw",
@@ -41,7 +41,7 @@
41
41
  "pathe": "^1.1.0",
42
42
  "playwright-core": "^1.30.0",
43
43
  "radix3": "^1.0.0",
44
- "satori": "^0.2.0",
44
+ "satori": "0.2.0",
45
45
  "satori-html": "^0.3.2",
46
46
  "sirv": "^2.0.2",
47
47
  "std-env": "^3.3.2",
@@ -53,7 +53,7 @@
53
53
  },
54
54
  "devDependencies": {
55
55
  "@antfu/eslint-config": "^0.35.1",
56
- "@nuxt/devtools-edge": "0.0.0-27925355.bb05ed5",
56
+ "@nuxt/devtools-edge": "0.0.0-27928010.c91ffe5",
57
57
  "@nuxt/module-builder": "^0.2.1",
58
58
  "@nuxt/test-utils": "3.1.2",
59
59
  "@nuxtjs/eslint-config-typescript": "^12.0.0",