-
-
Notifications
You must be signed in to change notification settings - Fork 168
Feat:add og image component and add feature flag for og image component #1107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
f315757
f18fb3e
12d0005
d391d82
774050f
caee206
0882aef
8ec71cd
80c0219
7fe1702
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| import { ImageResponse } from "next/og"; | ||
|
|
||
| import PostHogClient from "@/app/posthog"; | ||
| export const runtime = "edge"; | ||
|
|
||
| const height = 630; | ||
|
|
@@ -8,7 +8,13 @@ const width = 1200; | |
| export async function GET(request: Request) { | ||
| try { | ||
| const { searchParams } = new URL(request.url); | ||
|
|
||
| const posthogClient = PostHogClient() | ||
| const flags = await getData() | ||
| const origin = `${request.headers.get('x-forwarded-proto') || 'http'}://${request.headers.get("host")}` | ||
| posthogClient.capture({ | ||
| distinctId:"south-in", | ||
| event:"Og image generated" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoid hardcoding 'distinctId' to prevent PII leakage. Using a hardcoded Apply this diff to fix the issue: ...Also applies to: 218-219 |
||
| }) | ||
| // ?title=<title> | ||
| const hasTitle = searchParams.has("title"); | ||
| const title = hasTitle | ||
|
|
@@ -21,8 +27,131 @@ export async function GET(request: Request) { | |
| import.meta.url, | ||
| ), | ||
| ).then((res) => res.arrayBuffer()); | ||
|
|
||
| return new ImageResponse( | ||
| if (flags['og-image']){ | ||
| return new ImageResponse( | ||
| ( | ||
| <div | ||
| tw="flex flex-col h-full w-full justify-center" | ||
| style={{padding: "0 88px",backgroundColor:"#1d1b36",backgroundRepeat:"repeat",background: ` | ||
| url('${origin}/images/og/noise.svg'), | ||
| radial-gradient(circle at bottom left, rgba(255, 255, 255, 0.1), transparent 40%), | ||
| radial-gradient(circle at top right, rgba(255, 255, 255, 0.1), transparent 40%) | ||
| ` }} | ||
| > | ||
| <div className="line1" style={{ | ||
| width:"1200px", | ||
| height:"30px", | ||
| borderTop:"1px solid #39374E", | ||
| borderBottom:"1px solid #39374E", | ||
| position:"absolute", | ||
| top:"50px" | ||
| }}></div> | ||
| <div className="line2" style={{ | ||
| width:"1200px", | ||
| height:"30px", | ||
| borderTop:"1px solid #39374E", | ||
| borderBottom:"1px solid #39374E", | ||
| position:"absolute", | ||
| bottom:"50px" | ||
| }}></div> | ||
| <div className="line3" style={{ | ||
| width:"30px", | ||
| height:"100%", | ||
| borderRight:"1px solid #39374E", | ||
| position:"absolute", | ||
| left:"50px" | ||
| }}></div> | ||
| <div className="line4" style={{ | ||
| width:"30px", | ||
| height:"100%", | ||
| borderLeft:"1px solid #39374E", | ||
| position:"absolute", | ||
| right:"50px" | ||
| }}></div> | ||
| <img src={`${origin}/images/og/waves.svg`} style={{ | ||
| position:"absolute", | ||
| top:"0", | ||
| left:"0", | ||
| width:"1200", | ||
| height:"630" | ||
| }}/> | ||
| <img src={`${origin}/images/og/stars.svg`} style={{position:"absolute", | ||
| top:"0", | ||
| left:"0", | ||
| width:"1200", | ||
| height:"630" | ||
| }}/> | ||
| <img src={`${origin}/images/og/planet.svg`} style={{ | ||
| position:"absolute", | ||
| height:"188px", | ||
| width:"188px", | ||
| right:"0", | ||
| top:"10px" | ||
| }}/> | ||
|
|
||
| <img | ||
| alt="Codu Logo" | ||
| style={{ | ||
| position: "absolute", | ||
| height: "53px", | ||
| width: "163px", | ||
| top: "88px", | ||
| left: "86px", | ||
| }} | ||
| src="https://www.codu.co/_next/image?url=%2Fimages%2Fcodu.png&w=1920&q=75" | ||
| /> | ||
| <div tw="flex relative flex-col" style={{marginTop:"200px"}}> | ||
| <div | ||
| style={{ | ||
| color: "white", | ||
| fontSize: "52px", | ||
| lineHeight: 1, | ||
| fontWeight: "800", | ||
| letterSpacing: "-.025em", | ||
| fontFamily:"Lato", | ||
| lineClamp: 3, | ||
|
|
||
| }} | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Replace invalid CSS property 'lineClamp' with proper text truncation. The Apply this diff to fix the issue: Alternatively, if you're using Tailwind CSS with the Ensure the plugin is installed and configured correctly.
|
||
| > | ||
| {title} | ||
| </div> | ||
| <div tw="flex justify-between mt-7 text-white"> | ||
| <div style={{display:"flex",flexDirection:"column",gap:"0.5rem"}}> | ||
| <div>Ben Gover</div> | ||
| <span>26th January 2024 . 4 min read</span> | ||
| </div> | ||
| <button style={{ | ||
| padding: '10px 20px', | ||
| fontSize: '16px', | ||
| fontWeight: 'bold', | ||
| color: 'transparent', | ||
| background: 'linear-gradient(to bottom right, #FCE9A7 0%, #FF7379 53%, #F963ED 100%)', | ||
| backgroundClip: 'text', | ||
| WebkitBackgroundClip: 'text', | ||
| border: '2px solid #FF7379', | ||
|
|
||
| }}> | ||
| Article | ||
| </button> | ||
| </div> | ||
| </div> | ||
|
|
||
| </div> | ||
| ), | ||
| { | ||
| fonts: [ | ||
| { | ||
| name: "Inter Latin", | ||
| data: fontData, | ||
| style: "normal", | ||
| }, | ||
| ], | ||
| height, | ||
| width, | ||
| }, | ||
| ); | ||
| } else { | ||
| return new ImageResponse( | ||
| ( | ||
| <div | ||
| tw="bg-black flex flex-col h-full w-full justify-center" | ||
|
|
@@ -45,7 +174,7 @@ export async function GET(request: Request) { | |
| color: "white", | ||
| fontSize: "64px", | ||
| lineHeight: 1, | ||
| fontWeight: "800", | ||
| fontWeight:"bold", | ||
| letterSpacing: "-.025em", | ||
| lineClamp: 3, | ||
| }} | ||
|
|
@@ -74,9 +203,18 @@ export async function GET(request: Request) { | |
| width, | ||
| }, | ||
| ); | ||
|
|
||
| } | ||
|
|
||
| } catch { | ||
| return new Response(`Failed to generate the image`, { | ||
| status: 500, | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| async function getData(){ | ||
| const postHog = PostHogClient() | ||
| const flags = await postHog.getAllFlags("south-in") | ||
| return flags | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,8 @@ | ||||||||||||||
| import {PostHog} from 'posthog-node' | ||||||||||||||
|
|
||||||||||||||
| export default function PostHogClient(){ | ||||||||||||||
| const posthogClient = new PostHog(process.env.NEXT_PUBLIC_POSTHOG_KEY||"",{ | ||||||||||||||
| host: process.env.NEXT_PUBLIC_POSTHOG_HOST | ||||||||||||||
| }) | ||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Security concern: Potential exposure of PostHog key to client-side. The use of environment variables prefixed with Consider renaming these environment variables to remove the - const posthogClient = new PostHog(process.env.NEXT_PUBLIC_POSTHOG_KEY||"",{
- host: process.env.NEXT_PUBLIC_POSTHOG_HOST
+ const posthogClient = new PostHog(process.env.POSTHOG_KEY||"",{
+ host: process.env.POSTHOG_HOSTAlso, update any configuration files or deployment scripts to use these new variable names. 📝 Committable suggestion
Suggested change
|
||||||||||||||
| return posthogClient | ||||||||||||||
| } | ||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Optimize 'getData' function placement and usage.
The
getDatafunction is declared after it's called, which can affect readability. Additionally, creating a new PostHog client insidegetDataleads to multiple instances. Pass the existingposthogClienttogetDataand consider moving the function above its usage.Apply this diff to refactor:
Also applies to: 216-220