✨Discover storytelling in the AI age with Pixar's Matthew Luhn at Sanity Connect, May 8th—register now

Struggling to extend the functionality of the default publish action in Sanity.io and looking for guidance and code examples. Sharing code snippets and seeking help fromthe...

16 replies
Last updated: Apr 16, 2021
I’m really struggling with how to add extra operations to the Publish action while preserving it’s original functionality (field validating, etc). Is there a more general explanation for the default publish action? I can see the code for it but a) it’s in typescript which adds more confusion when trying to learn/port it b) is rather complex and isn’t as readable as I’d like (compounded by the use of react hooks which results in a lot of things flying around all at once). I’m convinced I’m missing something - I’ve seen all the guides on the document actions API, example code on setting a field at publish time, etc but none of them explain the higher concepts in a way that informs me on how I should be extending existing functionality. I’ve rewritten this multiple times but always end up with a completely broken mess. Am I the only one trying to do these kinds of things unsuccessfully? Also has anyone ever created unit tests for these? I can’t fathom how one can be certain it works as expected without them.. thanks!
Apr 9, 2021, 2:54 PM
Hey User, based off your last thread, the specific functionality you’re looking for is:-preserve existing document validations
-prompt the user to confirm they want to publish the changes
-set a custom
lastUpdated
field-publish the document
Am I missing anything? We can start breaking this down piece by piece.
Apr 9, 2021, 3:54 PM
yep! one nuance would be
prompt the user to confirm they want to publish the changes
is only when a date field has changed (it drives site URLs). Sorry if I’m being a pest on this, I’ve jsut never run into a problem with Sanity I couldn’t find great docs for so this is really weird! I want to share my result when it works too
Apr 9, 2021, 3:56 PM
Not a pest at all! It’s definitely complex to figure these things out. Let me pass this along to some team members and see if we can get it working. Do you feel comfortable sharing any of your code snippets associated with this?
Apr 9, 2021, 3:58 PM
sure, what would be useful? I have a lot of spaghetti code but I’ll do what I can
Apr 9, 2021, 3:59 PM
How about the document action resolver? Or whatever your
"part:@sanity/base/document-actions/resolver"
points to.
Apr 9, 2021, 4:02 PM
heh alright this is going to be awful, it’s largely based on the built-in publish action
Apr 9, 2021, 4:05 PM
it’s obviously a WIP - I left off at trying to figure out why the doc was POSTing updates on every text input rather than holding off until I hit publish
Apr 9, 2021, 4:05 PM
Awesome, thanks for this. I’ll start asking around and digging in.
Apr 9, 2021, 4:09 PM
I really appreciate it, thank you!
Apr 9, 2021, 4:10 PM
Hi
user S
. Heres an example of just delegating to the built in Publish action, after a confirmation popover

https://gist.github.com/runeb/354d42cb5751c3d1638400be7556f2c2
Apr 9, 2021, 10:25 PM
Followed this bc I'm looking for a similar solution and a note on the gist above -- there's a weird way to get the default PublishAction, but I think you can just import it like this:
import { PublishAction } from 'part:@sanity/base/document-actions';
Apr 10, 2021, 1:14 AM
Thanks
user S
, that is indeed the best way! I have updated the gist
Apr 12, 2021, 4:18 PM
this helped me a lot too, thanks for this. I’m now getting a much cleaner implementation of mashing a couple actions together without demolishing/replicating the existing functionality
Apr 14, 2021, 3:50 PM
here’s what I’ve landed on, with all the inputs I’ve gotten from everyone, which I appreciate greatly!
Apr 16, 2021, 3:10 PM
All this does is watch to see if you’ve edited a special datefield (drives slugs in our blog posts) and if so, prompts to make sure you know what you’re doing. Lastly, it keeps a last updated field up to date. Reason for a last updated field is so we can do API updates to content (such as a data migration that doesn’t change the resulting display content) without all the dates on the site saying everything was just updated.
Apr 16, 2021, 3:13 PM
here’s where I make use of this:
Apr 16, 2021, 3:17 PM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?