nuxt-og-image 0.0.1 → 0.0.3
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 +16 -20
- package/dist/runtime/browserService.d.ts +5 -0
- package/dist/runtime/browserService.mjs +44 -0
- package/dist/runtime/composables/defineOgImage.mjs +1 -1
- package/dist/runtime/const.d.ts +5 -0
- package/dist/runtime/const.mjs +5 -0
- package/dist/runtime/nitro/html.mjs +1 -1
- package/dist/runtime/nitro/image.d.ts +1 -2
- package/dist/runtime/nitro/image.mjs +1 -1
- package/package.json +1 -1
package/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -107,7 +107,7 @@ declare module 'nitropack' {
|
|
|
107
107
|
name: "defineOgImage",
|
|
108
108
|
from: resolve("./runtime/composables/defineOgImage")
|
|
109
109
|
});
|
|
110
|
-
addComponent({
|
|
110
|
+
await addComponent({
|
|
111
111
|
name: "OgImage",
|
|
112
112
|
filePath: resolve("./runtime/components/OgImage.vue"),
|
|
113
113
|
island: true
|
|
@@ -134,7 +134,9 @@ declare module 'nitropack' {
|
|
|
134
134
|
fileName,
|
|
135
135
|
absoluteUrl,
|
|
136
136
|
outputPath: `${nitro.options.output.dir}/public/${config.outputDir}/${fileName}`,
|
|
137
|
+
linkingHtml: ctx.fileName,
|
|
137
138
|
route: ctx.route,
|
|
139
|
+
hasPayload: screenshotPath,
|
|
138
140
|
routeRules: routeRules.ogImage || "",
|
|
139
141
|
screenshotPath: screenshotPath || ctx.route
|
|
140
142
|
};
|
|
@@ -164,21 +166,16 @@ declare module 'nitropack' {
|
|
|
164
166
|
const browser = await createBrowser();
|
|
165
167
|
nitro.logger.info(`Generating ${entries.length} og:image screenshots`);
|
|
166
168
|
try {
|
|
167
|
-
const
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
});
|
|
178
|
-
});
|
|
179
|
-
});
|
|
180
|
-
});
|
|
181
|
-
await Promise.all(imageGenPromises);
|
|
169
|
+
for (const k in entries) {
|
|
170
|
+
const entry = entries[k];
|
|
171
|
+
const start = Date.now();
|
|
172
|
+
const imgBuffer = await screenshot(browser, `${host}${entry.screenshotPath}`, config);
|
|
173
|
+
await writeFile(entry.outputPath, imgBuffer);
|
|
174
|
+
const generateTimeMS = Date.now() - start;
|
|
175
|
+
nitro.logger.log(chalk.gray(
|
|
176
|
+
` ${Number(k) === entries.length - 1 ? "\u2514\u2500" : "\u251C\u2500"} /${config.outputDir}/${entry.fileName} (${generateTimeMS}ms)`
|
|
177
|
+
));
|
|
178
|
+
}
|
|
182
179
|
} catch (e) {
|
|
183
180
|
console.error(e);
|
|
184
181
|
} finally {
|
|
@@ -189,12 +186,11 @@ declare module 'nitropack' {
|
|
|
189
186
|
} finally {
|
|
190
187
|
previewProcess.kill();
|
|
191
188
|
}
|
|
192
|
-
const
|
|
193
|
-
|
|
194
|
-
const html = await readFile(`${nitro.options.output.dir}/${htmlFile}`, "utf-8");
|
|
189
|
+
for (const entry of entries.filter((e) => e.hasPayload)) {
|
|
190
|
+
const html = await readFile(`${nitro.options.output.dir}/public${entry.linkingHtml}`, "utf-8");
|
|
195
191
|
const newHtml = html.replace(new RegExp(`<link id="${LinkPrerenderId}" rel="prerender" href="(.*?)">`), "").replace(new RegExp(`<script id="${PayloadScriptId}" type="application/json">(.*?)<\/script>`), "").replace("\n\n", "\n");
|
|
196
192
|
if (html !== newHtml) {
|
|
197
|
-
await writeFile(`${nitro.options.output.dir}
|
|
193
|
+
await writeFile(`${nitro.options.output.dir}/public${entry.linkingHtml}`, newHtml, { encoding: "utf-8" });
|
|
198
194
|
}
|
|
199
195
|
}
|
|
200
196
|
const ogImageFolders = await fg([`**/${HtmlRendererRoute}`], { cwd: nitro.options.output.dir, onlyDirectories: true });
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import type { Browser } from 'playwright';
|
|
3
|
+
import type { ScreenshotOptions } from './types';
|
|
4
|
+
export declare function createBrowser(): Promise<Browser>;
|
|
5
|
+
export declare function screenshot(browser: Browser, url: string, options: ScreenshotOptions): Promise<Buffer>;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
async function createLambdaBrowser() {
|
|
2
|
+
try {
|
|
3
|
+
const playwright = await import("playwright-core");
|
|
4
|
+
const awsChrome = await import("chrome-aws-lambda");
|
|
5
|
+
return await playwright.chromium.launch({
|
|
6
|
+
args: awsChrome.args,
|
|
7
|
+
executablePath: await awsChrome.executablePath,
|
|
8
|
+
headless: awsChrome.headless
|
|
9
|
+
});
|
|
10
|
+
} catch (e) {
|
|
11
|
+
}
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
export async function createBrowser() {
|
|
15
|
+
const lambdaBrowser = await createLambdaBrowser();
|
|
16
|
+
if (lambdaBrowser)
|
|
17
|
+
return lambdaBrowser;
|
|
18
|
+
const playwright = await import("playwright");
|
|
19
|
+
return await playwright.chromium.launch({
|
|
20
|
+
chromiumSandbox: true
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
export async function screenshot(browser, url, options) {
|
|
24
|
+
const page = await browser.newPage({
|
|
25
|
+
colorScheme: options.colorScheme
|
|
26
|
+
});
|
|
27
|
+
await page.setViewportSize({
|
|
28
|
+
width: options.width,
|
|
29
|
+
height: options.height
|
|
30
|
+
});
|
|
31
|
+
await page.goto(url, {
|
|
32
|
+
timeout: 1e4,
|
|
33
|
+
waitUntil: "networkidle"
|
|
34
|
+
});
|
|
35
|
+
if (options.mask) {
|
|
36
|
+
await page.evaluate((mask) => {
|
|
37
|
+
for (const el of document.querySelectorAll(mask))
|
|
38
|
+
el.style.display = "none";
|
|
39
|
+
}, options.mask);
|
|
40
|
+
}
|
|
41
|
+
if (options.selector)
|
|
42
|
+
await page.locator(options.selector).screenshot();
|
|
43
|
+
return await page.screenshot();
|
|
44
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export declare const HtmlRendererRoute = "__og_image";
|
|
2
|
+
export declare const RuntimeImageSuffix = "og-image.png";
|
|
3
|
+
export declare const PayloadScriptId = "nuxt-og-image-payload";
|
|
4
|
+
export declare const MetaOgImageContentPlaceholder = "__NUXT_OG_IMAGE_PLACEHOLDER__";
|
|
5
|
+
export declare const LinkPrerenderId = "nuxt-og-image-screenshot-path";
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export const HtmlRendererRoute = "__og_image";
|
|
2
|
+
export const RuntimeImageSuffix = "og-image.png";
|
|
3
|
+
export const PayloadScriptId = "nuxt-og-image-payload";
|
|
4
|
+
export const MetaOgImageContentPlaceholder = "__NUXT_OG_IMAGE_PLACEHOLDER__";
|
|
5
|
+
export const LinkPrerenderId = "nuxt-og-image-screenshot-path";
|
|
@@ -2,7 +2,7 @@ import { withQuery } from "ufo";
|
|
|
2
2
|
import { renderSSRHead } from "@unhead/ssr";
|
|
3
3
|
import { createHeadCore } from "@unhead/vue";
|
|
4
4
|
import { defineEventHandler, getQuery } from "h3";
|
|
5
|
-
import { HtmlRendererRoute, PayloadScriptId } from "
|
|
5
|
+
import { HtmlRendererRoute, PayloadScriptId } from "../const.mjs";
|
|
6
6
|
export const extractOgPayload = (html) => {
|
|
7
7
|
const payload = html.match(new RegExp(`<script id="${PayloadScriptId}" type="application/json">(.+?)<\/script>`))?.[1];
|
|
8
8
|
if (payload) {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { defineEventHandler, getRequestHeader, setHeader } from "h3";
|
|
2
|
-
import { HtmlRendererRoute, RuntimeImageSuffix } from "
|
|
2
|
+
import { HtmlRendererRoute, RuntimeImageSuffix } from "../const.mjs";
|
|
3
3
|
import { createBrowser, screenshot } from "../../browserService";
|
|
4
4
|
export default defineEventHandler(async (e) => {
|
|
5
5
|
if (!e.path?.endsWith(RuntimeImageSuffix))
|