Skip to content

Commit

Permalink
Only process site.changed webhook if data actually changed
Browse files Browse the repository at this point in the history
refs [AP-346](https://linear.app/tryghost/issue/AP-346/only-process-sitechanged-webhook-if-data-actually-changed)

The `site.changed` webhook can sometimes be triggered by Ghost when no settings
have actually changed (i.e in the case of post publish / unpublish). To prevent
unnecessary processing, we should only process the webhook if the data we care
about has actually changed
  • Loading branch information
mike182uk committed Aug 14, 2024
1 parent 62aef77 commit 4b63802
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions src/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,14 +166,33 @@ export async function siteChangedWebhook(
try {
// Retrieve site settings from Ghost
const host = ctx.req.header('host') || '';

const settings = await getSiteSettings(host);

// Update the database
// Retrieve the actor and check if anything has changed
const handle = ACTOR_DEFAULT_HANDLE;
const db = ctx.get('db');

const current = await db.get<PersonData>(['handle', handle]);

if (
current &&
current.icon === settings.site.icon &&
current.name === settings.site.title &&
current.summary === settings.site.description
) {
console.log('No site settings changed, nothing to do');

return new Response(JSON.stringify({}), {
headers: {
'Content-Type': 'application/activity+json',
},
status: 200,
});
}

console.log('Site settings changed, will notify followers');

// Update the database if the site settings have changed
const updated = {
...current,
icon: settings.site.icon,
Expand All @@ -183,7 +202,7 @@ export async function siteChangedWebhook(

await db.set(['handle', handle], updated);

// Publish activity
// Publish activity if the site settings have changed
const apCtx = fedify.createContext(ctx.req.raw as Request, {
db,
globaldb: ctx.get('globaldb'),
Expand Down

0 comments on commit 4b63802

Please sign in to comment.