-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(SEO): dynamic og image generator
- Loading branch information
1 parent
45ca3d9
commit 49d371b
Showing
17 changed files
with
496 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { ogGeneratorHandler } from 'shared/server/og-generator'; | ||
|
||
export default ogGeneratorHandler; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import core from 'puppeteer-core'; | ||
import { getOptions } from './options'; | ||
import { FileType } from './types'; | ||
let _page: core.Page | null; | ||
|
||
async function getPage(isDev: boolean) { | ||
if (_page) { | ||
return _page; | ||
} | ||
const options = await getOptions(isDev); | ||
const browser = await core.launch(options); | ||
_page = await browser.newPage(); | ||
return _page; | ||
} | ||
|
||
export async function getScreenshot(html: string, type: FileType, isDev: boolean) { | ||
const page = await getPage(isDev); | ||
await page.setViewport({ width: 2048, height: 1170 }); | ||
await page.setContent(html); | ||
const file = await page.screenshot({ type }); | ||
return file; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './og-generator.handler'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { IncomingMessage, ServerResponse } from 'http'; | ||
import { parseRequest } from './parser'; | ||
import { getScreenshot } from './chromium'; | ||
import { getHtml } from './template'; | ||
|
||
const isDev = !process.env.AWS_REGION; | ||
const isHtmlDebug = process.env.OG_HTML_DEBUG === '1'; | ||
|
||
export async function ogGeneratorHandler( | ||
req: IncomingMessage, | ||
res: ServerResponse, | ||
) { | ||
try { | ||
const parsedReq = parseRequest(req); | ||
const html = getHtml(parsedReq); | ||
if (isHtmlDebug) { | ||
res.setHeader('Content-Type', 'text/html'); | ||
res.end(html); | ||
return; | ||
} | ||
const { fileType } = parsedReq; | ||
const file = await getScreenshot(html, fileType, isDev); | ||
res.statusCode = 200; | ||
res.setHeader('Content-Type', `image/${fileType}`); | ||
res.setHeader( | ||
'Cache-Control', | ||
`public, immutable, no-transform, s-maxage=31536000, max-age=31536000`, | ||
); | ||
res.end(file); | ||
} catch (e) { | ||
res.statusCode = 500; | ||
res.setHeader('Content-Type', 'text/html'); | ||
res.end('<div></div>'); | ||
console.error(e); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import chrome from 'chrome-aws-lambda'; | ||
const exePath = process.platform === 'win32' | ||
? 'C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe' | ||
: process.platform === 'linux' | ||
? '/usr/bin/google-chrome' | ||
: '/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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { IncomingMessage } from 'http'; | ||
import { parse } from 'url'; | ||
import { APIRoutes } from '../../constants/client'; | ||
import { ParsedRequest } from './types'; | ||
|
||
const BASE_URL = `${APIRoutes.OG_IMAGE}/`; | ||
|
||
export function parseRequest(req: IncomingMessage) { | ||
console.log('HTTP ' + req.url); | ||
const { pathname, query } = parse(req.url || '/', true); | ||
const { md } = query || {}; | ||
|
||
const arr = (pathname ?? '').replace(BASE_URL, '').split('.'); | ||
let extension = ''; | ||
let text = ''; | ||
if (arr.length === 0) { | ||
text = ''; | ||
} else if (arr.length === 1) { | ||
text = arr[0]; | ||
} else { | ||
extension = arr.pop() as string; | ||
text = arr.join('.'); | ||
} | ||
|
||
const parsedRequest: ParsedRequest = { | ||
fileType: extension === 'jpeg' ? extension : 'png', | ||
text: decodeURIComponent(text), | ||
md: md === '1' || md === 'true', | ||
}; | ||
|
||
return parsedRequest; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
const entityMap: { [key: string]: string } = { | ||
'&': '&', | ||
'<': '<', | ||
'>': '>', | ||
'"': '"', | ||
"'": ''', | ||
'/': '/', | ||
}; | ||
|
||
export function sanitizeHtml(html: string) { | ||
return String(html).replace(/[&<>"'/]/g, (key) => entityMap[key]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { readFileSync } from 'fs'; | ||
import { marked } from 'marked'; | ||
import twemoji from 'twemoji'; | ||
import { sanitizeHtml } from './sanitizer'; | ||
import { ParsedRequest } from './types'; | ||
const twOptions = { folder: 'svg', ext: '.svg' }; | ||
const emojify = (text: string) => twemoji.parse(text, twOptions); | ||
|
||
let rglr: string; | ||
|
||
const readFonts = () => { | ||
if (rglr) { | ||
return; | ||
} | ||
|
||
rglr = readFileSync( | ||
`${__dirname}/../../../../public/fonts/silka/silka-medium-webfont.woff2`, | ||
).toString('base64'); | ||
}; | ||
|
||
function getCss() { | ||
readFonts(); | ||
|
||
const background = '#000000'; | ||
const headingColor = '#F2AA4C'; | ||
const bodyColor = '#ffffff'; | ||
|
||
return ` | ||
@font-face { | ||
font-family: "Silka"; | ||
src: url(data:font/woff2;charset=utf-8;base64,${rglr}) format('woff2'); | ||
font-weight: 400; | ||
font-style: normal; | ||
} | ||
body { | ||
background: ${background}; | ||
color: ${bodyColor}; | ||
height: 100vh; | ||
display: flex; | ||
text-align: center; | ||
align-items: center; | ||
justify-content: center; | ||
font-size: 80px; | ||
font-family: Silka; | ||
} | ||
.emoji { | ||
height: 1em; | ||
width: 1em; | ||
margin: 0 .05em 0 .1em; | ||
vertical-align: -0.1em; | ||
} | ||
.heading { | ||
color: ${headingColor}; | ||
line-height: 1.3; | ||
margin-bottom: 20px; | ||
max-width: 70vw; | ||
} | ||
p { | ||
margin: 0; | ||
}`; | ||
} | ||
|
||
export function getHtml(parsedReq: ParsedRequest) { | ||
const { text, md } = parsedReq; | ||
return `<!DOCTYPE html> | ||
<html> | ||
<meta charset="utf-8"> | ||
<title>Generated Image</title> | ||
<meta name="viewport" content="width=device-width, initial-scale=1"> | ||
<style> | ||
${getCss()} | ||
</style> | ||
<body> | ||
<div> | ||
<div class="heading">${emojify( | ||
md ? marked(text) : sanitizeHtml(text), | ||
)} | ||
</div> | ||
<p>andreas.fyi</p> | ||
</div> | ||
</body> | ||
</html>`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export type FileType = 'png' | 'jpeg'; | ||
|
||
export interface ParsedRequest { | ||
fileType: FileType; | ||
text: string; | ||
md?: boolean; | ||
} |
Oops, something went wrong.