fix(wiki): render the public pages on the server
The documentation was assembled in the browser, so a client that runs no script saw an empty shell — including whoever fetches the terms and privacy links before approving an application, which is exactly what those pages are for. The wiki is now delivered as finished HTML, carries no script at all, and declares itself indexable, unlike the panel. Search works through a plain query string, an unknown page answers 404 rather than a blank page, and both legal pages are linked from the footer of every page. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
3d0e21d7cb
commit
8869c74b7b
7 files changed
+348
-222
No files matched your search
+73
-10
@@ -9,13 +9,71 @@ import { registerApiRoutes } from './api/index.js';
|
||||
import { authenticate, assertSameOrigin } from './auth/session.js';
|
||||
import { authorize } from './auth/guard.js';
|
||||
import { broadcast, closeEventStreams, topicForPath } from './events.js';
|
||||
import { HttpError, readJsonBody, readRawBody, sendEmpty, sendJson, sendStatic } from './http.js';
|
||||
import { HttpError, readJsonBody, readRawBody, sendEmpty, sendHtml, sendJson, sendStatic } from './http.js';
|
||||
import { createRouter } from './router.js';
|
||||
import { listPublished } from '../database/repositories/wiki-repository.js';
|
||||
import { pagePath } from './services/wiki-seed.js';
|
||||
import { renderIndex, renderNotFound, renderPage } from './services/wiki-render.js';
|
||||
import { ROOT_PAGES, seedWiki } from './services/wiki-seed.js';
|
||||
|
||||
/** HTTP server of the dashboard (M16-T01). */
|
||||
|
||||
const log = createLogger('web');
|
||||
|
||||
/**
|
||||
* The wiki address a path stands for: `''` for the index, a slug for a page,
|
||||
* `null` when the path is not the wiki's business.
|
||||
*/
|
||||
function wikiSlug(pathname) {
|
||||
const clean = pathname.replace(/\/+$/, '');
|
||||
if (clean === '/wiki' || clean === '') return clean === '/wiki' ? '' : null;
|
||||
if (clean.startsWith('/wiki/')) return decodeURIComponent(clean.slice('/wiki/'.length));
|
||||
|
||||
const root = clean.replace(/^\//, '');
|
||||
return ROOT_PAGES.includes(root) ? root : null;
|
||||
}
|
||||
|
||||
/** Answers with the finished HTML of a wiki page, or its index. */
|
||||
function sendWiki(response, slug, term) {
|
||||
let pages = [];
|
||||
try {
|
||||
pages = listPublished();
|
||||
} catch (error) {
|
||||
log.warn('Could not read the wiki pages', error);
|
||||
}
|
||||
|
||||
const categories = [];
|
||||
for (const page of pages) {
|
||||
const entry = { ...page, path: pagePath(page.slug) };
|
||||
let category = categories.find((item) => item.name === page.category);
|
||||
if (!category) categories.push((category = { name: page.category, pages: [] }));
|
||||
category.pages.push(entry);
|
||||
}
|
||||
const all = categories.flatMap((category) => category.pages);
|
||||
|
||||
if (slug === '') {
|
||||
const needle = term.trim().toLowerCase();
|
||||
const results =
|
||||
needle.length >= 2
|
||||
? all.filter((page) =>
|
||||
[page.title, page.summary, page.content].some((field) =>
|
||||
String(field ?? '')
|
||||
.toLowerCase()
|
||||
.includes(needle),
|
||||
),
|
||||
)
|
||||
: null;
|
||||
sendHtml(response, 200, renderIndex(categories, { term, results }));
|
||||
return;
|
||||
}
|
||||
|
||||
const page = all.find((entry) => entry.slug === slug);
|
||||
if (!page) {
|
||||
sendHtml(response, 404, renderNotFound(categories));
|
||||
return;
|
||||
}
|
||||
sendHtml(response, 200, renderPage(page, categories));
|
||||
}
|
||||
const publicDir = path.join(path.dirname(fileURLToPath(import.meta.url)), 'public');
|
||||
|
||||
/** Sliding window per client address, reset every minute. */
|
||||
@@ -72,15 +130,20 @@ export function createHandler({ client = null, setupOnly = false, startedAt = Da
|
||||
|
||||
const isApi = pathname.startsWith('/api/') || pathname.startsWith('/auth/');
|
||||
if (!isApi) {
|
||||
// The wiki is a separate public page: it must never fall back to the
|
||||
// dashboard shell, which would send a visitor to a login screen. The
|
||||
// legal pages answer at the root, where platforms expect them.
|
||||
const isWiki =
|
||||
pathname === '/wiki' ||
|
||||
pathname.startsWith('/wiki/') ||
|
||||
ROOT_PAGES.includes(pathname.replace(/^\/|\/$/g, ''));
|
||||
const fallback = isWiki ? 'wiki.html' : 'index.html';
|
||||
const served = await sendStatic(response, publicDir, pathname, { fallback, request });
|
||||
// The wiki is rendered here rather than in the browser: a terms page
|
||||
// that needs scripts to show its text reads as blank to anything that
|
||||
// does not run them, which includes whoever checks the link before
|
||||
// approving an application.
|
||||
const slug = wikiSlug(pathname);
|
||||
if (slug !== null) {
|
||||
sendWiki(response, slug, url.searchParams.get('q') ?? '');
|
||||
return;
|
||||
}
|
||||
|
||||
const served = await sendStatic(response, publicDir, pathname, {
|
||||
fallback: 'index.html',
|
||||
request,
|
||||
});
|
||||
if (!served) sendEmpty(response, 404);
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user