I have a package that essentially allows you to do strongly typed validation with IPublishedContent. The code hinges on being able to retrieve the preview state of a page during a ContentPublishingNotification so that in turn we can validate against the published content.
Recently I was testing a new release on Umbraco (17.6.2) and started noticing I was getting stale preview content when using save and publish. This meant that when a value was changed it would initially retrieve it’s non dirty value causing incorrect validation states and then a following publish it would update. However, if I first saved only then did a publish the values would be correct.
What confuses me that on older versions roughly around <= 17.3.0 it does work as expected so I’m unsure why this would be the case now? I presume this is a change in the underlying Umbraco code which means it’s not refreshed from when the content is saved and the publishing notification retrieves the preview content.
Does anyone have any suggestions of the best way to achieve the desired behaviour or if it’s just a non starter? I see other packages use the same technic but perhaps within the notification handlers this is a step too far.
I’ve had a look into this. The cache refresh doesn’t fire straight away, it’s queued and only flushed once the current scope completes. Umbraco’s docs confirm it, ContentSavedNotification and ContentPublishedNotification are “published upon successful completion of the current scope”. That’s what triggers the cache update.
ContentPublishingNotification is different, it’s cancelable, so it fires immediately rather than waiting for scope completion, that’s how it’s able to stop the operation. If your handler runs inside a scope that hasn’t completed yet, it’ll see the cache as it was before that scope’s changes were flushed.
Save then publish as two separate operations each gets its own scope, so the first one’s cache refresh has already been flushed by the time the second starts, that lines up with why that combination works for you and a single save-and-publish doesn’t.
The correct way to get the values would be ContentPublishingNotification.PublishedEntities, it gives you the IContent with the new values directly, no cache involved. Doesn’t help you here though since you need the IPublishedContent for your strongly typed models.
I couldn’t find anything in the changelogs confirming a change to this behaviour between versions, so worth raising with HQ to get it confirmed.