format project

This commit is contained in:
Kurt Hutten
2021-08-01 09:44:10 +10:00
parent caf944716b
commit ad0e9c1d4d
4 changed files with 43 additions and 15 deletions

View File

@@ -2,14 +2,14 @@
// Got the proof of concept working locally, but even though chrome-aws-lambda is supposed to fit into a AWS lambda it did not for me // Got the proof of concept working locally, but even though chrome-aws-lambda is supposed to fit into a AWS lambda it did not for me
// in the mean time this is causing builds to fail so moved it out here. // in the mean time this is causing builds to fail so moved it out here.
import { builder } from '@netlify/functions' import { builder } from '@netlify/functions'
const { headless, executablePath, puppeteer} = require('chrome-aws-lambda') const { headless, executablePath, puppeteer } = require('chrome-aws-lambda')
const captureWidth = 1200 const captureWidth = 1200
const captureHeight = 630 const captureHeight = 630
const clipY = 0 const clipY = 0
async function unwrappedHandler (event, context) { async function unwrappedHandler(event, context) {
let path = event.path const path = event.path
.replace(/.+\/og-image-generator/, '') .replace(/.+\/og-image-generator/, '')
.replace(/\/og-image-.+\.jpg/, '') .replace(/\/og-image-.+\.jpg/, '')
@@ -19,17 +19,23 @@ async function unwrappedHandler (event, context) {
executablePath: process.env.URL?.includes('localhost') executablePath: process.env.URL?.includes('localhost')
? null ? null
: await executablePath, : await executablePath,
args: ['--no-sandbox','--disable-web-security','--disable-gpu', '--hide-scrollbars', '--disable-setuid-sandbox'], args: [
'--no-sandbox',
'--disable-web-security',
'--disable-gpu',
'--hide-scrollbars',
'--disable-setuid-sandbox',
],
// args: chromium.args, // args: chromium.args,
defaultViewport: { defaultViewport: {
width: captureWidth, width: captureWidth,
height: captureHeight + clipY height: captureHeight + clipY,
}, },
headless: headless headless: headless,
}) })
const page = await browser.newPage() const page = await browser.newPage()
await page.goto(url, {"waitUntil" : "networkidle0"}); await page.goto(url, { waitUntil: 'networkidle0' })
const screenshot = await page.screenshot({ const screenshot = await page.screenshot({
type: 'jpeg', type: 'jpeg',
@@ -40,8 +46,8 @@ async function unwrappedHandler (event, context) {
x: 0, x: 0,
y: clipY, y: clipY,
width: captureWidth, width: captureWidth,
height: captureHeight height: captureHeight,
} },
}) })
await browser.close() await browser.close()
@@ -55,10 +61,10 @@ async function unwrappedHandler (event, context) {
return { return {
statusCode: 200, statusCode: 200,
headers: { headers: {
'Content-Type': 'image/jpg' 'Content-Type': 'image/jpg',
}, },
body: screenshot, body: screenshot,
isBase64Encoded: true isBase64Encoded: true,
} }
} }

View File

@@ -1,6 +1,16 @@
import { Helmet } from 'react-helmet' import { Helmet } from 'react-helmet'
const Seo = ({ title, description, lang, socialImageUrl}: { title: string; description: string; lang: string; socialImageUrl?: string}) => { const Seo = ({
title,
description,
lang,
socialImageUrl,
}: {
title: string
description: string
lang: string
socialImageUrl?: string
}) => {
return ( return (
<> <>
<Helmet <Helmet

View File

@@ -41,7 +41,10 @@ export const Success = ({
const image = userProject?.Project?.mainImage const image = userProject?.Project?.mainImage
const gravatar = userProject?.image const gravatar = userProject?.image
return ( return (
<div className="flex-col flex h-screen bg-ch-gray-800 text-ch-gray-300" id="social-card-loaded"> <div
className="flex-col flex h-screen bg-ch-gray-800 text-ch-gray-300"
id="social-card-loaded"
>
<div <div
className="flex-grow grid" className="flex-grow grid"
style={{ gridTemplateColumns: '7fr 5fr' }} style={{ gridTemplateColumns: '7fr 5fr' }}

View File

@@ -9,11 +9,20 @@ import { Toaster } from '@redwoodjs/web/toast'
const ProjectPage = ({ userName, projectTitle }) => { const ProjectPage = ({ userName, projectTitle }) => {
const { currentUser } = useAuth() const { currentUser } = useAuth()
const [state, thunkDispatch] = useIdeState() const [state, thunkDispatch] = useIdeState()
const cacheInvalidator = new Date().toISOString().split('-').slice(0, 2).join('-') const cacheInvalidator = new Date()
.toISOString()
.split('-')
.slice(0, 2)
.join('-')
const socialImageUrl = `/.netlify/functions/og-image-generator/${userName}/${projectTitle}/og-image-${cacheInvalidator}.jpg` const socialImageUrl = `/.netlify/functions/og-image-generator/${userName}/${projectTitle}/og-image-${cacheInvalidator}.jpg`
return ( return (
<> <>
<Seo title={projectTitle} description={projectTitle} socialImageUrl={socialImageUrl} lang="en-US" /> <Seo
title={projectTitle}
description={projectTitle}
socialImageUrl={socialImageUrl}
lang="en-US"
/>
<Toaster timeout={1500} /> <Toaster timeout={1500} />
<IdeContext.Provider value={{ state, thunkDispatch, project: null }}> <IdeContext.Provider value={{ state, thunkDispatch, project: null }}>
<ProjectCell <ProjectCell