Skip to content

Commit

Permalink
fix(sanity): prevent empty actions being executed
Browse files Browse the repository at this point in the history
  • Loading branch information
juice49 committed Aug 12, 2024
1 parent 7860c8e commit c2e4eb3
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -331,10 +331,22 @@ describe('checkoutPair -- server actions', () => {
])
draft.commit()

expect(mockedActionRequest).toHaveBeenCalledWith([], {
tag: 'document.commit',
transactionId: expect.any(String),
})
expect(mockedActionRequest).toHaveBeenCalledWith(
[
{
actionType: 'sanity.action.document.edit',
draftId: idPair.draftId,
publishedId: idPair.publishedId,
patch: {
unset: ['_empty_action_guard_pseudo_field_'],
},
},
],
{
tag: 'document.commit',
transactionId: expect.any(String),
},
)

sub.unsubscribe()
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ function isLiveEditMutation(mutationParams: Mutation['params'], publishedId: str
}

function toActions(idPair: IdPair, mutationParams: Mutation['params']): Action[] {
return mutationParams.mutations.flatMap((mutations) => {
const actions = mutationParams.mutations.flatMap<Action>((mutations) => {
// This action is not always interoperable with the equivalent mutation. It will fail if the
// published version of the document already exists.
if (mutations.createIfNotExists) {
Expand All @@ -123,6 +123,24 @@ function toActions(idPair: IdPair, mutationParams: Mutation['params']): Action[]
}
throw new Error('Cannot map mutation to action')
})

// Empty action invocations are a noop; although Content Lake accepts them, no transaction will
// be executed, causing Studio to become stuck in a pending state. To prevent this occurring, a
// fake unset mutation is added whenever an empty set of actions would otherwise be executed.
if (actions.length === 0) {
return [
{
actionType: 'sanity.action.document.edit',
draftId: idPair.draftId,
publishedId: idPair.publishedId,
patch: {
unset: ['_empty_action_guard_pseudo_field_'],
},
},
]
}

return actions
}

function commitActions(client: SanityClient, idPair: IdPair, mutationParams: Mutation['params']) {
Expand Down

0 comments on commit c2e4eb3

Please sign in to comment.