Building the Amplience blog (pt.1): JAMstack & Dynamic Content
In this blog we'll look at how we built the Amplience blog site itself, using the tools and technologies that we've been building up over several years, including our modern headless Content Management System (CMS) - Dynamic Content. If you're a developer looking to build lightning fast microsites using Dynamic Content, then this post will help get you started.
If you can't wait to get started, you can find the source code on GitHub.
This blog post is part 1 of a series of 2. In Part 1 we explain how the blog is delivered as a static website using the JAMstack architecture, while in Part 2 we go into detail about how we made use of Dynamic Content’s preview and visualization features to allow users to preview their content before it’s published.
What we’ll cover
Why we chose the technologies we used
How to get data into your application using our SDK
Common patterns used when building a static site
We will not be going into too much detail into framework specific implementation details. Instead we’ll focus on how to build a site using Dynamic Content and how a simple workflow works.
What technology did we use to build this blog?
The App framework
For the framework we used Next.js. It's based on React with Server Side Rendering (SSR) and static export capabilities. We want to export our site statically at build time and don't require runtime SSR for a basic blog site, but the SSR features are useful for building dynamic preview and visualization features (see part 2 of this blog series).
We also decided to use TypeScript, to define structured types based on our schemas and to create lovely typesafe components.
To render a blog's markdown content as rich text we use react-markdown. This was chosen because it’s easy to use and provides control over rendering and styling.
For hosting we chose Netlify. Netlify is essentially a Continuous Integration(CI)/Continuous Deployment(CD) pipeline integrated with GitHub: you push changes to your master branch, it rebuilds your site and pushes to the production CDN.
Netlify includes easy to configure "build hooks" that will listen for an external webhook event and rebuild the site when triggered. Build hooks are set up in Netlify in 'site settings' -> 'build and deploy' and provide a webhook URL that we add to a webhook in Dynamic Content.
We created a webhook in Dynamic Content with the URL from Netlify and chose to trigger it with the "Edition published" event. This event is sent when an edition containing a slot with content for the blog is published.
We now have a continuous deployment pipeline set up that publishes our content whenever the codebase or the content changes.
To create, schedule and deliver the content for our blog we used Dynamic Content.
In this section, we’ll provide a quick overview of Dynamic Content’s functionality.
The following features of Dynamic Content were key to building the blog:
Content Types to model our data
Webhooks to trigger builds
The Content Delivery API to fetch our data at build time
Visualization and preview so that we can have a side by side view of the content in the browser when editing changes. We’ll cover visualization and preview in detail in Part 2.
Dynamic Content uses content types, defined using JSON Schema, that specify the way the content is structured and validated and can be used to model complex relationships between content. Content type schemas are easy to create using the Dynamic Content Schema Editor, as shown in the example below.
Once content types are deployed, your users can start using them to create content. This is done from the production section of the Dynamic Content app. To create a new piece of content, the user just chooses the type of content to add and fills in the content editing form as shown below, adding images and video from their own media repository. The content editing form also applies your business rules, such as the maximum and minimum length of the copy text, defined as part of the content type.
A visualization of how the blog will appear once it’s published is shown on the right hand side of the window. We’ll explain how to show this visualization, as well as how to preview the blog site at a specified date and time, in part 2 of this blog.
When building this blog we wanted to design a simple content model that would be able to grow to meet our needs in the future. We settled on 5 Content types and 1 Slot to begin with, we felt this would give us room to expand it later.
Our Content Model
Here is a model of the schemas we've created for our blog:
A root Slot which we use to plan and schedule our posts.
Our Blog List content type. This is made up of a list of Blog Posts, with a title and a subheading.
This was all we needed in order to create our homepage for our first iteration.
A Blog Post is where the main content lives. It includes the following properties:
Date of creation
A description of the post
A slug for SEO friendly URLs
A list of content: this can be made up of rich text, videos and images
A list of the blog post authors
When modeling your content you should have reuse at the front of your mind. Each one of our content types could be reused to make new blocks. The Author content type could be used for filtering, while Blog Post and Blog List could be used to populate a list of related posts. This shows the power of a content type based model.
Fetching content at build time
So, how do we populate our application with data? Well in Next.js you have to provide a map of all the pages with all the resources it needs to build that page. Your React Page containers will have a getInitialProps function which will be called at build time to populate your page and then export it as static HTML.
To retrieve the content we made use of the Dynamic Content Delivery SDK. Setting up the SDK is simple, just provide your account settings and then pass the content id to the getContentItem function as shown in the code below. When the request is resolved your JSON data is returned.
next.config.jsfile that fetches blog posts, creates a map of all the blogs and defines their path. It also provides an id so that the blog post itself can handle fetching of its data.
blog.tsx file is shown below. It is passed a query from
next.config.js and can then fetch the blog post for that route, process the data and then return it to the component. The code in blog.tsx file would be used to display a single post and in this example it would take a title, description and the content of the post in markdown format.
One of the benefits of using a Static Site Generator is that you can pass meta data on a per page basis in order to gain better SEO.
We wanted to ensure that our site met accessibility standards from the very start. To do this we created image/video content-types that made it a requirement to supply an alt tag, used for accessibility to describe an image to those users who are unable to see it. This means that when our site renders an image we can create a component that expects an alt tag and ensures all imagery is accessible.
Here's the image content type schema:
At build time we generate image URLs using the Dynamic Content Delivery SDK.
This allows us to make use of the features of Amplience Dynamic Media and serve multiple permutations of an asset, with different size, format and quality options, from the same source URL. A good use case for this would be using the same asset for the high quality feature image used at the top of a blog post and for a lower quality thumbnail image.
This also gives us the building blocks to build image source sets so that the appropriately sized image can be chosen for each screen size.
A basic example of building an image source set is shown below. We just provide an array with the desired sizes and generate URLs from the same source image, but with the width and height parameters for each size.
Where to go from here
We’ve made the source code for the blog available on GitHub and it’s a great starting point for your own projects. It features out of the box: a Progressive Web App manifest, offline support and SASS support using TypeScript and it should give you everything you need to make a lean static site powered by Dynamic Content.
If you want to know more about how to use Next.js as a Static Site Generator view their [docs] (https://nextjs.org/docs#usage).
In part 2 of this blog series we’ll look at how the visualization and preview features were implemented in the blog, allowing you to view individual blog posts before they’re published and see how the entire site will look at a specified date and time.