|
| 1 | +import { Router, Request, Response } from 'express'; |
| 2 | +import { fetchProjectMetaBySlug, fetchProjectMetaById, ProjectMeta } from '../hasura.js'; |
| 3 | + |
| 4 | +// OpenGraph cards for shared project links. Caddy routes only social-crawler |
| 5 | +// requests for /u/* and /projects/* here; real users fall through to the SPA. |
| 6 | +// So we serve a small purpose-built page with the project's card metadata |
| 7 | +// (og:image -> the rendered screenshot) — crawlers parse the tags, they don't |
| 8 | +// run the app. |
| 9 | +const router = Router(); |
| 10 | + |
| 11 | +const PUBLIC_ORIGIN = (process.env.PUBLIC_ORIGIN ?? 'https://code.zxplay.org').replace(/\/$/, ''); |
| 12 | +const SLUG = /^[a-z0-9-]+$/i; |
| 13 | +const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i; |
| 14 | + |
| 15 | +function esc(s: string): string { |
| 16 | + return s |
| 17 | + .replace(/&/g, '&') |
| 18 | + .replace(/"/g, '"') |
| 19 | + .replace(/</g, '<') |
| 20 | + .replace(/>/g, '>'); |
| 21 | +} |
| 22 | + |
| 23 | +const SITE_TITLE = 'Code · ZX Play'; |
| 24 | +const SITE_DESC = 'A ZX Spectrum emulator & programming environment for the browser.'; |
| 25 | +const SITE_IMAGE = `${PUBLIC_ORIGIN}/assets/images/embed-preview.png`; |
| 26 | + |
| 27 | +function ogPage(opts: { title: string; description: string; image: string; url: string }): string { |
| 28 | + const title = esc(opts.title); |
| 29 | + const description = esc(opts.description); |
| 30 | + const image = esc(opts.image); |
| 31 | + const url = esc(opts.url); |
| 32 | + return `<!doctype html> |
| 33 | +<html lang="en"> |
| 34 | +<head> |
| 35 | +<meta charset="utf-8"> |
| 36 | +<title>${title}</title> |
| 37 | +<meta property="og:type" content="website"> |
| 38 | +<meta property="og:title" content="${title}"> |
| 39 | +<meta property="og:description" content="${description}"> |
| 40 | +<meta property="og:image" content="${image}"> |
| 41 | +<meta property="og:url" content="${url}"> |
| 42 | +<meta name="twitter:card" content="summary_large_image"> |
| 43 | +<meta name="twitter:title" content="${title}"> |
| 44 | +<meta name="twitter:description" content="${description}"> |
| 45 | +<meta name="twitter:image" content="${image}"> |
| 46 | +</head> |
| 47 | +<body><p><a href="${esc(opts.url || PUBLIC_ORIGIN)}">View on ZX Play</a></p></body> |
| 48 | +</html>`; |
| 49 | +} |
| 50 | + |
| 51 | +function projectCard(meta: ProjectMeta, canonicalUrl: string): string { |
| 52 | + const version = Date.parse(meta.updatedAt) || 0; |
| 53 | + return ogPage({ |
| 54 | + title: `${meta.title} · ZX Play`, |
| 55 | + description: `${meta.title} — a ZX Spectrum program on ZX Play.`, |
| 56 | + image: `${PUBLIC_ORIGIN}/screenshots/${meta.projectId}.png?v=${version}`, |
| 57 | + url: canonicalUrl, |
| 58 | + }); |
| 59 | +} |
| 60 | + |
| 61 | +const genericCard = (url: string): string => |
| 62 | + ogPage({ title: SITE_TITLE, description: SITE_DESC, image: SITE_IMAGE, url: url || PUBLIC_ORIGIN }); |
| 63 | + |
| 64 | +async function send(res: Response, html: string): Promise<void> { |
| 65 | + // Short cache: a crawler re-fetch picks up edits / new screenshots reasonably soon. |
| 66 | + res.setHeader('Cache-Control', 'public, max-age=300'); |
| 67 | + res.type('html').send(html); |
| 68 | +} |
| 69 | + |
| 70 | +router.get('/u/:user/:project', async (req: Request, res: Response) => { |
| 71 | + const user = (req.params.user || '').toLowerCase(); |
| 72 | + const project = (req.params.project || '').toLowerCase(); |
| 73 | + const canonical = `${PUBLIC_ORIGIN}/u/${req.params.user}/${req.params.project}`; |
| 74 | + if (SLUG.test(user) && SLUG.test(project)) { |
| 75 | + try { |
| 76 | + const meta = await fetchProjectMetaBySlug(user, project); |
| 77 | + if (meta) { |
| 78 | + await send(res, projectCard(meta, canonical)); |
| 79 | + return; |
| 80 | + } |
| 81 | + } catch (err) { |
| 82 | + console.error('OG slug lookup failed:', err); |
| 83 | + } |
| 84 | + } |
| 85 | + await send(res, genericCard(canonical)); |
| 86 | +}); |
| 87 | + |
| 88 | +router.get('/projects/:id', async (req: Request, res: Response) => { |
| 89 | + const id = (req.params.id || '').toLowerCase(); |
| 90 | + const canonical = `${PUBLIC_ORIGIN}/projects/${req.params.id}`; |
| 91 | + if (UUID.test(id)) { |
| 92 | + try { |
| 93 | + const meta = await fetchProjectMetaById(id); |
| 94 | + if (meta) { |
| 95 | + await send(res, projectCard(meta, canonical)); |
| 96 | + return; |
| 97 | + } |
| 98 | + } catch (err) { |
| 99 | + console.error('OG id lookup failed:', err); |
| 100 | + } |
| 101 | + } |
| 102 | + await send(res, genericCard(canonical)); |
| 103 | +}); |
| 104 | + |
| 105 | +// Other crawler hits under these prefixes (profiles, lists) → generic site card. |
| 106 | +router.get(['/u', '/u/*', '/projects', '/projects/*'], async (_req: Request, res: Response) => { |
| 107 | + await send(res, genericCard('')); |
| 108 | +}); |
| 109 | + |
| 110 | +export default router; |
0 commit comments