Skip to content

Commit

Permalink
Update event-request-end.md
Browse files Browse the repository at this point in the history
  • Loading branch information
EvgeniyMukhamedjanov authored Nov 22, 2023
1 parent 96ad623 commit 23b9748
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions docs/v2/event-request-end.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,13 @@ Result:
## Sending requests from the event listener

If you want to send a Shopify Cart API request from a `liquid-ajax-cart:request-end` event listener,
make sure that the request that has been just finished and triggered the event **is not a [cart mutation](/v2/cart-mutations/)** request.
make sure that the request that triggered the event **is not a [cart mutation](/v2/cart-mutations/)** request.

Otherwise there is a high chance that you'll get into infinity loop of requests:
* cart mutation initiates a request A,
* the request A finishes and fires the `liquid-ajax-cart:request-end` event,
* your event listener initiates a request B,
* Liquid Ajax Cart notices that there was a request B that might change the cart state, so it runs cart mutation functions
* Liquid Ajax Cart notices that there was a request B that might change the cart state, so it runs cart mutation functions again,
* cart mutation initiates a request A ...

In order to prevent this, check the `initiator` property of the [Request state](/v2/request-state/) object:
Expand All @@ -107,4 +107,4 @@ document.addEventListener("liquid-ajax-cart:request-end", function(event) {
}
});
{%- endcapture -%}
{% include v2/codeblock.html language="javascript" code=highlight_code %}
{% include v2/codeblock.html language="javascript" code=highlight_code %}

0 comments on commit 23b9748

Please sign in to comment.