-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema-ui-ssr-handler.js
More file actions
149 lines (123 loc) · 4.26 KB
/
Copy pathschema-ui-ssr-handler.js
File metadata and controls
149 lines (123 loc) · 4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import {
renderCollectionListPage,
renderHomePage,
renderRecordDetailPage,
} from './cms-ssr.js';
import { readManifest, renderSchemaUiHtml } from './render-admin.js';
/**
* Handles Schema UI SSR routes (`/`, `/templates`, `/cms/...`).
* Returns true when the response was fully handled (including SSR 404s).
*
* @param {import('node:http').IncomingMessage} request
* @param {import('node:http').ServerResponse} response
* @param {{ cwd: string; db: object; basePath?: string; manifestUrl: URL }} context
*/
export async function handleSchemaUiSsrRequest(request, response, context) {
if (request.method !== 'GET') {
return false;
}
const host = request.headers.host ?? 'localhost';
const url = new URL(request.url ?? '/', `http://${host}`);
const basePath = normalizeBasePath(context.basePath);
const route = parsePath(url.pathname, basePath);
if (!route) {
return false;
}
try {
const { db, manifestUrl } = context;
const manifest = await readManifest(manifestUrl);
if (route.type === 'templates') {
const html = renderSchemaUiHtml(manifest);
sendHtml(response, html);
return true;
}
const recordsByCollection = await loadRecordsByCollection(db, manifest);
if (route.type === 'home') {
sendHtml(response, renderHomePage(manifest, recordsByCollection, { basePath }));
return true;
}
if (route.type === 'list') {
const html = renderCollectionListPage(manifest, route.collection, recordsByCollection[route.collection] ?? [], { basePath });
if (!html) {
response.writeHead(404, { 'content-type': 'text/html; charset=utf-8' });
response.end('<!doctype html><meta charset="utf-8"><title>404</title><p>Unknown collection</p>');
return true;
}
sendHtml(response, html);
return true;
}
const record = await db.collection(route.collection).get(route.id);
const html = renderRecordDetailPage(manifest, route.collection, record, recordsByCollection, { basePath });
if (!html) {
response.writeHead(404, { 'content-type': 'text/html; charset=utf-8' });
response.end('<!doctype html><meta charset="utf-8"><title>404</title><p>Record not found</p>');
return true;
}
sendHtml(response, html);
return true;
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
response.writeHead(500, { 'content-type': 'text/plain; charset=utf-8' });
response.end(message);
return true;
}
}
function sendHtml(response, html) {
response.writeHead(200, {
'content-type': 'text/html; charset=utf-8',
'cache-control': 'no-store',
});
response.end(html);
}
function parsePath(pathname, basePath = '') {
const strippedPathname = stripBasePath(pathname, basePath);
if (strippedPathname === null) {
return null;
}
const normalized = strippedPathname === '' ? '/' : strippedPathname;
if (normalized === '/' || normalized === '/index.html') {
return { type: 'home' };
}
if (normalized === '/templates') {
return { type: 'templates' };
}
const segments = normalized.replace(/^\/+|\/+$/gu, '').split('/').filter(Boolean);
if (segments[0] === 'cms' && segments.length === 2) {
return { type: 'list', collection: segments[1] };
}
if (segments[0] === 'cms' && segments.length === 3) {
return { type: 'detail', collection: segments[1], id: decodeURIComponent(segments[2]) };
}
return null;
}
function stripBasePath(pathname, basePath) {
const normalizedBasePath = normalizeBasePath(basePath);
if (!normalizedBasePath) {
return pathname;
}
if (pathname === normalizedBasePath) {
return '/';
}
if (pathname.startsWith(`${normalizedBasePath}/`)) {
return pathname.slice(normalizedBasePath.length) || '/';
}
return null;
}
function normalizeBasePath(basePath) {
if (!basePath || basePath === '/') {
return '';
}
return `/${String(basePath).replace(/^\/+|\/+$/gu, '')}`;
}
async function loadRecordsByCollection(db, manifest) {
/** @type {Record<string, unknown[]>} */
const out = {};
for (const name of Object.keys(manifest.collections ?? {})) {
const meta = manifest.collections[name];
if (meta?.kind !== 'collection') {
continue;
}
out[name] = await db.collection(name).all();
}
return out;
}