Skip to content

Commit 89ef7d6

Browse files
Merge pull request #23 from niteeshkanna-sh/claude/stoic-rubin-fnglq7
Let the content endpoint stand on files the server already has
2 parents a351737 + 3b9d203 commit 89ef7d6

2 files changed

Lines changed: 51 additions & 18 deletions

File tree

‎my-app/scripts/fetch-content.mjs‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,19 @@ if (process.env.CONTENT_API !== 'off') {
101101
if (!res.ok) throw new Error(`HTTP ${res.status}`);
102102

103103
const body = await res.json();
104-
if (!body?.ok || !body?.content) throw new Error('unexpected response shape');
104+
if (!body?.ok) throw new Error('unexpected response shape');
105105

106-
content = merge(defaults, body.content);
106+
// The panel sends only what has been edited; the defaults are already
107+
// here. It used to send both, which is the only thing that made the
108+
// endpoint depend on files a hand-updated panel does not have.
109+
content = merge(defaults, body.overrides ?? {});
110+
111+
if (body.ready === false) {
112+
console.warn(
113+
'fetch-content: the panel has no content table yet, so nothing can be edited.\n' +
114+
' Open Website content in the admin and press "Set up content storage".',
115+
);
116+
}
107117
const edited = Object.entries(content).flatMap(([page, sections]) =>
108118
Object.keys(sections).filter(
109119
(s) => JSON.stringify(sections[s]) !== JSON.stringify(defaults[page][s]),

‎public_html/admin.niteshacars.in/admin/api/public-content.php‎

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,25 @@
33

44
require_once __DIR__ . '/../src/db.php';
55
require_once __DIR__ . '/../src/http.php';
6-
require_once __DIR__ . '/../src/content.php';
76

87
/**
9-
* Page content, for the public website's build.
8+
* Page content overrides, for the public website's build.
109
*
11-
* Read by my-app's build rather than by a visitor's browser, and that is the
12-
* whole point. Content fetched at page load would sit behind JavaScript, and
13-
* the site's ranking rests on the prerendered HTML actually containing the
14-
* words. Baking it in at build time keeps the copy in the HTML, keeps the
15-
* site fast, and keeps it up when this server is not.
10+
* Returns only what has been edited. The site ships every section's default
11+
* copy in my-app/src/content/defaults.json and lays these on top, so there is
12+
* no reason to send the defaults back to a caller that already has them --
13+
* and doing so was the only thing that made this endpoint depend on
14+
* src/content.php and content-defaults.json.
1615
*
17-
* Returns defaults with any admin edits laid over them, so the caller gets a
18-
* complete document either way and never has to merge anything itself.
16+
* That dependency mattered more than it looked. Those two files are new, so a
17+
* panel updated by hand has this endpoint without them, and it fails on the
18+
* require rather than doing anything useful. Reading the table directly keeps
19+
* this file standing on db.php and http.php alone, both of which have been on
20+
* the server since the panel was installed.
1921
*
20-
* Nothing here is secret -- it is the text of a public web page -- but it is
21-
* still read-only, GET-only and behind the same origin allowlist as the other
22-
* public endpoints.
22+
* Read by the build rather than by a visitor's browser: content fetched at
23+
* page load would sit behind JavaScript, and the site's ranking rests on the
24+
* prerendered HTML carrying the words.
2325
*/
2426

2527
// ---- CORS: the same allowlist the other public endpoints use ----
@@ -44,9 +46,30 @@
4446
json_error('Only GET is supported here.', 405);
4547
}
4648

47-
// Shorter than the fleet's cache. A build runs rarely, and when someone has
48-
// just pressed Publish they should not be served a stale copy of their own
49-
// edit.
49+
// A build runs rarely, and someone who has just pressed Save should not be
50+
// served a stale copy of their own edit.
5051
header('Cache-Control: public, max-age=60');
5152

52-
json_out(['ok' => true, 'content' => content_all()]);
53+
// The table only exists once 003_content.sql has run. A panel that has not
54+
// been migrated yet has nothing overridden, which is exactly what an empty
55+
// result means -- so say so plainly rather than returning a 500 and making
56+
// the site build fall back for the wrong reason.
57+
$overrides = [];
58+
$ready = true;
59+
60+
try {
61+
foreach (fetch_all('SELECT page, section, data FROM content_sections') as $row) {
62+
$decoded = json_decode((string) $row['data'], true);
63+
if (is_array($decoded)) {
64+
$overrides[$row['page']][$row['section']] = $decoded;
65+
}
66+
}
67+
} catch (Throwable $e) {
68+
$ready = false;
69+
}
70+
71+
json_out([
72+
'ok' => true,
73+
'ready' => $ready,
74+
'overrides' => $overrides === [] ? (object) [] : $overrides,
75+
]);

0 commit comments

Comments
 (0)