fix: IndexNow hears of deleted and edited records

RecordDeleted marks the listed rows it excludes and RecordSaved the rows
whose lastmod it moves (indexnow_pending_at); the next rebuild sends
them to IndexNow as removed or changed and clears the marks. RecordSaved
checks for a nil event before reading it. TestIndexNow now deletes and
edits through the record events, as the admin does.
This commit is contained in:
Jakub Zych
2026-11-04 23:02:09 +01:00
parent 6b67ab759b
commit f58d55555c
8 changed files with 147 additions and 12 deletions

View File

@@ -119,7 +119,9 @@ type collected struct {
// leaves its rows in the table with a reason, as a hole; only a changed
// file size renumbers the files (D-30). In production, with IndexNow on,
// the added, changed and removed addresses are queued for IndexNow after
// the commit (NotifyIndexNow).
// the commit (NotifyIndexNow), with the rows the record listeners marked
// (indexnow_pending_at): a deleted record's addresses as removed, an
// edited record's listed addresses as changed.
func Rebuild(ctx context.Context, app *backpack.App) (RebuildResult, error) {
var res RebuildResult
if app == nil {
@@ -424,6 +426,11 @@ func apply(tx *gorm.DB, found collected, settings models.Settings, site string)
dirty := map[uint64]bool{}
var created []*models.URL
matched := map[slotKey]bool{}
// pending are the rows the record listeners marked for IndexNow
// (P33-62), with the mark this rebuild read; notified the addresses
// already in Added or Changed.
pending := map[uint64]time.Time{}
notified := map[string]bool{}
for _, w := range found.rows {
matched[w.key] = true
r, ok := byKey[w.key]
@@ -476,10 +483,21 @@ func apply(tx *gorm.DB, found collected, settings models.Settings, site string)
switch {
case listed && !wasListed:
res.Added = append(res.Added, Absolute(site, r.URL))
notified[r.URL] = true
case !listed && wasListed:
res.Removed = append(res.Removed, Absolute(site, before.URL))
case listed && before.URL != r.URL:
res.Changed = append(res.Changed, Absolute(site, r.URL))
notified[r.URL] = true
}
if r.IndexNowPendingAt != nil {
pending[r.ID] = *r.IndexNowPendingAt
if listed && !notified[r.URL] {
// A content edit that kept the address (RecordSaved
// moved its lastmod).
res.Changed = append(res.Changed, Absolute(site, r.URL))
notified[r.URL] = true
}
}
if rowChanged(before, *r) {
dirty[r.ID] = true
@@ -495,6 +513,14 @@ func apply(tx *gorm.DB, found collected, settings models.Settings, site string)
if found.unpublished[slotKey{r.TypeKey, r.RecordID, ""}] {
reason = models.ReasonUnpublished
}
if r.IndexNowPendingAt != nil {
// RecordDeleted already excluded the row; it was listed when
// it marked it, so IndexNow hears of it as removed.
pending[r.ID] = *r.IndexNowPendingAt
if r.ExcludedReason != "" {
res.Removed = append(res.Removed, Absolute(site, r.URL))
}
}
if r.ExcludedReason == reason {
continue
}
@@ -574,6 +600,13 @@ func apply(tx *gorm.DB, found collected, settings models.Settings, site string)
return res, err
}
}
// The IndexNow marks this rebuild read are cleared; a mark set after
// the read (a later save) survives for the next rebuild.
for id, at := range pending {
if err := tx.Exec(`UPDATE golem15_sitemap_urls SET indexnow_pending_at = NULL WHERE id = ? AND indexnow_pending_at <= ?`, id, at).Error; err != nil {
return res, err
}
}
if err := tx.Exec(`UPDATE golem15_sitemap_settings SET numbered_size = ?, built_at = ? WHERE id = 1`, size, now).Error; err != nil {
return res, err
}