Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

feat(seo): per-route meta for shared-shell pages and canonical slash URLs

nirholasf92dee47df1c
diff --git a/data/changelog.json b/data/changelog.json
index b9f8878f3..23373383e 100644
--- a/data/changelog.json
+++ b/data/changelog.json
@@ -17536,6 +17536,16 @@
"docs"
],
"link": "/mcp-tools"
+ },
+ {
+ "date": "2026-07-31",
+ "title": "Every docs page and tutorial now shows up properly in search and shares",
+ "summary": "Until now, sharing or searching any of the 300 documentation and tutorial pages showed the same generic card and sent search engines to the docs homepage instead of the page itself. Each page now carries its own title, description, preview image, and address, so links unfurl correctly in chats and social posts, and search results land readers on the exact page. Duplicate slash-variant URLs now redirect to one canonical address too.",
+ "tags": [
+ "improvement",
+ "infra"
+ ],
+ "link": "/docs"
}
]
}
diff --git a/server/index.mjs b/server/index.mjs
index 5681cc3e6..d1ba35aae 100644
--- a/server/index.mjs
+++ b/server/index.mjs
@@ -36,6 +36,7 @@ import { existsSync, statSync, readdirSync, readFileSync } from 'node:fs';
import path from 'node:path';
import { pathToFileURL, fileURLToPath } from 'node:url';
import { isSsrRoute, renderSsrPage } from './ssr-pages.mjs';
+import { hasSeoRoute, renderSeoHead } from './seo-head.mjs';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const ROOT = path.resolve(__dirname, '..');
@@ -432,6 +433,21 @@ app.use(async (req, res) => {
const extraQuery = {};
let fileStatus = null;
+ // The route table serves /x and /x/ identically via duplicated entries, so
+ // every page exists at two URLs and crawlers see duplicate content held
+ // together only by canonical tags. Collapse the slash variant permanently.
+ // API paths keep their exact form and the root is already bare.
+ if (
+ (req.method === 'GET' || req.method === 'HEAD') &&
+ currentPath.length > 1 &&
+ currentPath.endsWith('/') &&
+ !currentPath.startsWith('/api/')
+ ) {
+ const bare = currentPath.replace(/\/+$/, '') || '/';
+ res.redirect(301, bare + url.search);
+ return;
+ }
+
// Phase 1: rules before {handle: "filesystem"}.
for (const route of phase1Routes) {
const m = route.re.exec(currentPath);
@@ -510,6 +526,22 @@ app.use(async (req, res) => {
console.error(`[ssr] ${currentPath} fell back to the static shell:`, err.message);
}
}
+ // Shared-shell routes (/docs/*, /tutorials/*) all resolve to one HTML
+ // file whose static head names the shell's own route. Rewrite the head
+ // for the page actually requested so each route presents its own
+ // canonical, title, social card and JSON-LD. Uses the ORIGINAL request
+ // path: currentPath is already the dest rewrite (/docs/index.html).
+ if (req.method === 'GET' && file.endsWith('.html') && hasSeoRoute(url.pathname)) {
+ const html = renderSeoHead(url.pathname, file);
+ if (html) {
+ res.set(collected);
+ if (fileStatus) res.status(fileStatus);
+ res.set('content-type', 'text/html; charset=utf-8');
+ res.set('cache-control', 'public, max-age=60, s-maxage=300');
+ res.send(html);
+ return;
+ }
+ }
await serveFile(req, res, file, collected, fileStatus);
return;
}
diff --git a/server/seo-head.mjs b/server/seo-head.mjs
new file mode 100644
index 000000000..70b35cea2
--- /dev/null
+++ b/server/seo-head.mjs
@@ -0,0 +1,225 @@
+// Per-request <head> rewriting for shared-shell routes.
+//
+// Most pages get their SEO meta stamped statically at build time by
+// scripts/inject-seo-meta.mjs. Routes that share one template file cannot:
+// /docs/* (about 230 routes) and /tutorials/* (about 70) all serve the same
+// shell, so every one of them told crawlers the same canonical URL, title,
+// description and social card as the shell's own route. This module fixes that
+// at serve time: when a request path is a catalogued page (data/pages.json)
+// but the shell it resolves to carries a different canonical, the head is
+// rewritten with the page's own title, description, canonical, Open Graph and
+// Twitter tags, and JSON-LD before the response leaves the server.
+//
+// The title format, the dynamic page-og card params, and the JSON-LD graph
+// shape all mirror scripts/inject-seo-meta.mjs, so a crawler cannot tell
+// whether a page was stamped at build time or rewritten here.
+//
+// Safety rules this module holds itself to:
+// - Never fail a page: any error falls back to the untouched shell.
+// - A page whose canonical already matches the request path owns its meta
+// and is never touched (that is every statically stamped page).
+// - Results are cached per path; dist/ is immutable for a deploy's lifetime.
+
+import { readFileSync } from 'node:fs';
+import path from 'node:path';
+import { fileURLToPath } from 'node:url';
+
+const __dirname = path.dirname(fileURLToPath(import.meta.url));
+const ORIGIN = 'https://three.ws';
+
+function htmlEscape(s) {
+ return String(s)
+ .replace(/&/g, '&amp;')
+ .replace(/</g, '&lt;')
+ .replace(/>/g, '&gt;')
+ .replace(/"/g, '&quot;');
+}
+
+// Cut at the last word boundary inside the cap so the rendered OG card never
+// ends mid-word. Mirrors inject-seo-meta.mjs.
+function truncateAtWord(s, max) {
+ if (s.length <= max) return s;
+ const cut = s.slice(0, max);
+ const space = cut.lastIndexOf(' ');
+ return (space > max * 0.6 ? cut.slice(0, space) : cut).replace(/[\s,;:]+$/, '');
+}
+
+function pageOgUrl(page, sectionId) {
+ const q = new URLSearchParams();
+ q.set('s', sectionId || 'main');
+ q.set('t', page.title || 'three.ws');
+ if (page.description) q.set('d', truncateAtWord(page.description, 160));
+ q.set('p', page.path || '/');
+ return `${ORIGIN}/api/page-og?${q.toString()}`;
+}
+
+// path -> { page, sectionId } for every public, indexable catalogued page.
+let catalog = null;
+function getCatalog() {
+ if (catalog) return catalog;
+ catalog = new Map();
+ try {
+ const data = JSON.parse(
+ readFileSync(path.resolve(__dirname, '..', 'data', 'pages.json'), 'utf8'),
+ );
+ for (const s of data.sections || []) {
+ for (const p of s.pages || []) {
+ if (!p.path || p.path.startsWith('http')) continue;
+ if (p.indexable === false || p.auth === 'required') continue;
+ catalog.set(p.path, { page: p, sectionId: s.id });
+ }
+ }
+ } catch (err) {
+ console.error('[seo-head] failed to load data/pages.json:', err.message);
+ }
+ return catalog;
+}
+
+function normalizePath(pathname) {
+ if (!pathname || pathname === '/') return '/';
+ const bare = pathname.replace(/\/+$/, '');
+ return bare || '/';
+}
+
+export function hasSeoRoute(pathname) {
+ return getCatalog().has(normalizePath(pathname));
+}
+
+// Extract the href of an existing <link rel="canonical"> from a head string,
+// attribute order agnostic. Returns null when the head has none.
+function canonicalOf(head) {
+ const tag = head.match(/<link[^>]*rel=["']canonical["'][^>]*>/i);
+ if (!tag) return null;
+ const href = tag[0].match(/href=["']([^"']+)["']/i);
+ return href ? href[1] : null;
+}
+
+// Replace a whole tag matched by `re` with `replacement`, or record it as
+// missing so it can be appended before </head>. Rebuilding the entire tag
+// keeps the rewrite independent of the shell's attribute order.
+function setTag(head, re, replacement, missing) {
+ if (re.test(head)) return head.replace(re, () => replacement);
+ missing.push(replacement);
+ return head;
+}
+
+function buildJsonLd(page, url, ogImage) {
+ const graph = [
+ {
+ '@type': 'WebPage',
+ name: page.title,
+ description: page.description || undefined,
+ url,
+ isPartOf: { '@type': 'WebSite', name: 'three.ws', url: ORIGIN },
+ primaryImageOfPage: ogImage,
+ },
+ ];
+ const segs = page.path.split('/').filter(Boolean);
+ if (segs.length > 1) {
+ const items = [{ name: 'Home', url: `${ORIGIN}/` }];
+ let acc = '';
+ for (const s of segs) {
+ acc += `/${s}`;
+ items.push({
+ name: s.replace(/[-_]/g, ' ').replace(/\b\w/g, (c) => c.toUpperCase()),
+ url: `${ORIGIN}${acc}`,
+ });
+ }
+ graph.push({
+ '@type': 'BreadcrumbList',
+ itemListElement: items.map((it, i) => ({
+ '@type': 'ListItem',
+ position: i + 1,
+ name: it.name,
+ item: it.url,
+ })),
+ });
+ }
+ const ld = { '@context': 'https://schema.org', '@graph': graph };
+ return `<script type="application/ld+json">${JSON.stringify(ld).replace(/</g, '\\u003c')}</script>`;
+}
+
+/**
+ * Rewrite a shared shell's <head> for the page actually being served, or
+ * return null when the shell already carries this page's meta (or the path is
+ * not a catalogued public page).
+ *
+ * @param {string} pathname Original request path (before any dest rewrite).
+ * @param {string} html The shell HTML as served from dist/.
+ * @returns {string|null}
+ */
+export function rewriteHead(pathname, html) {
+ const p = normalizePath(pathname);
+ const entry = getCatalog().get(p);
+ if (!entry) return null;
+ const headMatch = html.match(/<head[^>]*>[\s\S]*?<\/head>/i);
+ if (!headMatch) return null;
+
+ const { page, sectionId } = entry;
+ const url = `${ORIGIN}${p}`;
+ const shellCanonical = canonicalOf(headMatch[0]);
+ // The canonical already names this page: it owns its meta. This is every
+ // statically stamped page, and the shell's own route (/docs on docs shell).
+ if (shellCanonical === url) return null;
+
+ let head = headMatch[0];
+ const fullTitle = `${page.title} · three.ws`;
+ const desc = page.description || '';
+ const ogImage = pageOgUrl(page, sectionId);
+ const missing = [];
+
+ head = setTag(head, /<title[^>]*>[\s\S]*?<\/title>/i, `<title>${htmlEscape(fullTitle)}</title>`, missing);
+ head = setTag(head, /<link[^>]*rel=["']canonical["'][^>]*>/i, `<link rel="canonical" href="${url}" />`, missing);
+ if (desc) {
+ head = setTag(head, /<meta[^>]*name=["']description["'][^>]*>/i, `<meta name="description" content="${htmlEscape(desc)}" />`, missing);
+ head = setTag(head, /<meta[^>]*property=["']og:description["'][^>]*>/i, `<meta property="og:description" content="${htmlEscape(desc)}" />`, missing);
+ head = setTag(head, /<meta[^>]*name=["']twitter:description["'][^>]*>/i, `<meta name="twitter:description" content="${htmlEscape(desc)}" />`, missing);
+ }
+ head = setTag(head, /<meta[^>]*property=["']og:title["'][^>]*>/i, `<meta property="og:title" content="${htmlEscape(fullTitle)}" />`, missing);
+ head = setTag(head, /<meta[^>]*property=["']og:url["'][^>]*>/i, `<meta property="og:url" content="${url}" />`, missing);
+ head = setTag(head, /<meta[^>]*property=["']og:image["'][^>]*>/i, `<meta property="og:image" content="${htmlEscape(ogImage)}" />`, missing);
+ head = setTag(head, /<meta[^>]*property=["']og:image:alt["'][^>]*>/i, `<meta property="og:image:alt" content="${htmlEscape(page.title)} on three.ws" />`, missing);
+ head = setTag(head, /<meta[^>]*name=["']twitter:title["'][^>]*>/i, `<meta name="twitter:title" content="${htmlEscape(fullTitle)}" />`, missing);
+ head = setTag(head, /<meta[^>]*name=["']twitter:image["'][^>]*>/i, `<meta name="twitter:image" content="${htmlEscape(ogImage)}" />`, missing);
+
+ // JSON-LD written for the shell's own route describes the wrong page here.
+ // Drop head blocks that reference the shell canonical, keep any others, and
+ // add a fresh WebPage + BreadcrumbList for this page.
+ if (shellCanonical) {
+ head = head.replace(/<script[^>]*application\/ld\+json[^>]*>[\s\S]*?<\/script>/gi, (m) =>
+ m.includes(shellCanonical) ? '' : m,
+ );
+ }
+ missing.push(buildJsonLd(page, url, ogImage));
+
+ if (missing.length) {
+ head = head.replace(/<\/head>/i, () => `${missing.map((t) => `\t${t}`).join('\n')}\n</head>`);
+ }
+
+ const start = headMatch.index;
+ return html.slice(0, start) + head + html.slice(start + headMatch[0].length);
+}
+
+// pathname -> rewritten HTML, or null when the shell should be served as-is.
+// dist/ never changes under a running deploy, so entries live forever.
+const cache = new Map();
+
+/**
+ * Cached rewrite for a request path, reading the shell from `file`. Returns
+ * null (serve the static file) on a cache-hit null, any read error, or a
+ * shell that already owns its meta.
+ */
+export function renderSeoHead(pathname, file) {
+ const key = normalizePath(pathname);
+ if (cache.has(key)) return cache.get(key);
+ let out = null;
+ try {
+ out = rewriteHead(key, readFileSync(file, 'utf8'));
+ } catch (err) {
+ console.error(`[seo-head] ${key} fell back to the static shell:`, err.message);
+ }
+ cache.set(key, out);
+ return out;
+}
+
+export const __test = { rewriteHead, hasSeoRoute, normalizePath, pageOgUrl, buildJsonLd };
diff --git a/tests/seo-head.test.js b/tests/seo-head.test.js
new file mode 100644
index 000000000..d3caae89b
--- /dev/null
+++ b/tests/seo-head.test.js
@@ -0,0 +1,89 @@
+// Per-route head rewriting for shared-shell pages (server/seo-head.mjs).
+//
+// The docs and tutorial shells serve hundreds of routes from one file, so the
+// rewriter must give each catalogued route its own canonical/OG/JSON-LD while
+// leaving pages that already own their meta untouched.
+
+import { describe, it, expect } from 'vitest';
+import { readFileSync } from 'node:fs';
+import path from 'node:path';
+import { fileURLToPath } from 'node:url';
+import { rewriteHead, hasSeoRoute } from '../server/seo-head.mjs';
+
+const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
+const docsShell = readFileSync(path.join(ROOT, 'docs/index.html'), 'utf8');
+const tutorialShell = readFileSync(path.join(ROOT, 'pages/tutorial.html'), 'utf8');
+const pagesJson = JSON.parse(readFileSync(path.join(ROOT, 'data/pages.json'), 'utf8'));
+
+const headOf = (html) => html.match(/<head[^>]*>[\s\S]*?<\/head>/i)[0];
+
+describe('rewriteHead', () => {
+ it('gives a /docs/* route its own canonical, title, OG and JSON-LD', () => {
+ const out = rewriteHead('/docs/swarms', docsShell);
+ expect(out).toBeTruthy();
+ const head = headOf(out);
+ expect(head).toContain('<link rel="canonical" href="https://three.ws/docs/swarms"');
+ expect(head).not.toContain('href="https://three.ws/docs"');
+ expect(head).toMatch(/<title>Docs · Trading swarms · three\.ws<\/title>/);
+ expect(head).toContain('<meta property="og:url" content="https://three.ws/docs/swarms"');
+ expect(head).toContain('p=%2Fdocs%2Fswarms');
+ expect(head).toContain('BreadcrumbList');
+ expect(head).toContain('"url":"https://three.ws/docs/swarms"');
+ // Body untouched.
+ expect(out.slice(out.indexOf('</head>'))).toBe(docsShell.slice(docsShell.indexOf('</head>')));
+ });
+
+ it('leaves the shell route itself alone (canonical already matches)', () => {
+ expect(rewriteHead('/docs', docsShell)).toBeNull();
+ expect(rewriteHead('/docs/', docsShell)).toBeNull();
+ });
+
+ it('normalizes trailing slashes before lookup', () => {
+ const out = rewriteHead('/docs/swarms/', docsShell);
+ expect(out).toBeTruthy();
+ expect(headOf(out)).toContain('href="https://three.ws/docs/swarms"');
+ });
+
+ it('rewrites the tutorial shell and drops the stale JSON-LD', () => {
+ const slug = pagesJson.sections
+ .flatMap((s) => s.pages)
+ .find((p) => /^\/tutorials\/[a-z0-9-]+$/.test(p.path) && p.path !== '/tutorials/text-to-3d');
+ expect(slug).toBeTruthy();
+ const out = rewriteHead(slug.path, tutorialShell);
+ expect(out).toBeTruthy();
+ const head = headOf(out);
+ expect(head).toContain(`<link rel="canonical" href="https://three.ws${slug.path}"`);
+ expect(head).not.toContain('canonical" href="https://three.ws/tutorials/text-to-3d"');
+ // Any remaining head JSON-LD must not describe the shell's route.
+ for (const m of head.matchAll(/<script[^>]*ld\+json[^>]*>([\s\S]*?)<\/script>/gi)) {
+ expect(m[1]).not.toContain('https://three.ws/tutorials/text-to-3d');
+ }
+ expect(head).toContain(`"url":"https://three.ws${slug.path}"`);
+ });
+
+ it('returns null for unknown and non-public paths', () => {
+ expect(rewriteHead('/definitely-not-a-page-xyz', docsShell)).toBeNull();
+ const gated = pagesJson.sections
+ .flatMap((s) => s.pages)
+ .find((p) => p.auth === 'required' || p.indexable === false);
+ if (gated) expect(rewriteHead(gated.path, docsShell)).toBeNull();
+ });
+
+ it('escapes catalog copy into attribute values', () => {
+ const withAmp = pagesJson.sections
+ .flatMap((s) => s.pages)
+ .find((p) => p.path.startsWith('/docs/') && /[&"]/.test(p.description || ''));
+ if (!withAmp) return;
+ const head = headOf(rewriteHead(withAmp.path, docsShell));
+ const desc = head.match(/<meta name="description" content="([^"]*)"/)[1];
+ expect(desc).not.toMatch(/&(?!amp;|lt;|gt;|quot;|#)/);
+ });
+});
+
+describe('hasSeoRoute', () => {
+ it('knows catalogued routes, with and without trailing slash', () => {
+ expect(hasSeoRoute('/docs/swarms')).toBe(true);
+ expect(hasSeoRoute('/docs/swarms/')).toBe(true);
+ expect(hasSeoRoute('/definitely-not-a-page-xyz')).toBe(false);
+ });
+});