Skip to content

Commit 49caa52

Browse files
committed
Switched to Parameter Structure to generate OG Image URL
Encode query parameters containing urls for facebook and other social media platforms
1 parent 13b7738 commit 49caa52

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

api/_lib/parser.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ import { ParsedRequest, Theme } from './types'
55
export function parseRequest(req: IncomingMessage) {
66
console.log('HTTP ' + req.url)
77
const { pathname, query } = parse(req.url || '/', true)
8-
// @todo: Switch to param stuctucture here: https://github.com/spinks/og-image/commit/45439b73b3a5156faf9f6757fc4f208b230f5d9a
9-
// to bypass facebook issue (facebook strips query params that appear to be urls)
10-
const { fontFamily, fontSize, images, widths, heights, theme, md } = query || {}
8+
const { fontFamily, fontSize, images, widths, heights, theme, md, imageObj } = query || {}
119

10+
if (Array.isArray(imageObj)) {
11+
throw new Error('Expected a single image Object');
12+
}
1213
if (Array.isArray(fontFamily)) {
1314
throw new Error('Expected a single fontFamily')
1415
}
@@ -19,6 +20,14 @@ export function parseRequest(req: IncomingMessage) {
1920
throw new Error('Expected a single theme')
2021
}
2122

23+
let parsedImages = JSON.parse(imageObj || '{}');
24+
if (Object.keys(parsedImages).length === 0) {
25+
console.log('Legacy image format');
26+
parsedImages.images = getArray(images);
27+
parsedImages.widths = getArray(widths);
28+
parsedImages.heights = getArray(heights);
29+
}
30+
2231
const arr = (pathname || '/').slice(1).split('.')
2332
let extension = ''
2433
let text = ''
@@ -38,9 +47,9 @@ export function parseRequest(req: IncomingMessage) {
3847
md: md === '1' || md === 'true',
3948
fontFamily: fontFamily === 'roboto-condensed' ? 'Roboto Condensed' : 'Source Sans Pro',
4049
fontSize: fontSize || '96px',
41-
images: getArray(images),
42-
widths: getArray(widths),
43-
heights: getArray(heights),
50+
images: parsedImages.images,
51+
widths: parsedImages.widths,
52+
heights: parsedImages.heights,
4453
}
4554
parsedRequest.images = getDefaultImages(
4655
parsedRequest.images,

0 commit comments

Comments
 (0)