-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Add share redirect page with OG meta tags and thumbnail generation #2156
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
Open
gary149
wants to merge
10
commits into
main
Choose a base branch
from
claude/custom-conversation-thumbnails-WZNK0
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
e996b3c
Add custom OG thumbnails for shared conversations
claude a5576ea
Fix /r/ meta-tag exclusion to be base-path aware
gary149 8cecbd8
Add OG tags to /conversation/[id] for shared conversations
gary149 c77888d
Fix thumbnail 404 for missing shares and wrong OG URL for imports
gary149 26bb8cc
Replace yellow chat bubble icon with "Shared conversation" pill label
gary149 7f43279
Increase shared conversation label size and weight
gary149 2ba48c6
Make shared conversation label bigger (32px)
gary149 cfa6dad
Remove model name and 'Shared on' text from thumbnail
gary149 553e966
Increase conversation title font size to 58px
gary149 6c12034
Merge branch 'main' into claude/custom-conversation-thumbnails-WZNK0
gary149 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
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
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,26 @@ | ||
| import type { PageServerLoad } from "./$types"; | ||
| import { collections } from "$lib/server/database"; | ||
| import { config } from "$lib/server/config"; | ||
| import { base } from "$app/paths"; | ||
|
|
||
| export const load: PageServerLoad = async ({ params, url }) => { | ||
| const shareId = params.id; | ||
|
|
||
| const shared = await collections.sharedConversations.findOne( | ||
| { _id: shareId }, | ||
| { projection: { title: 1, model: 1 } } | ||
| ); | ||
|
|
||
| const shareTitle = shared?.title || "Shared Conversation"; | ||
| const shareModel = shared?.model ?? ""; | ||
| const origin = config.get("PUBLIC_ORIGIN") || url.origin; | ||
| const appName = config.get("PUBLIC_APP_NAME") || "HuggingChat"; | ||
|
|
||
| return { | ||
| shareTitle, | ||
| shareModel, | ||
| shareId, | ||
| appName, | ||
| ogImageUrl: `${origin}${base}/r/${shareId}/thumbnail.png`, | ||
| }; | ||
| }; |
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,72 @@ | ||
| <script lang="ts"> | ||
| import { onMount } from "svelte"; | ||
| import { goto } from "$app/navigation"; | ||
| import { base } from "$app/paths"; | ||
| import { page } from "$app/state"; | ||
| import { useAPIClient, handleResponse } from "$lib/APIClient"; | ||
| import { usePublicConfig } from "$lib/utils/PublicConfig.svelte"; | ||
|
|
||
| let { data } = $props(); | ||
|
|
||
| const publicConfig = usePublicConfig(); | ||
|
|
||
| onMount(async () => { | ||
| const leafId = page.url.searchParams.get("leafId"); | ||
| const shareId = page.params.id; | ||
|
|
||
| // If logged in, try to import the share | ||
| if (data.loginEnabled && data.user && shareId) { | ||
| const client = useAPIClient(); | ||
| try { | ||
| const result = await client.conversations["import-share"] | ||
| .post({ shareId }) | ||
| .then(handleResponse); | ||
| if (result.conversationId) { | ||
| await goto( | ||
| `${base}/conversation/${result.conversationId}?leafId=${leafId ?? ""}&fromShare=${shareId}`, | ||
| { replaceState: true } | ||
| ); | ||
| return; | ||
| } | ||
| } catch { | ||
| // Fall through to view-only mode | ||
| } | ||
| } | ||
|
|
||
| // Not logged in or import failed: view-only mode | ||
| await goto(`${base}/conversation/${shareId}${leafId ? `?leafId=${leafId}` : ""}`, { | ||
| replaceState: true, | ||
| }); | ||
| }); | ||
| </script> | ||
|
|
||
| <svelte:head> | ||
| <title>{data.shareTitle} - {publicConfig.PUBLIC_APP_NAME}</title> | ||
| <meta property="og:title" content="{data.shareTitle} - {publicConfig.PUBLIC_APP_NAME}" /> | ||
| <meta property="og:type" content="website" /> | ||
| <meta | ||
| property="og:description" | ||
| content="Check out this conversation on {publicConfig.PUBLIC_APP_NAME}" | ||
| /> | ||
| <meta property="og:image" content={data.ogImageUrl} /> | ||
| <meta property="og:image:alt" content={data.shareTitle} /> | ||
| <meta property="og:image:width" content="1200" /> | ||
| <meta property="og:image:height" content="648" /> | ||
| <meta | ||
| property="og:url" | ||
| content="{publicConfig.PUBLIC_ORIGIN || page.url.origin}{base}/r/{data.shareId}" | ||
| /> | ||
| <meta property="og:site_name" content={publicConfig.PUBLIC_APP_NAME} /> | ||
| <meta name="twitter:card" content="summary_large_image" /> | ||
| <meta name="twitter:title" content="{data.shareTitle} - {publicConfig.PUBLIC_APP_NAME}" /> | ||
| <meta | ||
| name="twitter:description" | ||
| content="Check out this conversation on {publicConfig.PUBLIC_APP_NAME}" | ||
| /> | ||
| <meta name="twitter:image" content={data.ogImageUrl} /> | ||
| <meta name="twitter:image:alt" content={data.shareTitle} /> | ||
| </svelte:head> | ||
|
|
||
| <div class="flex h-full items-center justify-center"> | ||
| <div class="animate-pulse text-gray-400">Loading conversation...</div> | ||
| </div> |
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
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,66 @@ | ||
| import ConversationThumbnail from "./ConversationThumbnail.svelte"; | ||
| import type { RequestHandler } from "@sveltejs/kit"; | ||
|
|
||
| import { Resvg } from "@resvg/resvg-js"; | ||
| import satori from "satori"; | ||
| import { html } from "satori-html"; | ||
|
|
||
| import InterRegular from "$lib/server/fonts/Inter-Regular.ttf"; | ||
| import InterBold from "$lib/server/fonts/Inter-Bold.ttf"; | ||
| import { collections } from "$lib/server/database"; | ||
| import { config } from "$lib/server/config"; | ||
| import { render } from "svelte/server"; | ||
|
|
||
| export const GET: RequestHandler = async ({ params }) => { | ||
| const shareId = params.id; | ||
|
|
||
| const shared = await collections.sharedConversations.findOne( | ||
| { _id: shareId }, | ||
| { projection: { title: 1, model: 1 } } | ||
| ); | ||
|
|
||
| const title = shared?.title || "Untitled Conversation"; | ||
| const modelName = shared?.model ? (shared.model.split("/").pop() ?? shared.model) : ""; | ||
|
|
||
| const renderedComponent = render(ConversationThumbnail, { | ||
| props: { | ||
| title, | ||
| modelName, | ||
| isHuggingChat: config.isHuggingChat, | ||
| }, | ||
| }); | ||
|
|
||
| const reactLike = html( | ||
| "<style>" + renderedComponent.head + "</style>" + renderedComponent.body | ||
| ) as unknown as never; | ||
|
|
||
| const svg = await satori(reactLike, { | ||
| width: 1200, | ||
| height: 648, | ||
| fonts: [ | ||
| { | ||
| name: "Inter", | ||
| data: InterRegular as unknown as ArrayBuffer, | ||
| weight: 500, | ||
| }, | ||
| { | ||
| name: "Inter", | ||
| data: InterBold as unknown as ArrayBuffer, | ||
| weight: 700, | ||
| }, | ||
| ], | ||
| }); | ||
|
|
||
| const png = new Resvg(svg, { | ||
| fitTo: { mode: "original" }, | ||
| }) | ||
| .render() | ||
| .asPng(); | ||
|
|
||
| return new Response(new Uint8Array(png), { | ||
| headers: { | ||
| "Content-Type": "image/png", | ||
| "Cache-Control": "public, max-age=86400, s-maxage=604800, stale-while-revalidate=604800", | ||
| }, | ||
| }); | ||
| }; |
64 changes: 64 additions & 0 deletions
64
src/routes/r/[id]/thumbnail.png/ConversationThumbnail.svelte
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,64 @@ | ||
| <script lang="ts"> | ||
| import logo from "../../../../../static/huggingchat/fulltext-logo.svg?raw"; | ||
|
|
||
| interface Props { | ||
| title: string; | ||
| modelName?: string; | ||
| isHuggingChat?: boolean; | ||
| } | ||
|
|
||
| let { title, modelName = "", isHuggingChat = false }: Props = $props(); | ||
|
|
||
| // Satori doesn't reliably support text-overflow: ellipsis, | ||
| // so we truncate manually for predictable rendering. | ||
| function truncateTitle(text: string, maxLength: number): string { | ||
| if (text.length <= maxLength) return text; | ||
| return text.substring(0, maxLength - 1).trimEnd() + "\u2026"; | ||
| } | ||
|
|
||
| const displayTitle = truncateTitle(title, 80); | ||
| </script> | ||
|
|
||
| <div | ||
| class="flex h-[648px] w-[1200px] flex-col items-center justify-center bg-black text-white" | ||
| style="background-image: url(https://cdn-uploads.huggingface.co/production/uploads/5f17f0a0925b9863e28ad517/L4XVRJ7MsfFDD7ROx_geO.png);" | ||
| > | ||
| <!-- Chat bubble icon --> | ||
| <div | ||
| style="display: flex; align-items: center; justify-content: center; width: 72px; height: 72px; border-radius: 18px; background: linear-gradient(135deg, rgba(255,210,0,0.25), rgba(255,150,0,0.15)); border: 1px solid rgba(255,210,0,0.3); margin-bottom: 32px;" | ||
| > | ||
| <svg width="36" height="36" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"> | ||
| <path | ||
| d="M12 2C6.48 2 2 5.58 2 10c0 2.24 1.12 4.26 2.94 5.7L4 22l4.73-2.84C9.77 19.72 10.86 20 12 20c5.52 0 10-3.58 10-8s-4.48-8-10-8z" | ||
| fill="rgba(255,210,0,0.9)" | ||
| /> | ||
| </svg> | ||
| </div> | ||
|
|
||
| <!-- Conversation title --> | ||
| <div | ||
| style="font-size: 48px; font-weight: 700; color: white; text-align: center; max-width: 1000px; line-height: 1.25; overflow: hidden; display: flex; align-items: center; justify-content: center;" | ||
| > | ||
| {displayTitle} | ||
| </div> | ||
|
|
||
| <!-- Model name subtitle --> | ||
| {#if modelName} | ||
| <div style="font-size: 22px; color: rgba(255,255,255,0.5); margin-top: 20px; font-weight: 500;"> | ||
| {modelName} | ||
| </div> | ||
| {/if} | ||
|
|
||
| <!-- HuggingChat branding --> | ||
| {#if isHuggingChat} | ||
| <div | ||
| style="display: flex; align-items: center; margin-top: 32px; font-size: 28px; color: white;" | ||
| > | ||
| <div style="margin-right: 12px; font-size: 22px; color: rgba(255,255,255,0.7);"> | ||
| Shared on | ||
| </div> | ||
| <!-- eslint-disable-next-line --> | ||
| {@html logo} | ||
| </div> | ||
| {/if} | ||
| </div> |
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.
I checked
svelte.config.jsandkit.paths.baseis configurable viaAPP_BASE, sopage.url.pathnamecan be prefixed (for example/chat/r/<id>). With the currentpathname.startsWith('/r/')guard, those deployments won't skip the default OG/Twitter tags, so/r/[id]pages render both global and share-specific metadata and crawlers may pick the wrong preview title/image.Useful? React with 👍 / 👎.