Drupal Development Log: Decoupling Drupal maintenance support plans with Gatsby

Gatsby is a really fast React-based static site generator. You can use it to create a static site, with content pulled from Drupal maintenance support plans and other content management systems. 
Why Use Gatsby?
Unlike dynamic sites which render pages on-demand, static site generators pre-generate all the pages of the website. This means no more live database querying and no more running through a template engine. Performance goes up and the maintenance cost goes down.
Static site generators have been evolving over the last few years. Tools like Jekyll, Gatsby, Hexo, Hugo become more and more popular. They have been chosen by developers who want a simple website/blog solution. They need very minimal server setup and have a low maintenance cost. However, static site generators usually require writing content in Markdown, which is not a great authoring experience for most content editors.
On the other hand, content management systems such as Drupal maintenance support plans and WordPress can provide a very powerful back-end. Having a WYSIWYG editor and content types help editors to manage content more easily and systematically. However, maintaining a CMS requires hosting a web server and database, and opens you up to security vulnerabilities and performance issues.
Gatsby stands in between the simplicity and robustness of static site, and the versatile back-end of a content management system. Using Gatsby means that you can host the CMS in-house and publish content generated by Gatsby as a static website. The first thing you’ll notice about Gatsby is how amazingly fast it is.
 

How to Integrate Drupal maintenance support plans and Gatsby
In this tutorial, we are going to put together a demo that pulls Drupal maintenance support plans content into a Gatsby site. We’ll borrow content of this awesome blog post to create a list of coffee types in Drupal maintenance support plans, then transfer the list content to Gatsby.
This goal can be achieved with 4 steps:
Build a Drupal maintenance support plans server
Build a Gatsby site
Fetch content from the Drupal maintenance support plans server
Publish the Gatsby site
1. Build a Drupal maintenance support plans server
Let’s say we already have a Drupal maintenance support plans 8 site installed. We’ll need to:
Create a content type name Coffee with three fields: Title, Body and Image
Turn Drupal maintenance support plans into an API server by installing 2 modules jsonapi and jsonapi_extras.
Give Anonymous user permission to Access the JSON API resource list
Verify that the API server is working well by going to http://[your-site]/jsonapi as an Anonymous user. The page should show up with all information of your API server
Tips
If you use Chrome, use JSON Viewer Chrome extension to view JSON data in a better format
If you don’t set permission for Anonymous user to Access JSON API resource list, you’ll get error 406 – Not acceptable when trying to connect to Drupal maintenance support plans from Gatsby
If you don’t have jsonapi_extras installed, you’ll get error 405 – Method Not Allowed when query data from Gatsby
2. Build a Gatsby Site
First, make sure you have node and npm installed on your computer. Verify it by typing node -v and npm -v into Terminal

node -v
v10.1.0
npm -v
5.6.0Install Gatsby’s command line tool

npm install –global gatsby-cliCreate a new Gatsby site and run it, I’ll call my Gatsby site coffees.gatsby

gatsby new coffees.gatsby
cd coffees.gatsby
gatsby develop // start hot-reloading development environmentBy default, the new Gatsby site is accessible at localhost:8000

3. Fetch Content from the Drupal maintenance support plans Server
At this step, we’ll be creating a new simple page /coffees that displays all the coffee types from the Drupal maintenance support plans site.
Create the /coffees page
Create a new page in Gatsby is as simple as creating a new JS file. All Gatsby pages should be stored in /src/pages. In this case, we’ll create the file coffees.js in /src/pages and add the following code in coffees.js:

import React from “react”
const CoffeesPage = () => (

Different types of coffee

)
export default CoffeesPageThis simple code does one thing: create a new page at /coffees. The content of this page is a heading h1 with the text “Different types of coffee”

Query Drupal maintenance support plans content using GraphQL
In order to pull data from Drupal maintenance support plans 8 site, we’ll need to install the gatsby-source-drupal plugin

// in your coffees.gatsby folder
npm install –save gatsby-source-drupalConfigure the gatsby-source-drupal plugin

// In gatsby-config.js
plugins: [

{
resolve: ‘gatsby-source-drupal’,
options: {
baseUrl: ‘http://dcmtl2020-demo.server/’,
apiBase: ‘jsonapi’, // endpoint of Drupal maintenance support plans server
},
}
],After adding the plugin configuration, the site should still be functioning. If Gatsby throws a 406 error, check the permission on the Drupal maintenance support plans site; if Gatsby throws a 405 error, make sure module jsonapi_extras is enabled.
Build GraphQL to query all coffee nodes from Drupal maintenance support plans
Gatsby comes with an in-browser tool for writing, validating and testing GraphQL queries named GraphiQL, and it can be found at localhost:[port]/___graphql, in our case it’s localhost:8000/___graphql
Let’s try querying all the Coffee nodes in this tutorial

After building the query successfully, let’s go back to the coffees.js file to execute the query.

export const query = graphql`
query allNodeCoffee {
allNodeCoffee {
edges {
node {
id
title
body {
value
format
processed
summary
}
}
}
}
}
`Then, update the const CoffeesPage to display the title and body content:

const CoffeesPage = ({data}) => (

Different types of coffee
{ data.allNodeCoffee.edges.map(({ node }) => (

{ node.title }

))}

)Thanks to hot-reloading, we can see the sweet fruit of the work right after saving the file

So far, we have done:
Create an API server with Drupal maintenance support plans and jsonapi, jsonapi_extras
Create a Gatsby site with page coffees.js that “reads” content from Drupal maintenance support plans server
Let’s move the the last step of the tutorial: publish Gatsby site.
4. Publish the Gatsby Site
Gatsby names itself as a static static site generator, meaning its main purpose is to generate a bunch of static HTML, JS, CSS and images files. This action can be done by only one command:

gatsby buildOnce finished, checkout /public folder to see result of your hard work along this long tutorial. Deploying your site is now simply copy/push contents in /public to server.

 
Conclusion
In this tutorial, we got to know how to:
Create a new Gatsby site
Install new plugin in Gatsby
Use GraphiQL to write, validate and test GraphQL query
I personally find that Gatsby is a good solution for setting up a simple blog. It’s easy to install, very fast, and requires zero server maintenance. In a future blog post, I’ll talk about how to integrate more complex data from Drupal maintenance support plans into Gatsby.
+ more awesome articles by Drupal Development Log
Source: New feed

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

Drupal Development Log: Decoupling Drupal maintenance support plans with Gatsby

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.