Learn how to spin up a JavaScript app using Ghost as a headless CMS and build a completely custom front-end with Vue and Nuxt.js.

The following sections cover how to source content from Ghost and feed it directly into a web app built with Nuxt.js.

Diagram of Ghost providing content to Nuxt.js via the Content API

Prerequisites

This configuration of a Ghost publication requires existing moderate knowledge of JavaScript as well as Vue.js. 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 Nuxt.js application via the command line:

yarn create nuxt-app my-nuxt-app
cd my-nuxt-app
yarn dev

To install Nuxt.js manually refer to the official documentation for more information.

Getting started

Thanks to the JavaScript Content API Client Library, content from a Ghost site can be directly accessed within a Nuxt.js application.

Create a new file called posts.js within an api/ 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:

yarn add @tryghost/content-api

Once the helper is installed it can be added to the posts.js file using a static import statement:

import GhostContentAPI from "@tryghost/content-api";

Now an instance of the Ghost Content API can be created using Ghost site credentials:

import GhostContentAPI from "@tryghost/content-api";

// Create API instance with site credentials
const api = new GhostContentAPI({
  url: 'https://demo.ghost.io',
  key: '22444f78447824223cefc48062',
  version: "v2"
});

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.

Ghost Admin new Integration view

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:

export async function getPosts() {
  return await api.posts
    .browse({
      limit: "all"
    })
    .catch(err => {
      console.error(err);
    });
}

Using an async function means the Nuxt.js application will wait until all the content has been retrieved before loading the page. Since this function is being exported using the export notation, it will be available throughout the application.

Rendering posts

Since Nuxt.js is based on .vue, files can contain HTML, CSS and JavaScript to create a neatly packaged up component. For more information check out the official Vue.js documentation.

To render out a list of posts from a Ghost site, create a new index.vue file within a pages/ directory of your Nuxt.js project. Use the following code to expose the getPosts function within the index.vue file:

<script>
  import { getPosts } from '../api/posts';

  ...
</script>

The posts are provided as data to the rest of the .vue file using a asyncData function within the Nuxt.js framework:

<script>
  import { getPosts } from '../api/posts';

  export default {
    async asyncData () {
      const posts = await getPosts();
      return { posts: posts }
    }
  }
</script>

Posts will now be available to use within that file and can be generated as a list using Vue.js list rendering:

<template>
  <ul>
    <li v-for="post in posts">{{ post.title }}</li>
  </ul>
</template>

<script>
  import { getPosts } from '../api/posts';

  export default {
    async asyncData () {
      const posts = await getPosts();
      return { posts: posts }
    }
  }
</script>

For more information about how pages work, check out the Nuxt.js pages 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 api/posts.js file and add the following async function:

export async function getSinglePost(postSlug) {
  return await api.posts
    .read({
      slug: postSlug
    })
    .catch(err => {
      console.error(err);
    });
}

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.

Nuxt.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, prefixed with an underscore – _slug.vue.

The getSinglePost() function can be used within the _slug.vue file like so:

<template>
  <div>
    <h1>{{ post.title }}</h1>
    <div v-html="post.html"/>
  </div>
</template>

<script>
  import { getSinglePost } from '../api/posts';

  export default {
    async asyncData ({ params }) {
      const post = await getSinglePost(params.slug);
      return { post: post }
    }
  }
</script>

The <nuxt-link/> component can be used with the post.slug to link to posts from the listed posts in pages/index.vue:

<template>
  <ul>
    <li v-for="post in posts">
      <nuxt-link :to="{ path: post.slug }">{{ post.title }}</nuxt-link>
    </li>
  </ul>
</template>

Pages are linked in this fashion to make full use of client-side rendering as well as server-side rendering. To read more about how the <nuxt-link/> component works, check out the official documentation.

Next steps

Well done! You should have now retrieved posts from the Ghost Content API and sent them to your Nuxt.js site. For examples of how to extend this further by generating content pages, author pages or exposing post attributes, read our useful recipes.

Don't forget to refer to the official Nuxt.js guides and API documentation to get a greater understanding of the framework.