|
3 | 3 |
|
4 | 4 | require_once __DIR__ . '/../src/db.php'; |
5 | 5 | require_once __DIR__ . '/../src/http.php'; |
6 | | -require_once __DIR__ . '/../src/content.php'; |
7 | 6 |
|
8 | 7 | /** |
9 | | - * Page content, for the public website's build. |
| 8 | + * Page content overrides, for the public website's build. |
10 | 9 | * |
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. |
16 | 15 | * |
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. |
19 | 21 | * |
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. |
23 | 25 | */ |
24 | 26 |
|
25 | 27 | // ---- CORS: the same allowlist the other public endpoints use ---- |
|
44 | 46 | json_error('Only GET is supported here.', 405); |
45 | 47 | } |
46 | 48 |
|
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. |
50 | 51 | header('Cache-Control: public, max-age=60'); |
51 | 52 |
|
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