Skip to content
This repository has been archived by the owner on Jan 21, 2023. It is now read-only.

How to deploy og-image on Netlify? #172

Closed
iamrajiv opened this issue Oct 5, 2021 · 2 comments
Closed

How to deploy og-image on Netlify? #172

iamrajiv opened this issue Oct 5, 2021 · 2 comments

Comments

@iamrajiv
Copy link

iamrajiv commented Oct 5, 2021

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

@styfle
Copy link
Member

styfle commented Nov 8, 2021

You'll probably need to convert the API from Vercel Functions to Netlify Functions but I haven't tried it

@styfle styfle closed this as completed Nov 8, 2021
@andhisan
Copy link

andhisan commented Feb 9, 2022

Seems too late but I finally figured out how to deploy on Netlify.

Codes

netlify.toml

[build]
  command = "yarn build"
  publish = "public"

[functions]
  directory = "api"
  external_node_modules = ["chrome-aws-lambda"]

api/index.ts

import { 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 isDev needs to be tweaked for Netlify lambda because AWS_REGION is also set when run with Netlify CLI on local. I tested with several env variables but even NODE_ENV is not set on Netlify lambda environment.

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"

api/dist directory is not needed because Netlify natively supports TypeScript. So we do only type-check here.

Note

I still can't figure out how to shorten function URLs.

echo .netlify >> .gitignore

Also, don't forget to ignore .netlify.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants