nuxt-og-image 0.0.1 → 0.0.2
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/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
|
@@ -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))
|