-
Notifications
You must be signed in to change notification settings - Fork 898
feat(marketing): add dynamic OG image for changelog pages #1209
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
180 changes: 180 additions & 0 deletions
180
apps/marketing/src/app/changelog/[slug]/opengraph-image.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,180 @@ | ||
| import fs from "node:fs"; | ||
| import path from "node:path"; | ||
| import { ImageResponse } from "next/og"; | ||
| import { getChangelogEntry } from "@/lib/changelog"; | ||
| import { formatChangelogDate } from "@/lib/changelog-utils"; | ||
|
|
||
| export const alt = "Superset Changelog"; | ||
| export const size = { width: 1200, height: 630 }; | ||
| export const contentType = "image/png"; | ||
|
|
||
| function readFileAsDataUri({ | ||
| filePath, | ||
| mime, | ||
| }: { | ||
| filePath: string; | ||
| mime: string; | ||
| }): string | null { | ||
| try { | ||
| const absolutePath = path.join(process.cwd(), "public", filePath); | ||
| const buffer = fs.readFileSync(absolutePath); | ||
| return `data:${mime};base64,${buffer.toString("base64")}`; | ||
| } catch { | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| const interBold = fetch( | ||
| "https://fonts.gstatic.com/s/inter/v18/UcCO3FwrK3iLTeHuS_nVMrMxCp50SjIw2boKoduKmMEVuFuYMZhrib2Bg-4.ttf", | ||
| ).then((res) => res.arrayBuffer()); | ||
|
|
||
| export default async function Image({ | ||
| params, | ||
| }: { | ||
| params: Promise<{ slug: string }>; | ||
| }) { | ||
| const { slug } = await params; | ||
| const entry = getChangelogEntry(slug); | ||
| const fontData = await interBold; | ||
| const logoDataUri = readFileAsDataUri({ | ||
| filePath: "title.svg", | ||
| mime: "image/svg+xml", | ||
| }); | ||
|
|
||
| if (!entry) { | ||
| return new ImageResponse( | ||
| <div | ||
| style={{ | ||
| background: "#0a0a0a", | ||
| width: "100%", | ||
| height: "100%", | ||
| display: "flex", | ||
| alignItems: "center", | ||
| justifyContent: "center", | ||
| color: "#ffffff", | ||
| fontSize: 48, | ||
| fontFamily: "Inter", | ||
| }} | ||
| > | ||
| Superset Changelog | ||
| </div>, | ||
| { | ||
| ...size, | ||
| fonts: [ | ||
| { name: "Inter", data: fontData, weight: 700, style: "normal" }, | ||
| ], | ||
| }, | ||
| ); | ||
| } | ||
|
|
||
| const coverImageUri = entry.image | ||
| ? readFileAsDataUri({ | ||
| filePath: entry.image, | ||
| mime: "image/png", | ||
| }) | ||
|
Comment on lines
+70
to
+74
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. Derive the cover MIME type from the file extension
✅ Example+const MIME_BY_EXT: Record<string, string> = {
+ ".png": "image/png",
+ ".jpg": "image/jpeg",
+ ".jpeg": "image/jpeg",
+ ".webp": "image/webp",
+ ".svg": "image/svg+xml",
+};
+
const coverImageUri = entry.image
? readFileAsDataUri({
filePath: entry.image,
- mime: "image/png",
+ mime: MIME_BY_EXT[path.extname(entry.image).toLowerCase()] ?? "image/png",
})
: null;🤖 Prompt for AI Agents |
||
| : null; | ||
|
|
||
| return new ImageResponse( | ||
| <div | ||
| style={{ | ||
| background: "#0a0a0a", | ||
| width: "100%", | ||
| height: "100%", | ||
| display: "flex", | ||
| position: "relative", | ||
| fontFamily: "Inter", | ||
| }} | ||
| > | ||
| {/* Background cover image */} | ||
| {coverImageUri && ( | ||
| // biome-ignore lint/a11y/useAltText: ImageResponse requires native <img> | ||
| // biome-ignore lint/performance/noImgElement: ImageResponse requires native <img> | ||
| <img | ||
| src={coverImageUri} | ||
| style={{ | ||
| position: "absolute", | ||
| top: 0, | ||
| left: 0, | ||
| width: "100%", | ||
| height: "100%", | ||
| objectFit: "cover", | ||
| opacity: 0.7, | ||
| }} | ||
| /> | ||
| )} | ||
|
|
||
| {/* Dark gradient overlay for text legibility */} | ||
| <div | ||
| style={{ | ||
| position: "absolute", | ||
| top: 0, | ||
| left: 0, | ||
| width: "100%", | ||
| height: "100%", | ||
| background: | ||
| "linear-gradient(to bottom, rgba(10,10,10,0.65) 0%, rgba(10,10,10,0.25) 50%, rgba(10,10,10,0.7) 100%)", | ||
| }} | ||
| /> | ||
|
|
||
| {/* Content */} | ||
| <div | ||
| style={{ | ||
| position: "relative", | ||
| width: "100%", | ||
| height: "100%", | ||
| display: "flex", | ||
| flexDirection: "column", | ||
| justifyContent: "space-between", | ||
| padding: "48px 64px", | ||
| }} | ||
| > | ||
| {/* Title + date */} | ||
| <div style={{ display: "flex", flexDirection: "column", gap: 16 }}> | ||
| <div | ||
| style={{ | ||
| fontSize: 56, | ||
| fontWeight: 700, | ||
| color: "#ffffff", | ||
| lineHeight: 1.2, | ||
| maxWidth: "90%", | ||
| }} | ||
| > | ||
| {entry.title} | ||
| </div> | ||
| <div style={{ fontSize: 24, color: "#999999" }}> | ||
| {formatChangelogDate(entry.date)} | ||
| </div> | ||
| </div> | ||
|
|
||
| {/* Bottom: logo left-aligned */} | ||
| <div | ||
| style={{ | ||
| display: "flex", | ||
| justifyContent: "flex-start", | ||
| alignItems: "center", | ||
| }} | ||
| > | ||
| {logoDataUri ? ( | ||
| // biome-ignore lint/a11y/useAltText: ImageResponse requires native <img> | ||
| // biome-ignore lint/performance/noImgElement: ImageResponse requires native <img> | ||
| <img src={logoDataUri} height={120} /> | ||
| ) : ( | ||
| <div | ||
| style={{ | ||
| fontSize: 48, | ||
| fontWeight: 700, | ||
| color: "#ffffff", | ||
| }} | ||
| > | ||
| Superset | ||
| </div> | ||
| )} | ||
| </div> | ||
| </div> | ||
| </div>, | ||
| { | ||
| ...size, | ||
| fonts: [{ name: "Inter", data: fontData, weight: 700, style: "normal" }], | ||
| }, | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Normalize public asset paths and log unexpected read failures
If
filePathstarts with/(common for public assets),path.join()discards thepublicprefix and the read fails. Also, the catch currently swallows all errors, making failures hard to diagnose.🛠️ Suggested fix
function readFileAsDataUri({ filePath, mime, }: { filePath: string; mime: string; }): string | null { try { - const absolutePath = path.join(process.cwd(), "public", filePath); + const relativePath = filePath.replace(/^\/+/, ""); + const absolutePath = path.join(process.cwd(), "public", relativePath); const buffer = fs.readFileSync(absolutePath); return `data:${mime};base64,${buffer.toString("base64")}`; - } catch { + } catch (err) { + const error = err as NodeJS.ErrnoException; + if (error.code !== "ENOENT") { + console.error("[og/readFileAsDataUri] failed to read asset", { + filePath, + error, + }); + } return null; } }As per coding guidelines, Never swallow errors silently; at minimum log errors with context before rethrowing or handling them explicitly; and Use prefixed console logging with consistent context pattern: [domain/operation] message for entry/exit of significant operations, external API calls, and error conditions.
📝 Committable suggestion
🤖 Prompt for AI Agents