Empty matrix object after checking for a block type Not sure if this is a bug or my logic is off. I'm trying to add caching logic to templates to not cache certain pages if they have a block type form present because of the dynamic CSRF tokens. 

The logic:

{% set doNotCache = entry.mainBody.type('form').all()|length or craft.retour.getHttpStatus != 200 ? true : false %}


If the page returns true, the page renders properly. If the logic returns false the entry.mainBody object is empty (ie. doesn't render the matrix content). If I remove the logic all together the pages render properly.
So there are some changes to how .type() works in Craft 3 which you can read about here. What's happening is the type() you're setting here in your doNotCache is also getting applied later on in your template when you start actually looping over the blocks in entry.mainBody. There are two ways to solve this:


Reset the type() in your matrix block loop

{% for block in entry.mainBody.type(null).all() %}

Clone the mainBody field before you manipulate it

{% set doNotCache = clone(entry.mainBody).type('form').all()|length or craft.retour.getHttpStatus != 200 ? true : false %}