nystudio107 / craft-templatecomments

Adds a HTML comment with performance timings to demarcate `{% block %}`s and each Twig template that is included or extended.
https://nystudio107.com/
MIT License
20 stars 1 forks source link

Plugin breaks AJAX requests to Craft templates #4

Closed piotrpog closed 5 years ago

piotrpog commented 5 years ago

I encountered strange error today. I tried query template file using ajax. This template is just twig file in templates folder, and it worked fine when browsed it manually trough browser. But when I tried ajax, it returned error:

Exception 'Twig_Error_Syntax' with message 'Unknown "comments" tag.

It happened multiple times, not just one ajax request.

I figured it might have something to do with template comments plugin so i disabled it. And error disappeared. Most interesting thing is that when i enabled plugin again, problem did not appeared again.

My system info:

PHP version 7.2.1 Database driver & version MySQL 5.5.5 Image driver & version GD 7.2.1 Craft edition & version Craft Solo 3.1.5 Yii version 2.0.15.1 Twig version 2.6.2 Guzzle version 6.3.3 Imagine version 0.7-dev Plugins CP Field Inspect 1.0.5 Element Map 1.2.1 Enupal Translate 1.2.0 Imager v2.1.4 Image Resizer 2.0.4 Imager Pretransform 2.0.2 MobileDetect 1.0.2 Position Fieldtype 1.0.14 Redactor 2.3.0 Retour 3.1.5 SEOmatic 3.1.39 Smart Map 3.2.1 Super Table 2.1.5.3 Template Comments 1.0.5 Typed link field 1.0.16 Wordsmith 3.0.4

khalwat commented 5 years ago

Are you sure this is a request that Craft considers to be AJAX? I do test for this:

https://github.com/nystudio107/craft-templatecomments/blob/v1/src/TemplateComments.php#L91

How exactly are you loading this template? Code would help.

khalwat commented 5 years ago

If you're using Axios, you need to add the appropriate headers:

const VueAxios = axios.create({
    headers: {
        common: {
            'X-Requested-With': 'XMLHttpRequest',
        },
    },
});
khalwat commented 5 years ago

Will re-open if further info is provided

johndwells commented 5 years ago

@khalwat I've just encountered this. Running Template Comments 1.1.0, Craft 3.1.34.3.

We are using Axios to fetch the page over ajax, and we are sending the correct headers.

In TemplateComments::addComponents() you are testing for & aborting if the request is an AJAX one, however in TemplateComments::installEventListeners() you are not doing the same. Far as I can tell this is what's causing it.

khalwat commented 5 years ago

Fixed in https://github.com/nystudio107/craft-templatecomments/commit/20b70c4421fc52523c97ae9af15c9864979b6553

johndwells commented 5 years ago

Doh... this now breaks the CLI because isAjax() isn't available in the console. Sorry I didn't think of that when I suggested the fix. :(

khalwat commented 5 years ago

lol -- my bad, I should have known that

khalwat commented 5 years ago

Addressed in https://github.com/nystudio107/craft-templatecomments/releases/tag/1.1.2