nuxt-og-image 0.2.0 → 0.3.4
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.json +1 -1
- package/dist/module.mjs +15 -20
- package/dist/runtime/composables/defineOgImage.d.ts +1 -0
- package/dist/runtime/composables/defineOgImage.mjs +6 -2
- package/dist/runtime/nitro/html.mjs +4 -4
- package/dist/runtime/nitro/image.d.ts +2 -2
- package/dist/runtime/nitro/image.mjs +11 -22
- package/package.json +4 -6
package/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -8,26 +8,15 @@ import { toRouteMatcher, createRouter } from 'radix3';
|
|
|
8
8
|
import { withBase } from 'ufo';
|
|
9
9
|
import fg from 'fast-glob';
|
|
10
10
|
|
|
11
|
-
async function createLambdaBrowser() {
|
|
12
|
-
try {
|
|
13
|
-
const playwright = await import('playwright-core');
|
|
14
|
-
const awsChrome = await import('chrome-aws-lambda');
|
|
15
|
-
return await playwright.chromium.launch({
|
|
16
|
-
args: awsChrome.args,
|
|
17
|
-
executablePath: await awsChrome.executablePath,
|
|
18
|
-
headless: awsChrome.headless
|
|
19
|
-
});
|
|
20
|
-
} catch (e) {
|
|
21
|
-
}
|
|
22
|
-
return false;
|
|
23
|
-
}
|
|
24
11
|
async function createBrowser() {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
const
|
|
12
|
+
if (!process.env.AWS_LAMBDA_FUNCTION_NAME)
|
|
13
|
+
process.env.AWS_LAMBDA_FUNCTION_NAME = "test";
|
|
14
|
+
const playwright = await import('playwright-core');
|
|
15
|
+
const awsChrome = await import('chrome-aws-lambda');
|
|
29
16
|
return await playwright.chromium.launch({
|
|
30
|
-
|
|
17
|
+
args: awsChrome.args,
|
|
18
|
+
executablePath: await awsChrome.executablePath,
|
|
19
|
+
headless: awsChrome.headless
|
|
31
20
|
});
|
|
32
21
|
}
|
|
33
22
|
async function screenshot(browser, url, options) {
|
|
@@ -108,6 +97,10 @@ declare module 'nitropack' {
|
|
|
108
97
|
name: "defineOgImage",
|
|
109
98
|
from: resolve("./runtime/composables/defineOgImage")
|
|
110
99
|
});
|
|
100
|
+
addImports({
|
|
101
|
+
name: "defineOgImageScreenshot",
|
|
102
|
+
from: resolve("./runtime/composables/defineOgImage")
|
|
103
|
+
});
|
|
111
104
|
await addComponent({
|
|
112
105
|
name: "OgImage",
|
|
113
106
|
filePath: resolve("./runtime/components/OgImage.vue"),
|
|
@@ -125,6 +118,8 @@ declare module 'nitropack' {
|
|
|
125
118
|
let html = ctx.contents;
|
|
126
119
|
if (!html)
|
|
127
120
|
return;
|
|
121
|
+
if (!html.includes('id="nuxt-og-image-payload"'))
|
|
122
|
+
return;
|
|
128
123
|
const routeRules = defu({}, ..._routeRulesMatcher.matchAll(ctx.route).reverse());
|
|
129
124
|
if (routeRules.ogImage === false)
|
|
130
125
|
return;
|
|
@@ -165,7 +160,7 @@ declare module 'nitropack' {
|
|
|
165
160
|
});
|
|
166
161
|
})).trim();
|
|
167
162
|
const browser = await createBrowser();
|
|
168
|
-
nitro.logger.info(`Generating ${entries.length} og:
|
|
163
|
+
nitro.logger.info(`Generating ${entries.length} og:images...`);
|
|
169
164
|
try {
|
|
170
165
|
for (const k in entries) {
|
|
171
166
|
const entry = entries[k];
|
|
@@ -174,7 +169,7 @@ declare module 'nitropack' {
|
|
|
174
169
|
await writeFile(entry.outputPath, imgBuffer);
|
|
175
170
|
const generateTimeMS = Date.now() - start;
|
|
176
171
|
nitro.logger.log(chalk.gray(
|
|
177
|
-
` ${Number(k) === entries.length - 1 ? "\u2514\u2500" : "\u251C\u2500"} /${config.outputDir}/${entry.fileName} (${generateTimeMS}ms)
|
|
172
|
+
` ${Number(k) === entries.length - 1 ? "\u2514\u2500" : "\u251C\u2500"} /${config.outputDir}/${entry.fileName} (${generateTimeMS}ms) ${Math.round(Number(k) / (entries.length - 1) * 100)}%`
|
|
178
173
|
));
|
|
179
174
|
}
|
|
180
175
|
} catch (e) {
|
|
@@ -5,21 +5,25 @@ export const RuntimeImageSuffix = "og-image.png";
|
|
|
5
5
|
export const PayloadScriptId = "nuxt-og-image-payload";
|
|
6
6
|
export const MetaOgImageContentPlaceholder = "__NUXT_OG_IMAGE_PLACEHOLDER__";
|
|
7
7
|
export const LinkPrerenderId = "nuxt-og-image-screenshot-path";
|
|
8
|
+
export function defineOgImageScreenshot() {
|
|
9
|
+
defineOgImage();
|
|
10
|
+
}
|
|
8
11
|
export function defineOgImage(options = {}) {
|
|
9
12
|
if (process.server) {
|
|
10
13
|
const router = useRouter();
|
|
14
|
+
const route = router?.currentRoute?.value?.path || "";
|
|
11
15
|
useServerHead({
|
|
12
16
|
meta: [
|
|
13
17
|
{
|
|
14
18
|
property: "og:image",
|
|
15
|
-
content: () => options.runtime ? `${
|
|
19
|
+
content: () => options.runtime ? `${route}/${RuntimeImageSuffix}` : MetaOgImageContentPlaceholder
|
|
16
20
|
}
|
|
17
21
|
],
|
|
18
22
|
link: options.component ? [
|
|
19
23
|
{
|
|
20
24
|
id: LinkPrerenderId,
|
|
21
25
|
rel: "prerender",
|
|
22
|
-
href: `${
|
|
26
|
+
href: `${route}/${HtmlRendererRoute}`
|
|
23
27
|
}
|
|
24
28
|
] : [],
|
|
25
29
|
script: [
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { withQuery } from "ufo";
|
|
1
|
+
import { withoutTrailingSlash, withQuery } from "ufo";
|
|
2
2
|
import { renderSSRHead } from "@unhead/ssr";
|
|
3
3
|
import { createHeadCore } from "@unhead/vue";
|
|
4
4
|
import { defineEventHandler, getQuery } from "h3";
|
|
@@ -24,8 +24,8 @@ export const inferOgPayload = (html) => {
|
|
|
24
24
|
export default defineEventHandler(async (req) => {
|
|
25
25
|
if (!req.path?.endsWith(HtmlRendererRoute))
|
|
26
26
|
return;
|
|
27
|
-
const path = req.path.replace(
|
|
28
|
-
const html = await $fetch(path);
|
|
27
|
+
const path = req.path.replace(HtmlRendererRoute, "");
|
|
28
|
+
const html = await $fetch(withoutTrailingSlash(path));
|
|
29
29
|
const payload = {
|
|
30
30
|
path,
|
|
31
31
|
title: "Hello World",
|
|
@@ -35,7 +35,7 @@ export default defineEventHandler(async (req) => {
|
|
|
35
35
|
...inferOgPayload(html),
|
|
36
36
|
...getQuery(req)
|
|
37
37
|
};
|
|
38
|
-
const result = await $fetch(withQuery(`/__nuxt_island/${payload.
|
|
38
|
+
const result = await $fetch(withQuery(`/__nuxt_island/${payload.component || "OgImage"}`, {
|
|
39
39
|
props: JSON.stringify(payload)
|
|
40
40
|
}));
|
|
41
41
|
const head = createHeadCore();
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
-
import type { Browser } from 'playwright';
|
|
2
|
+
import type { Browser } from 'playwright-core';
|
|
3
3
|
import type { ScreenshotOptions } from '../../types';
|
|
4
4
|
export declare const HtmlRendererRoute = "__og_image";
|
|
5
5
|
export declare const RuntimeImageSuffix = "og-image.png";
|
|
6
|
-
export declare function createBrowser(): Promise<Browser>;
|
|
7
6
|
export declare function screenshot(browser: Browser, url: string, options: ScreenshotOptions): Promise<Buffer>;
|
|
7
|
+
export declare function createBrowser(): Promise<Browser>;
|
|
8
8
|
declare const _default: import("h3").EventHandler<Buffer | undefined>;
|
|
9
9
|
export default _default;
|
|
@@ -1,28 +1,6 @@
|
|
|
1
1
|
import { defineEventHandler, getRequestHeader, setHeader } from "h3";
|
|
2
2
|
export const HtmlRendererRoute = "__og_image";
|
|
3
3
|
export const RuntimeImageSuffix = "og-image.png";
|
|
4
|
-
async function createLambdaBrowser() {
|
|
5
|
-
try {
|
|
6
|
-
const playwright = await import("playwright-core");
|
|
7
|
-
const awsChrome = await import("chrome-aws-lambda");
|
|
8
|
-
return await playwright.chromium.launch({
|
|
9
|
-
args: awsChrome.args,
|
|
10
|
-
executablePath: await awsChrome.executablePath,
|
|
11
|
-
headless: awsChrome.headless
|
|
12
|
-
});
|
|
13
|
-
} catch (e) {
|
|
14
|
-
}
|
|
15
|
-
return false;
|
|
16
|
-
}
|
|
17
|
-
export async function createBrowser() {
|
|
18
|
-
const lambdaBrowser = await createLambdaBrowser();
|
|
19
|
-
if (lambdaBrowser)
|
|
20
|
-
return lambdaBrowser;
|
|
21
|
-
const playwright = await import("playwright");
|
|
22
|
-
return await playwright.chromium.launch({
|
|
23
|
-
chromiumSandbox: true
|
|
24
|
-
});
|
|
25
|
-
}
|
|
26
4
|
export async function screenshot(browser, url, options) {
|
|
27
5
|
const page = await browser.newPage({
|
|
28
6
|
colorScheme: options.colorScheme
|
|
@@ -45,6 +23,17 @@ export async function screenshot(browser, url, options) {
|
|
|
45
23
|
await page.locator(options.selector).screenshot();
|
|
46
24
|
return await page.screenshot();
|
|
47
25
|
}
|
|
26
|
+
export async function createBrowser() {
|
|
27
|
+
if (!process.env.AWS_LAMBDA_FUNCTION_NAME)
|
|
28
|
+
process.env.AWS_LAMBDA_FUNCTION_NAME = "test";
|
|
29
|
+
const playwright = await import("playwright-core");
|
|
30
|
+
const awsChrome = await import("chrome-aws-lambda");
|
|
31
|
+
return await playwright.chromium.launch({
|
|
32
|
+
args: awsChrome.args,
|
|
33
|
+
executablePath: await awsChrome.executablePath,
|
|
34
|
+
headless: awsChrome.headless
|
|
35
|
+
});
|
|
36
|
+
}
|
|
48
37
|
export default defineEventHandler(async (e) => {
|
|
49
38
|
if (!e.path?.endsWith(RuntimeImageSuffix))
|
|
50
39
|
return;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nuxt-og-image",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.3.4",
|
|
5
5
|
"packageManager": "pnpm@7.8.0",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"funding": "https://github.com/sponsors/harlan-zw",
|
|
@@ -28,17 +28,17 @@
|
|
|
28
28
|
"dependencies": {
|
|
29
29
|
"@nuxt/kit": "3.0.0",
|
|
30
30
|
"chalk": "^5.2.0",
|
|
31
|
+
"chrome-aws-lambda": "^10.1.0",
|
|
31
32
|
"defu": "^6.1.1",
|
|
32
33
|
"execa": "^6.1.0",
|
|
33
34
|
"fast-glob": "^3.2.12",
|
|
34
35
|
"ohash": "^1.0.0",
|
|
35
|
-
"playwright": "^1.28.1",
|
|
36
|
+
"playwright-core": "^1.28.1",
|
|
36
37
|
"radix3": "^1.0.0",
|
|
37
38
|
"ufo": "^1.0.1"
|
|
38
39
|
},
|
|
39
40
|
"unbuild": {
|
|
40
41
|
"externals": [
|
|
41
|
-
"playwright",
|
|
42
42
|
"playwright-core",
|
|
43
43
|
"chrome-aws-lambda"
|
|
44
44
|
]
|
|
@@ -50,12 +50,10 @@
|
|
|
50
50
|
"@nuxt/test-utils": "3.0.0",
|
|
51
51
|
"@nuxtjs/eslint-config-typescript": "^12.0.0",
|
|
52
52
|
"bumpp": "^8.2.1",
|
|
53
|
-
"chrome-aws-lambda": "^10.1.0",
|
|
54
53
|
"eslint": "8.29.0",
|
|
55
54
|
"nuxt": "npm:nuxt3@latest",
|
|
56
55
|
"pathe": "^1.0.0",
|
|
57
|
-
"
|
|
58
|
-
"playwright-core": "^1.28.1",
|
|
56
|
+
"puppeteer": "^19.4.0",
|
|
59
57
|
"vitest": "^0.25.5"
|
|
60
58
|
},
|
|
61
59
|
"scripts": {
|