Simple guide to add JustCMS content management to your Nuxt project
Setting up JustCMS with your Nuxt project is straightforward and only requires a few simple steps. Let's walk through the process:
Copy useJustCMS.ts file into your project's composables directory
Add your JustCMS token and project ID to nuxt.config.ts
First, make sure your project is ready:
The first step is to add the JustCMS composable to your project:
Next, you'll need to add your JustCMS credentials to your Nuxt configuration. Open your nuxt.config.ts file and add the following:
export default defineNuxtConfig({
runtimeConfig: {
public: {
justCmsToken: 'YOUR_JUSTCMS_API_TOKEN',
justCmsProject: 'YOUR_JUSTCMS_PROJECT_ID'
}
}
})
Replace the placeholder values with your actual credentials:
With these simple steps completed, you're ready to start using JustCMS in your Nuxt project. The composable is now available throughout your application, and you can begin fetching your content right away.
Using JustCMS in your Nuxt components is straightforward. The composable provides simple methods to fetch categories, pages, and menus. Let's look at some common usage examples.
To display categories in your component, import the composable and use the getCategories() method:
<template>
<div>
<h2>Categories</h2>
<ul>
<li v-for="cat in categories" :key="cat.slug">
{{ cat.name }}
</li>
</ul>
</div>
</template>
<script setup lang="ts">
import { useJustCMS } from '~/composables/useJustCMS'
const { getCategories } = useJustCMS()
const categories = await getCategories()
</script>
The composable offers flexible options for fetching pages. You can get all pages, filter by category, or use pagination:
<template>
<div>
<h2>Blog Posts</h2>
<div v-for="page in pages.pages" :key="page.slug">
<h3>{{ page.title }}</h3>
<p>{{ page.description }}</p>
</div>
</div>
</template>
<script setup lang="ts">
import { useJustCMS } from '~/composables/useJustCMS'
const { getPages } = useJustCMS()
const pages = await getPages({
category: 'blog',
page: 1,
perPage: 10
})
</script>
To fetch and display a single page by its slug, use the getPageBySlug() method:
<template>
<div v-if="page">
<h1>{{ page.title }}</h1>
<div v-html="page.content"></div>
</div>
</template>
<script setup lang="ts">
import { useJustCMS } from '~/composables/useJustCMS'
const { getPageBySlug } = useJustCMS()
const page = await getPageBySlug('about-us')
</script>
To fetch and display a menu by its ID, use the getMenuById() method:
<template>
<nav v-if="menu">
<ul>
<li v-for="item in menu.items" :key="item.id">
<NuxtLink :to="item.url">{{ item.label }}</NuxtLink>
</li>
</ul>
</nav>
</template>
<script setup lang="ts">
import { useJustCMS } from '~/composables/useJustCMS'
const { getMenuById } = useJustCMS()
const menu = await getMenuById('main-menu')
</script>
All data is fetched on the server before page load, improving SEO and performance
Full TypeScript support ensures your code is reliable and maintainable
Clean and intuitive methods make content fetching straightforward
Support for filtering, pagination, and different content types
These examples demonstrate how easy it is to integrate JustCMS content into your Nuxt components. The composable handles all the API communication, letting you focus on building your user interface.
The JustCMS composable provides four main functions to fetch different types of content from your project. Let's explore each function and how to use them effectively.
The simplest function that retrieves all categories from your JustCMS project. It requires no parameters and returns an array of categories.
const { getCategories } = useJustCMS()
const categories = await getCategories()
// Returns: Category[]
A versatile function that allows you to fetch pages with various options for filtering and pagination.
category, page, perPage
Object with pages array, total count, current page, and total pages
// Get all pages
const allPages = await getPages()
// Get pages from specific category
const categoryPages = await getPages({ category: 'blog' })
// Get paginated results
const paginatedPages = await getPages({ page: 1, perPage: 10 })
Use this function when you need to fetch a single page by its unique slug. Perfect for individual page views or detailed content displays.
const page = await getPageBySlug('about-us')
// Returns: Page | null
Retrieve a complete menu structure including all its items. This is commonly used for navigation menus or site structure.
const menu = await getMenuById('main-menu')
// Returns: Menu | null
All functions work seamlessly with Nuxt's SSR capabilities
Full TypeScript support for all function returns
Functions return null when content isn't found
These functions provide everything you need to fetch and display content from your JustCMS project. They're designed to be simple to use while giving you full flexibility in how you structure your content.
JustCMS provides several utility functions that make it easier to work with your content. These helper functions handle common tasks when working with content blocks, images, and categories.
The isBlockHasStyle() function helps you check if a content block has a specific style applied to it:
const isHighlighted = isBlockHasStyle(block, 'highlight')
// Returns: boolean
This function performs a case-insensitive check, making it more flexible and easier to use in your components.
Retrieves the large variant of an image (second variant in the array)
Gets the first image from an image block
// Get large image variant
const largeImage = getLargeImageVariant(page.coverImage)
// Get first image from block
const firstImage = getFirstImage(imageBlock)
To check if a page belongs to a specific category, use the hasCategory() function:
const isBlogPost = hasCategory(page, 'blog')
// Returns: boolean
These utility functions are designed to simplify common tasks and make your code more readable and maintainable. They handle edge cases and provide a consistent interface for working with JustCMS content.
All utility functions are fully typed for TypeScript support
Simple function calls with clear parameters
Standardized approach to common content operations