From f63aa97529296599cce2a79fb03e1007652d59f8 Mon Sep 17 00:00:00 2001 From: Steven Date: Tue, 5 Feb 2019 15:31:19 -0500 Subject: [PATCH] =?UTF-8?q?Fix=20local=20development=20=F0=9F=92=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- now.json | 2 +- public/index.html | 4 ++-- src/browser.ts | 2 +- src/card.ts | 11 ++--------- src/chromium.ts | 18 +++++++----------- src/options.ts | 27 +++++++++++++++++++++++++++ 6 files changed, 40 insertions(+), 24 deletions(-) create mode 100644 src/options.ts diff --git a/now.json b/now.json index e2769ae..f873248 100644 --- a/now.json +++ b/now.json @@ -13,7 +13,7 @@ { "src": "/", "dest": "/public/index.html" }, { "src": "/favicon.ico", "dest": "/public/favicon.ico" }, { "src": "/style.css", "dest": "/public/style.css" }, - { "src": "/browser.js", "dest": "/browser.js" }, + { "src": "/dist/browser.js", "dest": "/browser.js" }, { "src": "/(.+)", "dest": "/src/card.ts" } ] } diff --git a/public/index.html b/public/index.html index 60c1c48..9e9f0ce 100644 --- a/public/index.html +++ b/public/index.html @@ -34,7 +34,7 @@ - + - \ No newline at end of file + diff --git a/src/browser.ts b/src/browser.ts index a655d21..a04b722 100644 --- a/src/browser.ts +++ b/src/browser.ts @@ -187,7 +187,7 @@ const App = (_: any, state: AppState, setState: SetState) => { } = state; const mdValue = md ? '1' : '0'; const imageOptions = theme === 'light' ? imageLightOptions : imageDarkOptions; - const url = new URL(window.location.hostname === 'localhost' ? 'https://og-image.now.sh' : window.location.origin); + const url = new URL(window.location.hostname === 'localhost' ? 'http://localhost:13463' : window.location.origin); url.pathname = `${encodeURIComponent(text)}.${fileType}`; url.searchParams.append('theme', theme); url.searchParams.append('md', mdValue); diff --git a/src/card.ts b/src/card.ts index 80dd341..a788a2b 100644 --- a/src/card.ts +++ b/src/card.ts @@ -10,17 +10,10 @@ export default async function handler(req: IncomingMessage, res: ServerResponse) try { const parsedReq = parseRequest(req); const html = getHtml(parsedReq); - - if (isDev) { - res.setHeader('Content-Type', 'text/html'); - res.end(html); - return; - } - const { text, fileType } = parsedReq; const filePath = await writeTempFile(text, html); const fileUrl = pathToFileURL(filePath); - const file = await getScreenshot(fileUrl, fileType); + const file = await getScreenshot(fileUrl, fileType, isDev); res.statusCode = 200; res.setHeader('Content-Type', `image/${fileType}`); res.setHeader('Cache-Control', `public, immutable, no-transform, max-age=31536000`); @@ -38,4 +31,4 @@ if (isDev) { const PORT = process.env.PORT || 13463; const listen = () => console.log(`Listening on ${PORT}...`); createServer(handler).listen(PORT, listen); -} \ No newline at end of file +} diff --git a/src/chromium.ts b/src/chromium.ts index 1635722..be9da27 100644 --- a/src/chromium.ts +++ b/src/chromium.ts @@ -1,25 +1,21 @@ -import * as chromeAws from 'chrome-aws-lambda'; import { launch, Page } from 'puppeteer-core'; +import { getOptions as getLaunchOptions } from './options'; let _page: Page | null; -async function getPage() { +async function getPage(isDev: boolean) { if (_page) { return _page; } - const chrome = chromeAws as any; - const browser = await launch({ - args: chrome.args, - executablePath: await chrome.executablePath, - headless: chrome.headless, - }); + const options = await getLaunchOptions(isDev); + const browser = await launch(options); _page = await browser.newPage(); return _page; } -export async function getScreenshot(url: string, type: FileType) { - const page = await getPage(); +export async function getScreenshot(url: string, type: FileType, isDev: boolean) { + const page = await getPage(isDev); await page.setViewport({ width: 2048, height: 1170 }); await page.goto(url); const file = await page.screenshot({ type }); return file; -} \ No newline at end of file +} diff --git a/src/options.ts b/src/options.ts new file mode 100644 index 0000000..93e7d5e --- /dev/null +++ b/src/options.ts @@ -0,0 +1,27 @@ +import * as chromeAws from 'chrome-aws-lambda'; +const chrome = chromeAws as any; +const exePath = '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'; + +interface Options { + args: string[]; + executablePath: string; + headless: boolean; +} + +export async function getOptions(isDev: boolean) { + let options: Options; + if (isDev) { + options = { + args: [], + executablePath: exePath, + headless: true, + }; + } else { + options = { + args: chrome.args, + executablePath: await chrome.executablePath, + headless: chrome.headless, + }; + } + return options; +} \ No newline at end of file