Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

Generate opengraph images client side

Blue Mouseadd85ee9bbf9
diff --git a/web/app/lib/constants/index.ts b/web/app/lib/constants/index.ts
index ac6ed986a..aa37294ae 100644
--- a/web/app/lib/constants/index.ts
+++ b/web/app/lib/constants/index.ts
@@ -413,10 +413,12 @@ const NODE_ENV = isBrowser ? window.REMIX_ENV?.NODE_ENV : process.env.NODE_ENV
export const isDevelopment = !NODE_ENV || NODE_ENV === 'development'
-export const getOgImageUrl = (title: string) => {
- const apiUrl = API_URL.slice(0, -1)
+const FRONTEND_ORIGIN = isSelfhosted
+ ? (isBrowser ? window.REMIX_ENV?.BASE_URL : process.env.BASE_URL) || ''
+ : MAIN_URL
- return `${apiUrl}/v1/og-image?title=${encodeURIComponent(title)}`
+export const getOgImageUrl = (title: string) => {
+ return `${FRONTEND_ORIGIN}/og-image.png?title=${encodeURIComponent(title)}`
}
// Cookies
diff --git a/web/app/routes/og-image[.png].tsx b/web/app/routes/og-image[.png].tsx
new file mode 100644
index 000000000..d42807581
--- /dev/null
+++ b/web/app/routes/og-image[.png].tsx
@@ -0,0 +1,177 @@
+import { readFileSync } from 'node:fs'
+import { join } from 'node:path'
+
+import { ImageResponse } from '@takumi-rs/image-response'
+import type { LoaderFunctionArgs } from 'react-router'
+
+const MAX_TITLE_LENGTH = 50
+const MAX_DESCRIPTION_LENGTH = 120
+
+const logoData = readFileSync(
+ join(process.cwd(), 'public', 'assets', 'logo', 'blue.png'),
+)
+
+const persistentImages = [
+ {
+ src: 'swetrix-logo',
+ data: logoData.buffer as ArrayBuffer,
+ },
+]
+
+interface OgImageProps {
+ title: string
+ description?: string
+ label?: string
+}
+
+function OgImage({ title, description, label }: OgImageProps) {
+ const titleFontSize = title.length > 30 ? 84 : 96
+
+ return (
+ <div tw='flex w-full h-full' style={{ backgroundColor: '#f1f5f9' }}>
+ <div
+ tw='absolute'
+ style={{
+ top: 0,
+ left: 0,
+ right: 0,
+ bottom: 0,
+ backgroundImage:
+ 'linear-gradient(115deg, #f1f5f9 28%, #a855f7 70%, #4f46e5 100%)',
+ opacity: 0.5,
+ }}
+ />
+
+ <svg
+ width='1800'
+ height='945'
+ viewBox='0 0 1800 945'
+ style={{
+ position: 'absolute',
+ top: 0,
+ left: 0,
+ }}
+ >
+ <defs>
+ <pattern
+ id='grid-pattern'
+ width='36'
+ height='36'
+ patternUnits='userSpaceOnUse'
+ >
+ <path
+ d='M 36 0 L 0 0 L 0 36'
+ fill='none'
+ stroke='rgba(15, 23, 42, 0.1)'
+ strokeWidth='1'
+ />
+ </pattern>
+ <linearGradient id='grid-fade' x1='0%' y1='0%' x2='100%' y2='0%'>
+ <stop offset='0%' stopColor='white' stopOpacity='0' />
+ <stop offset='50%' stopColor='white' stopOpacity='0' />
+ <stop offset='100%' stopColor='white' stopOpacity='1' />
+ </linearGradient>
+ <mask id='grid-mask'>
+ <rect width='1800' height='945' fill='url(#grid-fade)' />
+ </mask>
+ </defs>
+ <rect
+ width='1800'
+ height='945'
+ fill='url(#grid-pattern)'
+ mask='url(#grid-mask)'
+ />
+ </svg>
+
+ <div
+ tw='flex flex-col justify-between w-full h-full'
+ style={{ padding: '108px 96px' }}
+ >
+ <div tw='flex items-center' style={{ gap: '18px' }}>
+ <img src='swetrix-logo' width={52} height={60} />
+ <div
+ style={{
+ fontSize: 48,
+ fontWeight: 600,
+ color: '#0f172a',
+ }}
+ >
+ Swetrix
+ </div>
+ </div>
+
+ <div tw='flex flex-col' style={{ maxWidth: '1425px', gap: '9px' }}>
+ {label ? (
+ <div
+ style={{
+ fontSize: 36,
+ fontWeight: 600,
+ color: '#4f46e5',
+ }}
+ >
+ {label}
+ </div>
+ ) : null}
+ <div
+ style={{
+ fontSize: titleFontSize,
+ fontWeight: 800,
+ lineHeight: 1.1,
+ letterSpacing: '-0.025em',
+ color: '#0f172a',
+ paddingBottom: description ? '0' : '8rem',
+ }}
+ >
+ {title}
+ </div>
+ {description ? (
+ <div
+ style={{
+ fontSize: 39,
+ fontWeight: 400,
+ lineHeight: 1.45,
+ color: '#334155',
+ marginTop: '18px',
+ }}
+ >
+ {description}
+ </div>
+ ) : null}
+ </div>
+ </div>
+ </div>
+ )
+}
+
+export async function loader({ request }: LoaderFunctionArgs) {
+ const url = new URL(request.url)
+ let title = url.searchParams.get('title') || 'Swetrix'
+ let description = url.searchParams.get('description') || ''
+ const label = url.searchParams.get('label') || ''
+
+ if (title.length > MAX_TITLE_LENGTH) {
+ title = `${title.slice(0, MAX_TITLE_LENGTH).trimEnd()}…`
+ }
+
+ if (description.length > MAX_DESCRIPTION_LENGTH) {
+ description = `${description.slice(0, MAX_DESCRIPTION_LENGTH).trimEnd()}…`
+ }
+
+ return new ImageResponse(
+ <OgImage
+ title={title}
+ description={description || undefined}
+ label={label || undefined}
+ />,
+ {
+ width: 1800,
+ height: 945,
+ format: 'png',
+ persistentImages,
+ headers: {
+ 'Cache-Control':
+ 'public, immutable, no-transform, s-maxage=1210000, max-age=1210000',
+ },
+ },
+ )
+}
diff --git a/web/package-lock.json b/web/package-lock.json
index 7b9b9e852..14ac857bc 100644
--- a/web/package-lock.json
+++ b/web/package-lock.json
@@ -15,6 +15,7 @@
"@react-router/serve": "^7.13.0",
"@swetrix/node": "^3.1.1",
"@tailwindcss/vite": "^4.1.18",
+ "@takumi-rs/image-response": "^0.69.5",
"@tanstack/react-virtual": "^3.13.18",
"@types/leaflet": "^1.9.21",
"@ua-parser-js/pro-business": "^2.0.6",
@@ -3190,6 +3191,176 @@
"vite": "^5.2.0 || ^6 || ^7"
}
},
+ "node_modules/@takumi-rs/core": {
+ "version": "0.69.5",
+ "resolved": "https://registry.npmjs.org/@takumi-rs/core/-/core-0.69.5.tgz",
+ "integrity": "sha512-9CLdT15SigVnAtZrlFMPAcXN72/z8u2HTFlwflaVCA019EjjYfO5sfyabCQa8JRCD2AtH/iLQHkpjMcryrozeg==",
+ "license": "(MIT OR Apache-2.0)",
+ "engines": {
+ "node": ">= 12.22.0 < 13 || >= 14.17.0 < 15 || >= 15.12.0 < 16 || >= 16.0.0"
+ },
+ "optionalDependencies": {
+ "@takumi-rs/core-darwin-arm64": "0.69.5",
+ "@takumi-rs/core-darwin-x64": "0.69.5",
+ "@takumi-rs/core-linux-arm64-gnu": "0.69.5",
+ "@takumi-rs/core-linux-arm64-musl": "0.69.5",
+ "@takumi-rs/core-linux-x64-gnu": "0.69.5",
+ "@takumi-rs/core-linux-x64-musl": "0.69.5",
+ "@takumi-rs/core-win32-arm64-msvc": "0.69.5",
+ "@takumi-rs/core-win32-x64-msvc": "0.69.5"
+ }
+ },
+ "node_modules/@takumi-rs/core-darwin-arm64": {
+ "version": "0.69.5",
+ "resolved": "https://registry.npmjs.org/@takumi-rs/core-darwin-arm64/-/core-darwin-arm64-0.69.5.tgz",
+ "integrity": "sha512-TuGKjx47LTeYOwjIzHqWcaTRbuEXWtOvgQwAAK2ZD2LrMUbAgK8BunAkcrNYWZYTzLiz85IvtZe9XD8XVneQoQ==",
+ "cpu": [
+ "arm64"
+ ],
+ "license": "(MIT OR Apache-2.0)",
+ "optional": true,
+ "os": [
+ "darwin"
+ ],
+ "engines": {
+ "node": ">= 12.22.0 < 13 || >= 14.17.0 < 15 || >= 15.12.0 < 16 || >= 16.0.0"
+ }
+ },
+ "node_modules/@takumi-rs/core-darwin-x64": {
+ "version": "0.69.5",
+ "resolved": "https://registry.npmjs.org/@takumi-rs/core-darwin-x64/-/core-darwin-x64-0.69.5.tgz",
+ "integrity": "sha512-L2kxqrc77dCHN0f+WVFE8Qf94qyCpJchO+5Q7wx91QFdoRXOA2I4AL/XzXOfNtChM+YrmJ+ljW91dJqM9nYUZQ==",
+ "cpu": [
+ "x64"
+ ],
+ "license": "(MIT OR Apache-2.0)",
+ "optional": true,
+ "os": [
+ "darwin"
+ ],
+ "engines": {
+ "node": ">= 12.22.0 < 13 || >= 14.17.0 < 15 || >= 15.12.0 < 16 || >= 16.0.0"
+ }
+ },
+ "node_modules/@takumi-rs/core-linux-arm64-gnu": {
+ "version": "0.69.5",
+ "resolved": "https://registry.npmjs.org/@takumi-rs/core-linux-arm64-gnu/-/core-linux-arm64-gnu-0.69.5.tgz",
+ "integrity": "sha512-hOZz9UZZkUGfd7xssLFoM5V7pBBVx5jdxmqmyz8SNf7gOl8+PyZM+dAm0lN0zM1b3NU4sC3k55vJi4ZgqOQtwA==",
+ "cpu": [
+ "arm64"
+ ],
+ "license": "(MIT OR Apache-2.0)",
+ "optional": true,
+ "os": [
+ "linux"
+ ],
+ "engines": {
+ "node": ">= 12.22.0 < 13 || >= 14.17.0 < 15 || >= 15.12.0 < 16 || >= 16.0.0"
+ }
+ },
+ "node_modules/@takumi-rs/core-linux-arm64-musl": {
+ "version": "0.69.5",
+ "resolved": "https://registry.npmjs.org/@takumi-rs/core-linux-arm64-musl/-/core-linux-arm64-musl-0.69.5.tgz",
+ "integrity": "sha512-krYjGAGkWniwHc29FoA0PC1wHsumuwHoaBuJnXrvx01tht/JS03rS5suoGzOh/y4jGMHMWnMF5LQGbmjnyA/Pw==",
+ "cpu": [
+ "arm64"
+ ],
+ "license": "(MIT OR Apache-2.0)",
+ "optional": true,
+ "os": [
+ "linux"
+ ],
+ "engines": {
+ "node": ">= 12.22.0 < 13 || >= 14.17.0 < 15 || >= 15.12.0 < 16 || >= 16.0.0"
+ }
+ },
+ "node_modules/@takumi-rs/core-linux-x64-gnu": {
+ "version": "0.69.5",
+ "resolved": "https://registry.npmjs.org/@takumi-rs/core-linux-x64-gnu/-/core-linux-x64-gnu-0.69.5.tgz",
+ "integrity": "sha512-eNTyttu3SLFeIVpUKHYeuhc0KF+FHqHT3lAJYqjAVujJYBJhtLjo1/VEuuG49rwEqPmIf/ZTxEp7NyGagnfr8g==",
+ "cpu": [
+ "x64"
+ ],
+ "license": "(MIT OR Apache-2.0)",
+ "optional": true,
+ "os": [
+ "linux"
+ ],
+ "engines": {
+ "node": ">= 12.22.0 < 13 || >= 14.17.0 < 15 || >= 15.12.0 < 16 || >= 16.0.0"
+ }
+ },
+ "node_modules/@takumi-rs/core-linux-x64-musl": {
+ "version": "0.69.5",
+ "resolved": "https://registry.npmjs.org/@takumi-rs/core-linux-x64-musl/-/core-linux-x64-musl-0.69.5.tgz",
+ "integrity": "sha512-Rl4X0DqnrExE741gIzag+332wRzd1dJoMHEbOP9u45srmd5D/AlYR/pIHfP3J4VG42ZtxVxImblNUhNTjSrtZw==",
+ "cpu": [
+ "x64"
+ ],
+ "license": "(MIT OR Apache-2.0)",
+ "optional": true,
+ "os": [
+ "linux"
+ ],
+ "engines": {
+ "node": ">= 12.22.0 < 13 || >= 14.17.0 < 15 || >= 15.12.0 < 16 || >= 16.0.0"
+ }
+ },
+ "node_modules/@takumi-rs/core-win32-arm64-msvc": {
+ "version": "0.69.5",
+ "resolved": "https://registry.npmjs.org/@takumi-rs/core-win32-arm64-msvc/-/core-win32-arm64-msvc-0.69.5.tgz",
+ "integrity": "sha512-i+6DZwJI7K537fnPx0S88AvwR07HxDvbmFcVKjm+KsoBCce1U5HTnyv+1Z/Fx5XnoDDPyZtCdeyF1fx62R44Ww==",
+ "cpu": [
+ "arm64"
+ ],
+ "license": "(MIT OR Apache-2.0)",
+ "optional": true,
+ "os": [
+ "win32"
+ ],
+ "engines": {
+ "node": ">= 12.22.0 < 13 || >= 14.17.0 < 15 || >= 15.12.0 < 16 || >= 16.0.0"
+ }
+ },
+ "node_modules/@takumi-rs/core-win32-x64-msvc": {
+ "version": "0.69.5",
+ "resolved": "https://registry.npmjs.org/@takumi-rs/core-win32-x64-msvc/-/core-win32-x64-msvc-0.69.5.tgz",
+ "integrity": "sha512-z2fiefantHZgaWJqzEoUuEuiDgo5ovjAPJVhZm7tl7I/8OLBMD04vwNQd9VGW/eAtggNhty9Q4tWUfz9bevsrA==",
+ "cpu": [
+ "x64"
+ ],
+ "license": "(MIT OR Apache-2.0)",
+ "optional": true,
+ "os": [
+ "win32"
+ ],
+ "engines": {
+ "node": ">= 12.22.0 < 13 || >= 14.17.0 < 15 || >= 15.12.0 < 16 || >= 16.0.0"
+ }
+ },
+ "node_modules/@takumi-rs/helpers": {
+ "version": "0.69.5",
+ "resolved": "https://registry.npmjs.org/@takumi-rs/helpers/-/helpers-0.69.5.tgz",
+ "integrity": "sha512-jQAd1lsjLDKasvEsSJzB0yTVDNV9aTVVgHYtUNlx5+Og4zyH5EyUTyehTvPhGW5wUDxmPUhphvtcolOIk/oZiA==",
+ "license": "(MIT OR Apache-2.0)"
+ },
+ "node_modules/@takumi-rs/image-response": {
+ "version": "0.69.5",
+ "resolved": "https://registry.npmjs.org/@takumi-rs/image-response/-/image-response-0.69.5.tgz",
+ "integrity": "sha512-tjB9KbgADO9elXaVkhNK0BK65yIt52AqvD44BR0K4Hq/pPET2iyzvMyR5gEMZ0/m6PQMBXE8mOiMq2JyJPR/DQ==",
+ "license": "(MIT OR Apache-2.0)",
+ "dependencies": {
+ "@takumi-rs/core": "0.69.5",
+ "@takumi-rs/helpers": "0.69.5",
+ "@takumi-rs/wasm": "0.69.5"
+ }
+ },
+ "node_modules/@takumi-rs/wasm": {
+ "version": "0.69.5",
+ "resolved": "https://registry.npmjs.org/@takumi-rs/wasm/-/wasm-0.69.5.tgz",
+ "integrity": "sha512-mUVYgJs235H3UDMhpiNCAZDOMc1yDoLYj6Mu7sUhWjJ1+p/W4fVFiB5F8ZQ7dZ+jZXquiQCIUdDnc2EjnoDo5g==",
+ "license": "(MIT OR Apache-2.0)"
+ },
"node_modules/@tanstack/react-virtual": {
"version": "3.13.18",
"resolved": "https://registry.npmjs.org/@tanstack/react-virtual/-/react-virtual-3.13.18.tgz",
diff --git a/web/package.json b/web/package.json
index 3670d8da0..264598bee 100644
--- a/web/package.json
+++ b/web/package.json
@@ -27,6 +27,7 @@
"@react-router/serve": "^7.13.0",
"@swetrix/node": "^3.1.1",
"@tailwindcss/vite": "^4.1.18",
+ "@takumi-rs/image-response": "^0.69.5",
"@tanstack/react-virtual": "^3.13.18",
"@types/leaflet": "^1.9.21",
"@ua-parser-js/pro-business": "^2.0.6",
diff --git a/web/vite.config.ts b/web/vite.config.ts
index 68a9de852..6d5b2e8c4 100644
--- a/web/vite.config.ts
+++ b/web/vite.config.ts
@@ -22,6 +22,7 @@ export default defineConfig({
'billboard.js',
/^remix-i18next.*/
],
+ external: ['@takumi-rs/image-response'],
},
plugins: [reactRouter(), tsconfigPaths(), tailwindcss()],
})