fix: purge the host site cache on Journal API post writes

Admin saves purged the site cache; the token API's create, update,
delete and featured-image writes did not, so API-published posts waited
for the site's cache TTL.
This commit is contained in:
Jakub Zych
2026-10-18 19:37:12 +02:00
parent 8de4928b80
commit 05e38aee5d
3 changed files with 50 additions and 3 deletions

View File

@@ -13,6 +13,7 @@ import (
"git.golem15.com/golem15/summercms/modules/backpack"
"git.golem15.com/golem15/summercms/modules/bouncer"
"git.golem15.com/golem15/summercms/modules/cabana"
"git.golem15.com/golem15/summercms/modules/cooler"
"git.golem15.com/golem15/summercms/modules/lagoon"
"git.golem15.com/golem15/summercms/modules/lagoon/attach"
"git.golem15.com/golem15/summercms/modules/wire"
@@ -230,6 +231,7 @@ func Store(app *backpack.App) http.HandlerFunc {
writeAPIError(w, http.StatusInternalServerError, "Internal server error")
return
}
purgeSiteCache(r, app, post)
wire.WriteJSON(w, http.StatusCreated, map[string]any{"data": serializePost(post, db.WithContext(r.Context()))})
}
}
@@ -283,6 +285,7 @@ func Update(app *backpack.App) http.HandlerFunc {
writeAPIError(w, http.StatusInternalServerError, "Internal server error")
return
}
purgeSiteCache(r, app, post)
wire.WriteJSON(w, http.StatusOK, map[string]any{"data": serializePost(post, db.WithContext(r.Context()))})
}
}
@@ -315,6 +318,7 @@ func Destroy(app *backpack.App) http.HandlerFunc {
writeAPIError(w, http.StatusInternalServerError, "Internal server error")
return
}
purgeSiteCache(r, app, post)
wire.WriteJSON(w, http.StatusOK, map[string]any{"message": msgPostDeleted})
}
}
@@ -376,6 +380,7 @@ func UploadFeaturedImage(app *backpack.App) http.HandlerFunc {
stored.AttachmentType = post.MorphName()
stored.AttachmentID = idStr
stored.Field = "featured_images"
purgeSiteCache(r, app, post)
wire.WriteJSON(w, http.StatusCreated, map[string]any{
"data": map[string]any{
"id": stored.ID,
@@ -387,6 +392,14 @@ func UploadFeaturedImage(app *backpack.App) http.HandlerFunc {
}
}
// purgeSiteCache queues the host site's cache purge for post, as the admin
// save hooks do. The API writes run outside a transaction, so the purge is
// queued at once and never delays the response. Without site_cache.purge_url
// it does nothing.
func purgeSiteCache(r *http.Request, app *backpack.App, post *models.Post) {
cooler.PurgeSiteCacheAfterCommit(r.Context(), nil, cooler.SiteCacheFromApp(app), "post", post.Slug)
}
// DeleteFeaturedImage is DELETE /_journal/api/v1/posts/{id}/featured-images/{fileId}.
func DeleteFeaturedImage(app *backpack.App) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
@@ -430,6 +443,7 @@ func DeleteFeaturedImage(app *backpack.App) http.HandlerFunc {
if bucket, ok := app.Lookup[*blob.Bucket](); ok && bucket != nil {
_ = attach.DeleteKeys(r.Context(), bucket, keys)
}
purgeSiteCache(r, app, post)
wire.WriteJSON(w, http.StatusOK, map[string]any{"message": msgImageRemoved})
}
}