-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b6adfa7
commit d848b57
Showing
7 changed files
with
86 additions
and
37 deletions.
There are no files selected for viewing
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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 |
---|---|---|
@@ -1,40 +1,44 @@ | ||
import { ImageResponse } from '@ethercorps/sveltekit-og' | ||
import { loadTokenData } from '../../token/[id]/data' | ||
import type { RequestHandler } from './$types' | ||
import { error } from '@sveltejs/kit' | ||
import OgToken from './og-token.svelte' | ||
import inter400 from '@fontsource/inter/files/inter-latin-400-normal.woff' | ||
import inter600 from '@fontsource/inter/files/inter-latin-600-normal.woff' | ||
import robotoMono400 from '@fontsource/roboto-mono/files/roboto-mono-latin-400-normal.woff' | ||
import { error } from '@sveltejs/kit' | ||
import { loadTokenData } from '../../token/[id]/data' | ||
import OgToken from './og-token.svelte' | ||
import { svelteToPngResponse } from './svelte-to-png' | ||
import type { RequestHandler } from './$types' | ||
|
||
export const GET: RequestHandler = async ({ params: { token_id }, fetch }) => { | ||
const props = await loadTokenData(token_id, fetch) | ||
if (!props.token) throw error(404, 'Token not found') | ||
try { | ||
const inter400Data = await fetch(inter400).then((res) => res.arrayBuffer()) | ||
const inter600Data = await fetch(inter600).then((res) => res.arrayBuffer()) | ||
const robotoMono400Data = await fetch(robotoMono400).then((res) => res.arrayBuffer()) | ||
|
||
const props = await loadTokenData(token_id, fetch) | ||
if (!props.token) throw error(404, 'Token not found') | ||
|
||
return new ImageResponse( | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-explicit-any | ||
OgToken as any, | ||
{ | ||
return await svelteToPngResponse(OgToken, props, { | ||
width: 1200, | ||
height: 630, | ||
fonts: [ | ||
{ | ||
name: 'sans-serif', | ||
data: await fetch(inter400).then((res) => res.arrayBuffer()), | ||
data: inter400Data, | ||
weight: 400, | ||
}, | ||
{ | ||
name: 'sans-serif', | ||
data: await fetch(inter600).then((res) => res.arrayBuffer()), | ||
data: inter600Data, | ||
weight: 600, | ||
}, | ||
{ | ||
name: 'monospace', | ||
data: await fetch(robotoMono400).then((res) => res.arrayBuffer()), | ||
data: robotoMono400Data, | ||
weight: 400, | ||
}, | ||
], | ||
}, | ||
props, | ||
) | ||
}) | ||
} catch (err) { | ||
console.error('error building opengraph image', err) | ||
throw error(500, err instanceof Error ? err.message : 'Opengraph image error') | ||
} | ||
} |
This file contains 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,8 @@ | ||
import type { ComponentType, SvelteComponent } from 'svelte' | ||
import type { SatoriOptions } from 'satori' | ||
|
||
export function svelteToPngResponse( | ||
component: ComponentType<SvelteComponent>, | ||
props: Record<string, unknown>, | ||
options: SatoriOptions, | ||
): Promise<Response> |
This file contains 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,43 @@ | ||
import satori from 'satori' | ||
import { Resvg } from '@resvg/resvg-js' | ||
|
||
export async function svelteToPngResponse(component, props, options) { | ||
let toReactElement | ||
// ugh, this is ugly | ||
await import('@ethercorps/svelte-h2j').then((module) => { | ||
toReactElement = module.toReactElement | ||
}) | ||
|
||
const reactElement = svelteComponentToJsx(component, props) | ||
const svg = await satori(reactElement, options) | ||
|
||
const resvg = new Resvg(svg, { | ||
fitTo: { | ||
mode: 'width', | ||
value: options.width, | ||
}, | ||
font: { | ||
// It will be faster to disable loading system fonts. | ||
loadSystemFonts: false, | ||
}, | ||
}) | ||
const pngData = resvg.render() | ||
const pngBuffer = pngData.asPng() | ||
|
||
return new Response(pngBuffer, { | ||
headers: { | ||
'Content-Type': 'image/png', | ||
'Cache-Control': 'public, immutable, no-transform, max-age=31536000', | ||
}, | ||
status: 200, | ||
}) | ||
|
||
function svelteComponentToJsx(component, props) { | ||
const SvelteRenderedMarkup = component.render(props) | ||
let htmlTemplate = SvelteRenderedMarkup.html || '' | ||
if (SvelteRenderedMarkup.css.code) { | ||
htmlTemplate = `${htmlTemplate}<style>${SvelteRenderedMarkup.css.code}</style>` | ||
} | ||
return toReactElement(htmlTemplate) | ||
} | ||
} |
This file contains 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