Delivering Extbase plugins in a headless page 

A plugin is the one kind of content element whose fields do not describe its output. Its content element holds only the configuration an editor chose, such as a selection or a storage folder. The output comes from the plugin itself, and an Extbase plugin renders HTML by default.

In the JSON page type from Delivering a TYPO3 page as JSON, the plugin has to deliver JSON instead, which the page template inserts as the data of its content element. This page shows how the plugin is rendered for its content element, how its action answers with JSON, and what changes when the action is not cacheable.

A conference list plugin in the content area of a page delivers this:

Content area in the response of https://example.org/conferences/page.json (excerpt)
{
  "content": {
    "main": [
      {
        "uid": 41,
        "type": "myextension_conferencelist",
        "header": "Upcoming conferences",
        "headerLink": "",
        "data": [
          {
            "uid": 12,
            "title": "Community Conference Europe",
            "link": "https://example.org/conferences/conference/12"
          },
          {
            "uid": 15,
            "title": "Community Conference Asia",
            "link": "https://example.org/conferences/conference/15"
          }
        ]
      }
    ]
  }
}
Copied!

Rendering an Extbase plugin for its content element in JSON 

The content element partial from Delivering content elements as JSON gets a section for the plugin's content type. The section renders the plugin with the f:cObject ViewHelper:

EXT:my_sitepackage/Resources/Private/Json/Partials/ContentElement.fluid.html (excerpt)
<f:section name="myextension_conferencelist">
  <f:cObject
    typoscriptObjectPath="tt_content.myextension_conferencelist.20"
    data="{record}"
    table="tt_content"
  />
</f:section>
Copied!

ExtensionUtility::configurePlugin() registers the TypoScript tt_content.myextension_conferencelist for the plugin's content type. It renders the content element with its frame HTML, and the plugin itself in 20 . The section renders only 20 , so the output is the plugin's and nothing else.

data="{record}" and table="tt_content" render the plugin for the content element at hand. The plugin reads its configuration from that record just as it does in the HTML page: the settings the editor chose in its FlexForm, and its storage folder from the field Record Storage Page. One plugin can therefore deliver different selections on different pages, without a line of configuration in the page type.

The section writes the output of the plugin as it is, without f:format.json . The plugin delivers JSON already, and encoding it again would turn it into a string.

Letting an Extbase plugin answer with JSON templates 

The conference list plugin also renders the conference list of HTML pages, and an editor places it the same way on every page. The plugin needs the same actions for both: listAction() and showAction() , configured as usual:

EXT:my_extension/ext_localconf.php
<?php

use MyVendor\MyExtension\Controller\ConferenceController;
use TYPO3\CMS\Extbase\Utility\ExtensionUtility;

defined('TYPO3') or die();

ExtensionUtility::configurePlugin(
  'MyExtension',
  'ConferenceList',
  [ConferenceController::class => ['list', 'show']],
);
Copied!

What differs is the representation. In the JSON page type, the plugin renders its actions with JSON templates instead of HTML templates. A TypoScript condition sets the format of the plugin to json for that page type only:

EXT:my_sitepackage/Configuration/Sets/MySitepackage/setup.typoscript
[request.getPageArguments().getPageType() == 1762214410]
  plugin.tx_myextension_conferencelist.format = json
[end]
Copied!

With the format json, Fluid looks for the template Conference/List.fluid.json instead of Conference/List.fluid.html. The controller stays as it is. The JSON templates follow the same rules as the templates of the page, see Writing JSON in a Fluid template:

EXT:my_extension/Resources/Private/Templates/Conference/List.fluid.json
{escaping off}
[
  <f:for each="{conferences}" as="conference" iteration="iteration">
    {
      "uid": <f:format.json value="{conference.uid}" />,
      "title": <f:format.json value="{conference.title}" />,
      "link": <f:format.json value="{f:uri.action(action: 'show', arguments: {conference: conference})}" />
    }<f:if condition="{iteration.isLast}"><f:else>,</f:else></f:if>
  </f:for>
]
Copied!
EXT:my_extension/Resources/Private/Templates/Conference/Show.fluid.json
{escaping off}
{
  "uid": <f:format.json value="{conference.uid}" />,
  "title": <f:format.json value="{conference.title}" />,
  "description": <f:format.json value="{conference.description}" />
}
Copied!

The format applies to the plugin only within the condition, so the HTML pages keep rendering the HTML templates.

The same approach delivers a plugin of another extension as JSON. Its JSON templates go into a template path of the site package, added with a higher key than the paths of the extension, see Overriding templates of a third-party extension. The extension itself does not need to change.

The action may still return htmlResponse() . Its content type does not reach the visitor, because the additionalHeaders of the JSON page type are applied last and set application/json.

Letting an Extbase plugin answer with JsonView 

A plugin that exists only for the headless frontend can render its data with JsonView instead of templates. The action shapes the data as an array and returns jsonResponse() , as the seats example does. The output is inserted into the page JSON in the same way.

Such a plugin sets the content type application/json itself. On an HTML page, that content type applies to the whole page, see An action returning JSON breaks the page. Use JsonView for plugins that are rendered in the JSON page type only, and JSON templates for plugins that appear on HTML pages as well.

Cacheable and non-cacheable Extbase plugins in a JSON page 

The output of a cacheable action is stored in the page cache together with the rest of the page JSON. A request for the same page receives it from the cache, without running the action.

A non-cacheable action is rendered as a USER_INT object. TYPO3 caches the page JSON with a placeholder where the plugin output belongs, and replaces the placeholder on every request with the output of the action. This works in the JSON page because the section writes the plugin output as it is. Passed through f:format.json , the placeholder would be encoded as a string, TYPO3 would no longer find it, and the JSON would contain the placeholder instead of the plugin output.

A page with a non-cacheable plugin is sent with the header Cache-Control: private, no-store, so neither the browser nor a proxy keeps a copy of the page JSON. Values that change on every request, such as the seats left in a conference, can therefore be cheaper to deliver from an endpoint of their own, which the frontend requests separately. Loading Extbase data with fetch() builds such an endpoint.

Next steps after delivering Extbase plugins as JSON 

The page type now delivers complete pages, including the output of their plugins. Connecting a headless frontend to TYPO3 describes what the frontend has to take care of when it requests them: mapping URLs, requests from the browser of another origin, frontend users, caching, and previews for editors.