Upgrade Your Drupal Skills

We trained 1,000+ Drupal Developers over the last decade.

See Advanced Courses NAH, I know Enough

Subrequests as another speed secret for Drupal 8

Parent Feed: 

The unmatched capabilities to interact with other systems is listed among Drupal 8’s best improvements (along with the Views and BigPipe in core, easier migrations, handier configuration management, excellent web accessibility, and much more). And there are various ways to provide great performance in this interaction. One of them is the new and fresh module called Subrequests. We will briefly get acquainted with the Subrequests’ principle of work and features for speed enhancement today.

Drupal 8 and third-party integration

Let’s first take a glance at your Drupal 8 site’s ability to share its data to any external systems you need. These can be native mobile applications on Android or iOs, super dynamic JavaScript apps (based on Angular, React, Node, Ember, Backbone, etc.), or any other third-party systems.

Among other things, this lets you use the trending “headless”, or “decoupled” approach, when Drupal serves as a back-end and outputs its data to some front-end system for the sake of exceptional speed, interactivity, real-time features, and more.

The key thing here is to present Drupal’s HTML data in the formats needed by these applications (JSON, XML, or HAL+JSON). Just like good interpreters help people who speak different languages, there are high-class “synchronous interpreters” between Drupal and third-party apps.

These great assistants transform the data into the required formats. The Drupal core now has a “team” of these built-in. These are web services: Serialization, HAL, RESTful Web services, and HTTP Basic Authentication.

In addition, there also are great contributed modules, one of which we will present right now.

Subrequests module and its performance-improving mission

How to help the “synchronous translation” work exceptionally fast? Here is where the Subrequests module comes in.

The Subrequests module groups any sets of requests together, with absolutely no limitations. Thanks to this aggregation, all JSON API requests are sent in just one. The absence of round-trips for fetching Drupal’s data greatly improves performance. Drupal bootstrap is only needed once.

The single request contains all the information that the app needs from Drupal. It’s kept in a JSON document called “blueprint.” The blueprint lists multiple subrequest objects.

The Subrequests module allows for very handy options like sending parallel requests, sending requests that depend on the response to previous request, and more.

Installing the Subrequests Drupal 8 module

This module did not exist in Drupal 7. Subrequests are a true novelty of Drupal 8, with its 8.x-2.0-beta3 released just a month ago.

For efficient work, the module needs some external PHP libraries. So to install the Subrequests module with all these libraries, you could use this Composer command:

composer require drupal/subrequests:^2.0

More technical details along with the code samples can be found in the article of the module’s maintainers, Lullabot, and on the module’s drupal.org page.

Wrap-up

To get the best of the Subrequests Drupal 8 module, create custom solutions for your website’s great performance and handy integration, or migrate to Drupal 8, contact InternetDevels developers. Get more news from our newsletter and on our social media pages!

Author: 
Original Post: 

About Drupal Sun

Drupal Sun is an Evolving Web project. It allows you to:

  • Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
  • Facet based on tags, author, or feed
  • Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
  • View the entire article text inline, or in the context of the site where it was created

See the blog post at Evolving Web

Evolving Web