Skip to content

Commit

Permalink
Merge pull request #46 from neg4n/custom-chrome-options
Browse files Browse the repository at this point in the history
Ability to set custom chrome options
  • Loading branch information
neg4n authored Sep 21, 2022
2 parents 1e579a5 + ad35f13 commit 2a9769d
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 9 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,15 @@ const nextApiOgImageConfig = {
// The hook function return is Map containing custom headers that will be set BEFORE sending
// response to the client.
hook: null,
// NOTE: Options within 'chrome' object only works when next-api-og-image is run in server (not serverless!!) environment.
chrome: {
// Custom command-line args passed to the browser start command
// by default, no arguments are provided.
args: null,
// Custom executable provided. Useful when you e.g. have to run Chromium instead of Google Chrome
// by default, executable is retrieved automatically (it looks for Google Chrome in the filesystem)
executable: null,
}
// NOTE: Options within 'dev' object works only when process.env.NODE_ENV === 'development'
dev: {
// Whether to replace binary data (image/screenshot) with HTML
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "next-api-og-image",
"version": "4.2.2",
"version": "4.3.0",
"description": "Easy way to generate open-graph images dynamically using Next.js API Routes.",
"scripts": {
"prepublishOnly": "npm run build",
Expand Down
35 changes: 29 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ type NextApiRequestWithOgImage = {
image: string | Buffer
}

type ChromeOptions = {
args?: string[]
executable?: string
}

export type NextApiOgImageConfig<
Strategy extends StrategyOption,
StrategyDetails extends string | object = string,
Expand All @@ -47,6 +52,7 @@ export type NextApiOgImageConfig<
type?: ImageType
quality?: number
hook?: (request: NextApiRequestWithOgImage) => Map<string, string> | Promise<Map<string, string>>
chrome?: ChromeOptions
dev?: Partial<{
inspectHtml: boolean
errorsInResponse: boolean
Expand All @@ -72,6 +78,10 @@ export function withOGImage<
type: 'png',
quality: 90,
hook: null,
chrome: {
args: null,
executable: null,
},
dev: {
inspectHtml: true,
errorsInResponse: true,
Expand All @@ -89,6 +99,7 @@ export function withOGImage<
hook,
height,
quality,
chrome: { args, executable },
dev: { inspectHtml, errorsInResponse },
} = options

Expand All @@ -104,7 +115,7 @@ export function withOGImage<

const createBrowserEnvironment = pipe(
getChromiumExecutable,
prepareWebPageFactory({ width, height }),
prepareWebPageFactory({ width, height }, { args, executable }),
createImageFactory({ inspectHtml, type, quality }),
)

Expand Down Expand Up @@ -246,15 +257,15 @@ function getChromiumExecutable(browserEnvironment: BrowserEnvironment) {
return { ...browserEnvironment, executable }
}

function prepareWebPageFactory(viewPort: Viewport) {
function prepareWebPageFactory(viewPort: Viewport, chromeOptions: ChromeOptions) {
return async function (browserEnvironment: BrowserEnvironment) {
const { page, envMode, executable } = browserEnvironment

if (page) {
return { ...browserEnvironment, page }
}

const chromiumOptions = await getChromiumOptions(envMode, executable)
const chromiumOptions = await getChromiumOptions(envMode, executable, chromeOptions)

const browser = await core.launch(chromiumOptions)
const newPage = await browser.newPage()
Expand All @@ -264,9 +275,17 @@ function prepareWebPageFactory(viewPort: Viewport) {
}
}

async function getChromiumOptions(envMode: EnvMode, executable: string) {
async function getChromiumOptions(
envMode: EnvMode,
defaultExecutable: string,
chromeOptions?: ChromeOptions,
) {
if (!isProductionLikeMode(envMode)) {
return { args: [], executablePath: executable, headless: true }
return {
args: chromeOptions?.args ?? [],
executablePath: chromeOptions?.executable ?? defaultExecutable,
headless: true,
}
} else {
if (isLambda) {
return {
Expand All @@ -275,7 +294,11 @@ async function getChromiumOptions(envMode: EnvMode, executable: string) {
headless: chrome.headless,
}
} else {
return { args: [], executablePath: executable, headless: true }
return {
args: chromeOptions?.args ?? [],
executablePath: chromeOptions?.executable ?? defaultExecutable,
headless: true,
}
}
}
}
Expand Down

0 comments on commit 2a9769d

Please sign in to comment.