putyourlightson / craft-spark

A real-time, template-driven hypermedia framework for Craft CMS.
MIT License
17 stars 0 forks source link

Error: Expected content-type to be text/event-stream, Actual: text/html; charset=UTF-8 #14

Closed atelierbim closed 1 week ago

atelierbim commented 1 week ago

Support Request

I setup a fresh craft install with ddev (Docker Desktop) and installed Spark plugin.

index.twig

    <div data-store="{ search: ''}">
        <input data-model="search" data-on-input.debounce_500ms="{{ spark('spark/fragments/search-results.twig') }}">
        <div id="search-results">
        </div>
    </div>

search-results.twig

<div class="search-results">
  {% set results = [] %}

  {% if search in not empty %}
    {% set results = craft.entries()
      .section('worksChannel')
      .search(search)
      .all() %}
  {% endif %}

  {% for work in results %}
    <p>{{ work.title }}</p>
  {% endfor %}

</div>
{% endfragment %}

when typing in the search input I get this in the console

Failed to load resource: the server responded with a status of 500 ()
Error: Expected content-type to be text/event-stream, Actual: text/html; charset=UTF-8

Here's the output:

<input data-model="search" data-on-input.debounce_500ms="$$get(&#039;https://spark-demo.ddev.site/index.php?p=actions/spark-module&amp;config=cd4cc9878d5fe99dbd021032ca72053f419065f99fac9b3f1fd6edc76c028e93{%22siteId%22%3A1%2C%22template%22%3A%22%5C/spark%5C/fragments%5C/search-results.twig%22}&#039;)">

Is there any ddev setting to do that is not in the docs? I also wanted to know if Spark is supposed to work on Craft Cloud hosting. Thanks

Plugin Version

1.0.0-alpha.5

bencroker commented 1 week ago

Please check the response body, there is likely an error in your Twig template code.

atelierbim commented 1 week ago

I thought Spark would inject the store variable to the fragment. I apparently doesn't do it in my case:

Twig\Error\RuntimeError: Variable "search" does not exist. in /var/www/html/templates/spark/fragments/search-results.twig:4
Stack trace:
#0 /var/www/html/storage/runtime/compiled_templates/92/92725ae0a433e6d15f9df60fc755d639.php(52): __TwigTemplate_2ae6099b6bdb7e6948fdeec956e17829-&gt;{closure}()
#1 /var/www/html/vendor/twig/twig/src/Template.php(393): __TwigTemplate_2ae6099b6bdb7e6948fdeec956e17829-&gt;doDisplay(Array, Array)
#2 /var/www/html/vendor/twig/twig/src/Template.php(349): Twig\Template-&gt;yield(Array, Array)
#3 /var/www/html/vendor/twig/twig/src/Template.php(364): Twig\Template-&gt;display(Array)
#4 /var/www/html/vendor/twig/twig/src/TemplateWrapper.php(35): Twig\Template-&gt;render(Array)
#5 /var/www/html/vendor/twig/twig/src/Environment.php(306): Twig\TemplateWrapper-&gt;render(Array)
#6 /var/www/html/vendor/craftcms/cms/src/web/View.php(539): Twig\Environment-&gt;render(&apos;spark/fragments...&apos;, Array)
#7 /var/www/html/vendor/putyourlightson/craft-spark-module/src/services/ResponseService.php(93): craft\web\View-&gt;renderTemplate(&apos;spark/fragments...&apos;, Array)
#8 /var/www/html/vendor/putyourlightson/craft-spark-module/src/services/ResponseService.php(40): putyourlightson\spark\services\ResponseService-&gt;renderTemplate(&apos;spark/fragments...&apos;, Array)
#9 /var/www/html/vendor/putyourlightson/craft-spark-module/src/controllers/DefaultController.php(61): putyourlightson\spark\services\ResponseService-&gt;stream(Object(putyourlightson\spark\models\ConfigModel), Object(putyourlightson\spark\models\StoreModel))
#10 [internal function]: putyourlightson\spark\controllers\DefaultController-&gt;putyourlightson\spark\controllers\{closure}()
#11 /var/www/html/vendor/yiisoft/yii2/web/Response.php(454): call_user_func(Object(Closure))
#12 /var/www/html/vendor/yiisoft/yii2/web/Response.php(343): yii\web\Response-&gt;sendContent()
#13 /var/www/html/vendor/yiisoft/yii2/base/Application.php(390): yii\web\Response-&gt;send()
#14 /var/www/html/web/index.php(12): yii\base\Application-&gt;run()
#15 {main}
bencroker commented 1 week ago

It does, but you need to call store.search.

    {% set results = craft.entries()
      .section('worksChannel')
      .search(store.search)
      .all() %}
atelierbim commented 1 week ago

OK got it. Thanks Ben. Here's the updated code: in search-results.twig

{% set results = [] %}
  {% if store.search is not empty %}
  {% set results = craft.entries()
  .section('worksChannel')
  .search(store.search)
  .all() %}
  {% endif %}