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
- Node.js 20 or higher.
- A Content Island account.
Prepare your content in Content Island
-
Create a new project from the Projects page.
-
In the Model tab, create a model called
postwith these fields:Field name Type Notes titleShort Text Required. slugShort Text Required and unique. contentLong Text Markdown body of the post. -
In the Content tab, add two or three posts and publish them.
-
In the General tab, copy the project’s Token (the API access token).
Create the Nuxt project
-
Create a new project:
Terminal window npm create nuxt@latest my-blogcd my-blog -
Add the Content Island module (it installs the package and registers it in
nuxt.config.ts) andmarked(to render Markdown):Terminal window npx nuxi@latest module add @content-island/nuxtnpm 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:
CONTENT_ISLAND_ACCESS_TOKEN=your-access-tokenAnd pass it to the module in 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
| Option | Type | Required | Default | Description |
|---|---|---|---|---|
accessToken | string | Yes | – | Your Content Island API token. |
domain | string | No | api.contentisland.net | Custom API domain (on premise). |
secureProtocol | boolean | No | true | Use HTTPS for API requests. |
apiVersion | string | No | 1.0 | API 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:
$contentIsland.getContentList(...)calls a server route that the module registers under/api/__content_island/*.- That server route uses the token from the private runtime config and calls Content Island.
- 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:
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:
<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:
<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:
npm install @content-island/api-clientimport { 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):
<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:
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:
CONTENT_ISLAND_ACCESS_TOKEN=PREVIEW_your-access-tokenSee Preview Token for more details.
Next steps
- Deploy your app: Vercel, Netlify or Cloudflare Pages.
- Serve several languages with Multilingual Support.
- Explore every method of the Client API library.
- See the module source and its playground on GitHub.