Sitecore Publishing Web Hook – Sitecore Rendering Host output caching PoC update

Sitecore 10.1 released a few weeks ago and one of the new features is the Publishing Web Hook. The main reason why this is introduced is to able to easily update JAMstack apps on publish. Official step-by-step guide for Vercel deployment can be found on the official Sitecore JSS documentation site, here.

However it brings other use-cases where using this new web hook makes our developer life easier. A few months ago I have posted about Sitecore ASP .NET Core SDK output caching PoC, which is based on Sitecore 10. With this new version I can remove all the implementation I have done on the Sitecore CM server side and just replace it with one single configuration, based on the Sitecore.JavaScriptServices.AppServices.PublishingWebHook.config.example:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/&quot; xmlns:role="http://www.sitecore.net/xmlconfig/role/&quot; xmlns:search="http://www.sitecore.net/xmlconfig/search/"&gt;
<sitecore>
<layoutService>
<publishWebHooks type="Sitecore.JavaScriptServices.AppServices.WebHooks.WebHooks, Sitecore.JavaScriptServices.AppServices">
<hooks hint="list:AddWebHook">
<hook type="Sitecore.JavaScriptServices.AppServices.WebHooks.WebHookDefinition, Sitecore.JavaScriptServices.AppServices">
<name>RenderingHostOutputCacheClear1</name>
<url>https://www.renderinhost1.localhost/api/outputcache/clear</url&gt;
<method>GET</method>
<site>Discovering.Sitecore101</site>
</hook>
<hook type="Sitecore.JavaScriptServices.AppServices.WebHooks.WebHookDefinition, Sitecore.JavaScriptServices.AppServices">
<name>RenderingHostOutputCacheClear2</name>
<url>https://www.renderinhost2.localhost/api/outputcache/clear</url&gt;
<method>GET</method>
<site>Discovering.Sitecore101</site>
</hook>
</hooks>
</publishWebHooks>
</layoutService>
</sitecore>
</configuration>

For comparison, here is my old custom configuration:

<?xml version="1.0"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:role="http://www.sitecore.net/xmlconfig/role/">
<sitecore role:require="Standalone or ContentManagement">
<RenderingHostCacheClear.Tenants>
<Tenant startPath="/sitecore/content/Discovering-Sitecore10">
<Endpoint>https://www.renderinhost1.localhost/api/outputcache/clear</Endpoint>
<Endpoint>https://www.renderinhost2.localhost/api/outputcache/clear</Endpoint>
</Tenant>
</RenderingHostCacheClear.Tenants>
<events>
<event name="publish:itemProcessed">
<handler
type="Discovering.Sitecore10.Processors.PublishEnd.TriggerRenderingHostCacheClear, Discovering.Sitecore10"
method="OnItemPublished"
resolve="true" />
</event>
</events>
</sitecore>
</configuration>

As you can see the configuration values are pretty similar, seems like Sitecore had the same idea how to handle web hooks 👀.

Good to know about Publishing Web Hooks

1. If calling the web hook, fails for some reason, the publish process will still succeeds, although the exception is logged with the “ERROR WebHook execution failed.” message, something like this for example when the given host is not reachable:

2492 11:08:24 ERROR WebHook execution failed.
Exception: System.AggregateException
Message: One or more errors occurred.
Source: mscorlib
at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
at Sitecore.JavaScriptServices.AppServices.WebHooks.PostWebHookRequestClient.Execute(WebHookDefinition definition)
Nested Exception
Exception: System.Net.Http.HttpRequestException
Message: An error occurred while sending the request.
Nested Exception
Exception: System.Net.WebException
Message: The remote name could not be resolved: 'www.discovering_sitecore101.localhost'
Source: System
at System.Net.HttpWebRequest.EndGetRequestStream(IAsyncResult asyncResult, TransportContext& context)
at System.Net.Http.HttpClientHandler.GetRequestStreamCallback(IAsyncResult ar)

2. At the moment there is no retry policy if calling the web hook fails. So if shit this happens, then you need restart a publish or call the web hook manually – after it’s supposed to work – to retrigger the publishing web hook event.

3. At the moment Sitecore can only send predefined hardcoded request body to your endpoint in the <body> parameter, when <method>POST</method> is used (<method>GET</method> does not send the <body> parameter for a reason). Although it’s possible to extend the default WebHookHandler to able to handle custom dynamic values.

4. If you go to production using a publishing web hook with multiple servers, you should have a dedicated server which sends the web hook events, because by enabling the default configuration, it runs on all CM and CD servers where it’s deployed. If the configuration is enabled on all servers, then all of them will trigger the publishing web hook event, which can lead to unnecessary load on the servers where the endpoint is enabled, as they will be called multiple times for one publish.

5. UPDATE: While exploring the publishing web hook, I found and reported a bug to Sitecore Support (483283). The WebHookHandler  fails to trigger all relevant web hooks if there is a web hook which should not run, before the ones which should run. No patch available but it should be fixed in the next release.

Happy hooking, aye! 🏴‍☠️

One thought on “Sitecore Publishing Web Hook – Sitecore Rendering Host output caching PoC update

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s