Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: [POC] send webhooks when nodes are purged (i.e. Next on-demand revalidation support) #287

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

jasonbahl
Copy link
Collaborator

This is a proof-of-concept for sending webhooks in response to purge actions tracked by WPGraphQL Smart Cache.

Why?

This can help facilitate remote sources reacting to changes in the CMS. Some common applications:

  • NextJS On Demand Revalidation
  • Sending payloads to logging services for debugging, etc
  • Notification systems (i.e. Slack notifications, etc)

Why not use another webhook plugin?

WPGraphQL Smart Cache already handles a lot of the logic required to determine whether a node is public or private, etc and centralizes a lot of mechanisms. By piggy-packing on the existing functionality we can provide a lot of value with minimal code.

How it works:

  • WPGraphQL Smart Cache tracks events (i.e. post updated, post deleted)
  • WPGraphQL Smart Cache emits a purge event as do_action( 'graphql_purge', $key, $event, $host );
  • When the graphql_purge event is called, a $webhook_configs array is iterated over to determine which endpoint(s) to send webhook(s) to
  • Each endpoint configured is sent a webhook as a POST or GET request with a payload (either POST body or GET query params)
  • The payload and destination can be configured / filtered to meet various needs, but the default payload at the moment looks like:

GET:

{
"url": "http://localhost:3000/api/revalidate/?key=dGVybToxMQ=&event=term_saved&hostname=uri-debugging.local/graphql&path=/tag/tag-1/",
"method": "GET",
"headers": [],
"body": null,
"blocking": false
}

POST

{
"url": "http://localhost:3000/api/revalidate/",
"method": "POST",
"headers": {
"Content-Type": "application/json"
},
"body": "{\"key\":\"dGVybToxMQ==\",\"event\":\"term_saved\",\"hostname\":\"uri-debugging.local\\/graphql\",\"path\":\"\\/tag\\/tag-1\\/\"}",
"blocking": false
}

- add `get_path_from_key` function to abstract some of the logic around purge event keys and determining paths
@wp-graphql wp-graphql deleted a comment from gitguardian bot Jul 12, 2024
@jasonbahl jasonbahl marked this pull request as draft July 18, 2024 17:23
@jasonbahl
Copy link
Collaborator Author

I converted this PR to a draft as it's a Proof of Concept more than production ready. If we want to work on this and make it production ready we'll need to scope it out and figure out what all is required for this.

@josephfusco josephfusco changed the title feat: send webhooks when nodes are purged (i.e. Next on-demand revalidation support) feat: [POC] send webhooks when nodes are purged (i.e. Next on-demand revalidation support) Aug 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant