This repository has been archived by the owner on Jan 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
How to deploy og-image
on Netlify?
#172
Comments
You'll probably need to convert the API from Vercel Functions to Netlify Functions but I haven't tried it |
Seems too late but I finally figured out how to deploy on Netlify. Codesnetlify.toml[build]
command = "yarn build"
publish = "public"
[functions]
directory = "api"
external_node_modules = ["chrome-aws-lambda"] api/index.tsimport { parseRequest } from './_lib/parser';
import { getScreenshot } from './_lib/chromium';
import { getHtml } from './_lib/template';
import { Handler } from '@netlify/functions';
import { Buffer } from 'buffer';
const isHtmlDebug = process.env.OG_HTML_DEBUG === '1';
export const handler: Handler = async (event, ctx) => {
const isDev = !ctx.clientContext?.custom?.netlify;
try {
const parsedReq = parseRequest(event);
const html = getHtml(parsedReq, isHtmlDebug);
if (isHtmlDebug) {
return {
statusCode: 200,
headers: {
'Content-Type': 'text/html',
'Cache-Control': '',
},
body: html,
};
}
const { fileType } = parsedReq;
const file = await getScreenshot(html, fileType, isDev);
return {
statusCode: 200,
headers: {
'Content-Type': `image/${fileType}`,
'Cache-Control': `public, immutable, no-transform, s-maxage=31536000, max-age=31536000`,
},
body: Buffer.from(file).toString('base64'),
isBase64Encoded: true,
};
} catch (e) {
console.error(e);
return {
statusCode: 500,
headers: {
'Content-Type': 'text/html',
'Cache-Control': '',
},
body: '<h1>Internal Error</h1><p>Sorry, there was a problem</p>',
};
}
}; The tricky part is that api/_lib/parser.ts-import { IncomingMessage } from 'http';
+import { Event } from '@netlify/functions/dist/function/event';
import { parse } from 'url';
import { ParsedRequest, Theme } from './types';
-export function parseRequest(req: IncomingMessage) {
+export function parseRequest(req: Event) {
console.log('HTTP ' + req.url);
- const { pathname, query } = parse(req.url || '/', true);
+ const { pathname, query } = parse(req.path.replace('/.netlify/functions/index', '') || '/', true); Not cool but this works anyway package.json- "build": "tsc -p api/tsconfig.json && tsc -p web/tsconfig.json"
+ "build": "tsc -p api/tsconfig.json --noEmit && tsc -p web/tsconfig.json"
NoteI still can't figure out how to shorten function URLs. echo .netlify >> .gitignore Also, don't forget to ignore |
Closed
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Description
I want to deploy
https://github.com/vercel/og-image
on Netlify. I tried deploying and it deployed successfully but it is not loading anything.https://og-image.netlify.app
The text was updated successfully, but these errors were encountered: