Skip to main content

Hey, I finally have a new website 👋

I’m a founder, designer, and filmmaker — and I’m trying to capture a bit more of all of this with my new site.

Had a lot of fun making this in Next.js, with @TryGhost as backend, deployed on @vercel.

Check it out → https://t.co/iawYNTuB8y pic.twitter.com/o1i81y5uL6
— Fabrizio Rinaldi (@linuz90) August 3, 2021

Prerequisites

This configuration of a Ghost publication requires existing moderate knowledge of JavaScript and React. You’ll need an active Ghost account to get started, which can either be self-hosted or using Ghost(Pro). Additionally, you’ll need to setup a React & Next.js application via the command line:
Then answer the prompts. The examples in these docs answer “No” to all for simplicity:
Note this uses the pages router, not the app router.
Finally, start the app:
Next.js can also be setup manually – refer to the official Next.js documentation for more information.

Getting started

Thanks to the JavaScript Content API Client Library, it’s possible for content from a Ghost site can be directly accessed within a Next.js application. Create a new file called posts.js within an lib/ directory. This file will contain all the functions needed to request Ghost post content, as well as an instance of the Ghost Content API. Install the official JavaScript Ghost Content API helper using:
Once the helper is installed it can be added to the posts.js file using a static import statement:
Now an instance of the Ghost Content API can be created using Ghost site credentials:
Change the url value to the URL of the Ghost site. For Ghost(Pro) customers, this is the Ghost URL ending in .ghost.io, and for people using the self-hosted version of Ghost, it’s the same URL used to view the admin panel. Create a custom integration within Ghost Admin to generate a key and change the key value.
For more detailed steps on setting up Integrations check out our documentation on the Content API.

Exposing content

The posts.browse() endpoint can be used to get all the posts from a Ghost site. This can be done with the following code as an asynchronous function:
Using an asynchronous function means Next.js will wait until all the content has been retrieved from Ghost before loading the page. The export function means your content will be available throughout the application.

Rendering posts

Since you’re sending content from Ghost to a React application, data is passed to pages and components with props. Next.js extends upon that concept with getStaticProps. This function will load the Ghost site content into the page before it’s rendered in the browser. Use the following to import the getPosts function created in previous steps within a page you want to render Ghost posts, like pages/index.js:
The posts can be fetched from within getStaticProps for the given page:
Now the posts can be used within the Home page in pages/index.js via the component props:
Pages in Next.js are stored in a pages/ directory. To find out more about how pages work check out the official documentation.

Rendering a single post

Retrieving Ghost content from a single post can be done in a similar fashion to retrieving all posts. By using posts.read() it’s possible to query the Ghost Content API for a particular post using a post id or slug. Reopen the lib/posts.js file and add the following async function:
This function accepts a single postSlug parameter, which will be passed down by the template file using it. The page slug can then be used to query the Ghost Content API and get the associated post data back. Next.js provides dynamic routes for pages that don’t have a fixed URL / slug. The name of the js file will be the variable, in this case the post slug, wrapped in square brackets – [slug].js. In order to generate these routes, Next.js needs to know the slug for each post. This is accomplished by using getStaticPaths in posts/[slug].js. Create another function in lib/posts.js called getAllPostSlugs. The maximum amount of items that can be fetched from a resource at once is 100, so use pagination to make sure all the slugs are fetched:
Now getSinglePost() and getAllPostSlugs() can be used within the pages/posts/[slug].js file like so:
Pages can be linked to with the Next.js <Link/> component. Calling it can be done with:
The Link component is used like so:
Pages are linked in this fashion within Next.js applications to make full use of client-side rendering as well as server-side rendering. To read more about how the Link component works and it’s use within Next.js apps check out their documentation.

Examples

The flexibility of the Ghost Content API allows you to feed posts, pages and any other pieces of content from Ghost site into a Next.js JavaScript app. Below are a few examples of how content from Ghost can be passed into a Next.js project.

Getting pages

Pages can be generated in the same fashion as posts, and can even use the same dynamic route file.

Adding post attribute data

Using the include option within the Ghost Content API means that attribute data, such as tags and authors, will be included in the post object data:

Rendering author pages

An author can be requested using the authors.read() endpoint.
A custom author template file can be created at pages/authors/[slug].js, which will also prevent author URLs colliding with post and page URLs:

Formatting post dates

The published date of a post, post.published_at, is returned as a date timestamp. Modern JavaScript methods can convert this date into a selection of humanly readable formats. To output the published date as “Aug 28, 1963”:
The date can then be added to the template using {post.dateFormatted}.

Further reading

Check out the extensive Next.js documentation and learning courses for more information and to get more familiar when working with Next.js.