@@ -3,20 +3,23 @@ import type { Context } from "hono";
33import { eq } from "drizzle-orm" ;
44import { z } from "zod" ;
55
6- import { defineCacheEntry } from "@/builders" ;
7- import { zodRichTextDocument } from "@/content/rich-text/document" ;
86import { core_site_status } from "@/database/site" ;
7+ import { manifest } from "@/manifest" ;
98
109import type { DomainCache } from "./cache" ;
1110import type { SiteOfflineMessage , SiteStatusUpdate } from "./site-status" ;
1211
12+ import { defineCacheEntry } from "./cache-entry" ;
1313import {
1414 checkStaffPermissionOfUser ,
1515 getStaffFlags ,
1616} from "./check-staff-permission" ;
1717import { coreStaffPermissions } from "./core-staff-permissions" ;
1818import { invalidatePageCacheTags } from "./page-cache" ;
19- import { SITE_STATUS_PAGE_CACHE_TAG } from "./site-status" ;
19+ import {
20+ SITE_STATUS_PAGE_CACHE_TAG ,
21+ zodSiteOfflineMessage ,
22+ } from "./site-status" ;
2023import { staffPermissionArgs } from "./staff-permissions" ;
2124
2225/** The stored row, as every request reads it. */
@@ -38,34 +41,22 @@ const ONLINE: StoredSiteStatus = {
3841 showCountdown : true ,
3942} ;
4043
41- /**
42- * Every request reads it, so it is cached. A save deletes the entry; the TTL
43- * only bounds how long another instance without a shared store lags behind.
44- */
4544const siteStatusCache = defineCacheEntry ( {
45+ pluginId : manifest . id ,
4646 name : "site.status" ,
4747 version : 1 ,
4848 schema : z . object ( {
4949 changedAt : z . string ( ) . nullable ( ) ,
5050 changedById : z . number ( ) . int ( ) . nullable ( ) ,
51- /** JSON of the message: rich-text attrs are not typed as JSON. */
52- message : z . string ( ) ,
51+ messageJson : z . string ( ) ,
5352 offline : z . boolean ( ) ,
5453 opensAt : z . string ( ) . nullable ( ) ,
5554 showCountdown : z . boolean ( ) ,
5655 } ) ,
5756 ttlSeconds : 300 ,
5857} ) ;
5958
60- const zodMessage = z . array (
61- z . object ( { languageCode : z . string ( ) , value : zodRichTextDocument } ) ,
62- ) ;
63-
64- const parseMessage = ( json : string ) : SiteOfflineMessage =>
65- zodMessage . parse ( JSON . parse ( json ) ) ;
66-
67- /** Test apps built without the global middleware have no cache. */
68- const cacheOf = ( c : Context ) => c . get ( "cache" ) as DomainCache | undefined ;
59+ const cacheOf = ( c : Context ) : DomainCache | undefined => c . get ( "cache" ) ;
6960
7061const perRequest = new WeakMap < Context , Promise < StoredSiteStatus > > ( ) ;
7162
@@ -101,11 +92,14 @@ export const loadSiteStatus = async (c: Context): Promise<StoredSiteStatus> => {
10192 ? cache
10293 . entry ( siteStatusCache )
10394 . remember ( async ( ) => {
104- const row = await readRow ( c ) ;
95+ const { message , ... row } = await readRow ( c ) ;
10596
106- return { ...row , message : JSON . stringify ( row . message ) } ;
97+ return { ...row , messageJson : JSON . stringify ( message ) } ;
10798 } )
108- . then ( cached => ( { ...cached , message : parseMessage ( cached . message ) } ) )
99+ . then ( ( { messageJson, ...cached } ) => ( {
100+ ...cached ,
101+ message : zodSiteOfflineMessage . parse ( JSON . parse ( messageJson ) ) ,
102+ } ) )
109103 : readRow ( c ) ;
110104 perRequest . set ( c , loading ) ;
111105
@@ -136,8 +130,8 @@ export const canBypassSiteOffline = async (c: Context): Promise<boolean> => {
136130} ;
137131
138132/**
139- * Writes the site status and returns the row before the write (`null` when
140- * there was none). Run it inside the caller's transaction, then call
133+ * Writes the site status. Returns the row before the write (`null` when there
134+ * was none) and whether this save flipped `offline` . Run it inside the caller's transaction, then call
141135 * {@link afterSiteStatusSaved} once it has committed. Server-only.
142136 */
143137export const saveSiteStatus = async (
0 commit comments