diff --git a/example/package-lock.json b/example/package-lock.json index cbd2bbf..e4899ea 100644 --- a/example/package-lock.json +++ b/example/package-lock.json @@ -5,6 +5,7 @@ "requires": true, "packages": { "": { + "name": "example", "version": "0.1.0", "dependencies": { "next": "11.1.2", @@ -20,12 +21,10 @@ } }, "..": { - "version": "2.1.0", + "version": "4.2.1", "license": "MIT", "dependencies": { - "chrome-aws-lambda": "^10.1.0", "deepmerge": "^4.2.2", - "patch-package": "^6.4.7", "puppeteer-core": "^10.4.0", "twemoji": "^13.1.0" }, @@ -42,6 +41,7 @@ "typescript": "^4.4.3" }, "peerDependencies": { + "chrome-aws-lambda": ">=1.6.3", "next": ">=9.0.0", "react": ">=16.0.0", "react-dom": ">=16.0.0" @@ -5569,10 +5569,8 @@ "@types/react": "^17.0.29", "@types/react-dom": "^17.0.9", "@types/twemoji": "^12.1.2", - "chrome-aws-lambda": "^10.1.0", "deepmerge": "^4.2.2", "microbundle": "^0.13.3", - "patch-package": "^6.4.7", "prettier": "^2.4.1", "puppeteer-core": "^10.4.0", "rimraf": "^3.0.2", diff --git a/package-lock.json b/package-lock.json index 8471ee6..92a535f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,16 +1,16 @@ { "name": "next-api-og-image", - "version": "4.2.1", + "version": "4.2.2", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "next-api-og-image", - "version": "4.2.1", + "version": "4.2.2", "license": "MIT", "dependencies": { - "chrome-aws-lambda": "^10.1.0", "deepmerge": "^4.2.2", + "is-lambda": "^1.0.1", "puppeteer-core": "^10.4.0", "twemoji": "^13.1.0" }, @@ -27,6 +27,7 @@ "typescript": "^4.4.3" }, "peerDependencies": { + "chrome-aws-lambda": ">=1.6.3", "next": ">=9.0.0", "react": ">=16.0.0", "react-dom": ">=16.0.0" @@ -2815,6 +2816,7 @@ "version": "10.1.0", "resolved": "https://registry.npmjs.org/chrome-aws-lambda/-/chrome-aws-lambda-10.1.0.tgz", "integrity": "sha512-NZQVf+J4kqG4sVhRm3WNmOfzY0OtTSm+S8rg77pwePa9RCYHzhnzRs8YvNI6L9tALIW6RpmefWiPURt3vURXcw==", + "peer": true, "dependencies": { "lambdafs": "^2.0.3" }, @@ -4388,6 +4390,11 @@ "node": ">=0.10.0" } }, + "node_modules/is-lambda": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/is-lambda/-/is-lambda-1.0.1.tgz", + "integrity": "sha512-z7CMFGNrENq5iFB9Bqo64Xk6Y9sg+epq1myIcdHaGnbMTYOxvzsEtdYqQUylB7LxfkvgrrjP32T6Ywciio9UIQ==" + }, "node_modules/is-module": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/is-module/-/is-module-1.0.0.tgz", @@ -4651,6 +4658,7 @@ "bundleDependencies": [ "tar-fs" ], + "peer": true, "dependencies": { "tar-fs": "^2.1.1" }, @@ -4678,12 +4686,14 @@ } ], "inBundle": true, - "license": "MIT" + "license": "MIT", + "peer": true }, "node_modules/lambdafs/node_modules/bl": { "version": "4.1.0", "inBundle": true, "license": "MIT", + "peer": true, "dependencies": { "buffer": "^5.5.0", "inherits": "^2.0.4", @@ -4694,6 +4704,7 @@ "version": "3.6.0", "inBundle": true, "license": "MIT", + "peer": true, "dependencies": { "inherits": "^2.0.3", "string_decoder": "^1.1.1", @@ -4721,6 +4732,7 @@ ], "inBundle": true, "license": "MIT", + "peer": true, "dependencies": { "base64-js": "^1.3.1", "ieee754": "^1.1.13" @@ -4729,12 +4741,14 @@ "node_modules/lambdafs/node_modules/chownr": { "version": "1.1.4", "inBundle": true, - "license": "ISC" + "license": "ISC", + "peer": true }, "node_modules/lambdafs/node_modules/end-of-stream": { "version": "1.4.4", "inBundle": true, "license": "MIT", + "peer": true, "dependencies": { "once": "^1.4.0" } @@ -4742,7 +4756,8 @@ "node_modules/lambdafs/node_modules/fs-constants": { "version": "1.0.0", "inBundle": true, - "license": "MIT" + "license": "MIT", + "peer": true }, "node_modules/lambdafs/node_modules/ieee754": { "version": "1.2.1", @@ -4761,22 +4776,26 @@ } ], "inBundle": true, - "license": "BSD-3-Clause" + "license": "BSD-3-Clause", + "peer": true }, "node_modules/lambdafs/node_modules/inherits": { "version": "2.0.4", "inBundle": true, - "license": "ISC" + "license": "ISC", + "peer": true }, "node_modules/lambdafs/node_modules/mkdirp-classic": { "version": "0.5.3", "inBundle": true, - "license": "MIT" + "license": "MIT", + "peer": true }, "node_modules/lambdafs/node_modules/once": { "version": "1.4.0", "inBundle": true, "license": "ISC", + "peer": true, "dependencies": { "wrappy": "1" } @@ -4785,6 +4804,7 @@ "version": "3.0.0", "inBundle": true, "license": "MIT", + "peer": true, "dependencies": { "end-of-stream": "^1.1.0", "once": "^1.3.1" @@ -4794,6 +4814,7 @@ "version": "1.1.1", "inBundle": true, "license": "MIT", + "peer": true, "dependencies": { "safe-buffer": "~5.1.0" } @@ -4801,12 +4822,14 @@ "node_modules/lambdafs/node_modules/string_decoder/node_modules/safe-buffer": { "version": "5.1.2", "inBundle": true, - "license": "MIT" + "license": "MIT", + "peer": true }, "node_modules/lambdafs/node_modules/tar-fs": { "version": "2.1.1", "inBundle": true, "license": "MIT", + "peer": true, "dependencies": { "chownr": "^1.1.1", "mkdirp-classic": "^0.5.2", @@ -4818,6 +4841,7 @@ "version": "2.2.0", "inBundle": true, "license": "MIT", + "peer": true, "dependencies": { "bl": "^4.0.3", "end-of-stream": "^1.4.1", @@ -4833,6 +4857,7 @@ "version": "3.6.0", "inBundle": true, "license": "MIT", + "peer": true, "dependencies": { "inherits": "^2.0.3", "string_decoder": "^1.1.1", @@ -4845,12 +4870,14 @@ "node_modules/lambdafs/node_modules/util-deprecate": { "version": "1.0.2", "inBundle": true, - "license": "MIT" + "license": "MIT", + "peer": true }, "node_modules/lambdafs/node_modules/wrappy": { "version": "1.0.2", "inBundle": true, - "license": "ISC" + "license": "ISC", + "peer": true }, "node_modules/lilconfig": { "version": "2.0.3", @@ -10220,6 +10247,7 @@ "version": "10.1.0", "resolved": "https://registry.npmjs.org/chrome-aws-lambda/-/chrome-aws-lambda-10.1.0.tgz", "integrity": "sha512-NZQVf+J4kqG4sVhRm3WNmOfzY0OtTSm+S8rg77pwePa9RCYHzhnzRs8YvNI6L9tALIW6RpmefWiPURt3vURXcw==", + "peer": true, "requires": { "lambdafs": "^2.0.3" } @@ -11413,6 +11441,11 @@ "is-extglob": "^2.1.1" } }, + "is-lambda": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/is-lambda/-/is-lambda-1.0.1.tgz", + "integrity": "sha512-z7CMFGNrENq5iFB9Bqo64Xk6Y9sg+epq1myIcdHaGnbMTYOxvzsEtdYqQUylB7LxfkvgrrjP32T6Ywciio9UIQ==" + }, "is-module": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/is-module/-/is-module-1.0.0.tgz", @@ -11597,17 +11630,20 @@ "version": "2.0.3", "resolved": "https://registry.npmjs.org/lambdafs/-/lambdafs-2.0.3.tgz", "integrity": "sha512-5YWwZA/QKk09GdfcJ/ABVO+bpFoGlnTBa5jmyM8Kt9yIzl2lDDVBPLK+Aenq2UEcuDpqxXIYI5zLB7VZNepTrg==", + "peer": true, "requires": { "tar-fs": "^2.1.1" }, "dependencies": { "base64-js": { "version": "1.5.1", - "bundled": true + "bundled": true, + "peer": true }, "bl": { "version": "4.1.0", "bundled": true, + "peer": true, "requires": { "buffer": "^5.5.0", "inherits": "^2.0.4", @@ -11617,6 +11653,7 @@ "readable-stream": { "version": "3.6.0", "bundled": true, + "peer": true, "requires": { "inherits": "^2.0.3", "string_decoder": "^1.1.1", @@ -11628,6 +11665,7 @@ "buffer": { "version": "5.7.1", "bundled": true, + "peer": true, "requires": { "base64-js": "^1.3.1", "ieee754": "^1.1.13" @@ -11635,34 +11673,41 @@ }, "chownr": { "version": "1.1.4", - "bundled": true + "bundled": true, + "peer": true }, "end-of-stream": { "version": "1.4.4", "bundled": true, + "peer": true, "requires": { "once": "^1.4.0" } }, "fs-constants": { "version": "1.0.0", - "bundled": true + "bundled": true, + "peer": true }, "ieee754": { "version": "1.2.1", - "bundled": true + "bundled": true, + "peer": true }, "inherits": { "version": "2.0.4", - "bundled": true + "bundled": true, + "peer": true }, "mkdirp-classic": { "version": "0.5.3", - "bundled": true + "bundled": true, + "peer": true }, "once": { "version": "1.4.0", "bundled": true, + "peer": true, "requires": { "wrappy": "1" } @@ -11670,6 +11715,7 @@ "pump": { "version": "3.0.0", "bundled": true, + "peer": true, "requires": { "end-of-stream": "^1.1.0", "once": "^1.3.1" @@ -11678,19 +11724,22 @@ "string_decoder": { "version": "1.1.1", "bundled": true, + "peer": true, "requires": { "safe-buffer": "~5.1.0" }, "dependencies": { "safe-buffer": { "version": "5.1.2", - "bundled": true + "bundled": true, + "peer": true } } }, "tar-fs": { "version": "2.1.1", "bundled": true, + "peer": true, "requires": { "chownr": "^1.1.1", "mkdirp-classic": "^0.5.2", @@ -11701,6 +11750,7 @@ "tar-stream": { "version": "2.2.0", "bundled": true, + "peer": true, "requires": { "bl": "^4.0.3", "end-of-stream": "^1.4.1", @@ -11712,6 +11762,7 @@ "readable-stream": { "version": "3.6.0", "bundled": true, + "peer": true, "requires": { "inherits": "^2.0.3", "string_decoder": "^1.1.1", @@ -11722,11 +11773,13 @@ }, "util-deprecate": { "version": "1.0.2", - "bundled": true + "bundled": true, + "peer": true }, "wrappy": { "version": "1.0.2", - "bundled": true + "bundled": true, + "peer": true } } }, diff --git a/package.json b/package.json index 34b4f5b..c1c0895 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "next-api-og-image", - "version": "4.2.1", + "version": "4.2.2", "description": "Easy way to generate open-graph images dynamically using Next.js API Routes.", "scripts": { "prepublishOnly": "npm run build", @@ -39,10 +39,10 @@ "lib" ], "peerDependencies": { + "chrome-aws-lambda": ">=1.6.3", "next": ">=9.0.0", "react": ">=16.0.0", - "react-dom": ">=16.0.0", - "chrome-aws-lambda": ">=1.6.3" + "react-dom": ">=16.0.0" }, "prettier": { "printWidth": 110, @@ -66,6 +66,7 @@ }, "dependencies": { "deepmerge": "^4.2.2", + "is-lambda": "^1.0.1", "puppeteer-core": "^10.4.0", "twemoji": "^13.1.0" } diff --git a/src/index.ts b/src/index.ts index e3db19a..540e8f6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,6 +3,7 @@ import type { Except, RequireExactlyOne } from 'type-fest' import type { Page, Viewport } from 'puppeteer-core' import type { ReactElement } from 'react' import { renderToStaticMarkup } from 'react-dom/server' +import isLambda from 'is-lambda' import os from 'os' import deepMerge from 'deepmerge' import twemoji from 'twemoji' @@ -129,6 +130,7 @@ export function withOGImage< } const headers = await hook(extendedRequest) + if (!!headers) { for (const [extendedheaderName, extendedHeaderValue] of headers.entries()) { response.setHeader(extendedheaderName, extendedHeaderValue) @@ -252,13 +254,7 @@ function prepareWebPageFactory(viewPort: Viewport) { return { ...browserEnvironment, page } } - const chromiumOptions = !isProductionLikeMode(envMode) - ? { args: [], executablePath: executable, headless: true } - : { - args: chrome.args, - executablePath: await chrome.executablePath, - headless: chrome.headless, - } + const chromiumOptions = await getChromiumOptions(envMode, executable) const browser = await core.launch(chromiumOptions) const newPage = await browser.newPage() @@ -268,6 +264,22 @@ function prepareWebPageFactory(viewPort: Viewport) { } } +async function getChromiumOptions(envMode: EnvMode, executable: string) { + if (!isProductionLikeMode(envMode)) { + return { args: [], executablePath: executable, headless: true } + } else { + if (isLambda) { + return { + args: chrome.args, + executablePath: await chrome.executablePath, + headless: chrome.headless, + } + } else { + return { args: [], executablePath: executable, headless: true } + } + } +} + function createImageFactory({ inspectHtml, type,