Skip to content

Get started with Nuxt

In this guide you will build a small blog with Nuxt that reads its posts from Content Island using the official @content-island/nuxt module:

  • A home page that lists every post.
  • A detail page per post, at /posts/<slug>, that renders its Markdown content.

Along the way you will see the basics you need in any Nuxt project: how the module keeps your token on the server, useAsyncData, server routes and the different rendering modes.

Prerequisites

Prepare your content in Content Island

  1. Create a new project from the Projects page.

  2. In the Model tab, create a model called post with these fields:

    Field nameTypeNotes
    titleShort TextRequired.
    slugShort TextRequired and unique.
    contentLong TextMarkdown body of the post.
  3. In the Content tab, add two or three posts and publish them.

  4. In the General tab, copy the project’s Token (the API access token).

Create the Nuxt project

  1. Create a new project:

    Terminal window
    npm create nuxt@latest my-blog
    cd my-blog
  2. Add the Content Island module (it installs the package and registers it in nuxt.config.ts) and marked (to render Markdown):

    Terminal window
    npx nuxi@latest module add @content-island/nuxt
    npm install marked

Configure the access token

The access token gives access to your project, so it must never reach the browser. Store it in an environment variable:

.env
CONTENT_ISLAND_ACCESS_TOKEN=your-access-token

And pass it to the module in nuxt.config.ts:

nuxt.config.ts
export default defineNuxtConfig({
modules: ['@content-island/nuxt'],
contentIsland: {
accessToken: process.env.CONTENT_ISLAND_ACCESS_TOKEN,
},
});

The module stores these options in the private runtimeConfig, which only exists on the server. In production you can also override the token at runtime with the NUXT_CONTENT_ISLAND_ACCESS_TOKEN environment variable.

Module options

OptionTypeRequiredDefaultDescription
accessTokenstringYes–Your Content Island API token.
domainstringNoapi.contentisland.netCustom API domain (on premise).
secureProtocolbooleanNotrueUse HTTPS for API requests.
apiVersionstringNo1.0API version to use.

How the module works

The module gives you a $contentIsland object with the same read methods as the Client API library (getContentList, getContent, getProject…). But it never calls Content Island from the browser:

  1. $contentIsland.getContentList(...) calls a server route that the module registers under /api/__content_island/*.
  2. That server route uses the token from the private runtime config and calls Content Island.
  3. The result goes back to your component.

During server-side rendering the call doesn’t even go through the network, and when the user navigates in the browser the request goes to your own server. Either way, the token stays on the server.

Define the model type

Create a type that mirrors the post model:

app/types/post.ts
export interface Post {
id: string;
title: string;
slug: string;
content: string;
}

List the posts

Use useAsyncData to fetch data in a page. Nuxt runs it on the server for the first request, sends the result to the browser inside the HTML and doesn’t fetch it again during hydration:

app/pages/index.vue
<script setup lang="ts">
import type { Post } from '~/types/post';
const { $contentIsland } = useNuxtApp();
const { data: posts } = await useAsyncData('posts', () => $contentIsland.getContentList<Post>({ contentType: 'post' }));
</script>
<template>
<main>
<h1>My blog</h1>
<ul>
<li v-for="post in posts" :key="post.id">
<NuxtLink :to="`/posts/${post.slug}`">{{ post.title }}</NuxtLink>
</li>
</ul>
</main>
</template>

Run npm run dev and open http://localhost:3000 to see your posts.

Show a post

Create a dynamic route. The [slug] part of the file name is available in route.params.slug. Throw a 404 error with createError when the post doesn’t exist:

app/pages/posts/[slug].vue
<script setup lang="ts">
import { marked } from 'marked';
import type { Post } from '~/types/post';
const route = useRoute();
const slug = route.params.slug as string;
const { $contentIsland } = useNuxtApp();
const { data: post } = await useAsyncData(`post-${slug}`, async () => {
const [found] = await $contentIsland.getContentList<Post>({ contentType: 'post', 'fields.slug': slug });
return found ? { ...found, html: await marked.parse(found.content) } : null;
});
if (!post.value) {
throw createError({ statusCode: 404, statusMessage: 'Post not found', fatal: true });
}
</script>
<template>
<main v-if="post">
<NuxtLink to="/">← Back</NuxtLink>
<h1>{{ post.title }}</h1>
<article v-html="post.html" />
</main>
</template>

Server basics in Nuxt

Server routes: your own server functions

Every file in server/api becomes an HTTP endpoint that runs only on the server (Nuxt’s server engine, Nitro). This is where you put logic that the module doesn’t cover: combining several requests, paginating, transforming data or using a Write Token.

For example, a paginated endpoint for a “Load more” button. Install the client to use it directly:

Terminal window
npm install @content-island/api-client
server/api/posts.get.ts
import { createClient } from '@content-island/api-client';
import type { Post } from '~/types/post';
const PAGE_SIZE = 5;
export default defineEventHandler(async event => {
const { contentIsland } = useRuntimeConfig(event);
const client = createClient({ accessToken: contentIsland.accessToken });
const page = Number(getQuery(event).page ?? 0);
return client.getContentList<Post>({
contentType: 'post',
pagination: { take: PAGE_SIZE, skip: page * PAGE_SIZE },
});
});

The .get.ts suffix limits the route to GET requests. Call it from any component with $fetch (or useFetch while rendering the page):

app/components/LoadMore.vue
<script setup lang="ts">
import type { Post } from '~/types/post';
const posts = ref<Post[]>([]);
const page = ref(0);
const loadMore = async () => {
const nextPosts = await $fetch<Post[]>('/api/posts', { query: { page: page.value } });
posts.value.push(...nextPosts);
page.value++;
};
</script>
<template>
<ul>
<li v-for="post in posts" :key="post.id">{{ post.title }}</li>
</ul>
<button @click="loadMore">Load more</button>
</template>

Save it as app/components/LoadMore.vue. Nuxt auto-imports components, so you can use <LoadMore /> in any page without importing it.

Rendering modes

By default Nuxt renders every page on the server on each request (SSR), so content changes are visible immediately. You can change it per route with routeRules, or prerender the whole site:

nuxt.config.ts
export default defineNuxtConfig({
routeRules: {
// Generate the home page at build time.
'/': { prerender: true },
// Cache the posts and regenerate them in the background at most every 60 seconds.
'/posts/**': { swr: 60 },
},
});

To generate a fully static site (SSG) for any static host, run npx nuxi generate.

Preview unpublished content

While you develop, prefix the token with PREVIEW_ to also read content that is not published yet:

.env
CONTENT_ISLAND_ACCESS_TOKEN=PREVIEW_your-access-token

See Preview Token for more details.

Next steps