These integration guides are not official documentation and the Strapi Support Team will not provide assistance with them.
What Is Nuxt.js
Nuxt.js is an open-source framework built on Vue.js that simplifies modern web application development. It supports server-side rendering (SSR), static site generation (SSG), and single-page application (SPA) features, which makes it a versatile tool for fast, SEO-friendly websites.
Nuxt.js automatically manages routing, code splitting, and hot module reloading to improve the developer experience. It also seamlessly integrates with back-end services via REST and GraphQL APIs. Thanks to its modular architecture, Nuxt.js allows easy functionality extension and performance optimization, making it ideal for building scalable, dynamic web applications.
Why Integrate Nuxt.js with Strapi
Integrating Nuxt.js with Strapi creates a powerful combination for building fast, scalable web applications.
Hereβs why this integration is a great choice:
- Seamless Content Management: Strapi is a headless CMS that provides an intuitive interface for managing content. Nuxt.js consumes this content via REST or GraphQL APIs.
- High Performance: Nuxt.jsβs SSR and SSG improve SEO and loading times, ensuring your app is fast and search engine optimized.
- Decoupled Architecture: Separating the front-end (Nuxt.js) from the back-end (Strapi) offers flexibility to deliver content across multiple platforms, including web, mobile, or IoT.
- Scalability: Both Nuxt.js and Strapi are built for scalability. Strapiβs API-driven model scales your content, while Nuxt.js seamlessly handles increasing traffic.
- Streamlined Development: Nuxt.js supports automatic code splitting and hot module reloading to boost developer productivity. Strapiβs extensible architecture allows for easy customization and plugin integration.
Keep in touch with the latest Strapi and Nuxt.js updates
How to Integrate Nuxt.js with Strapi
Integrating Nuxt.js with Strapi creates a powerful setup for building dynamic, content-driven websites. If youβre new to headless CMS, our headless CMS guide can help clarify the concepts before you begin.
1. Set Up the Strapi Backend
To create a new Strapi project, run:
1npx create-strapi-app@latest my-project --quickstart
Once installation is complete, open http://localhost:1337/admin in your browser. Here, you can set up your content types and configure API permissions.
2. Set Up the Nuxt.js Frontend
Create a new Nuxt.js project with:
1npx nuxi init my-nuxt-frontend
Next, install the Strapi module for Nuxt.js:
1npm install @nuxtjs/strapi
Register the module and configure it in your nuxt.config.js
file:
1modules: [
2 '@nuxtjs/strapi'
3],
4
5strapi: {
6 url: process.env.STRAPI_URL || 'http://localhost:1337/api',
7 entities: ['articles', 'categories']
8}
3. Connect Nuxt.js to Strapiβs REST API
In the Strapi admin panel, go to Settings β Roles & Permissions and adjust permissions so your Nuxt.js frontend can access the necessary content types.
To fetch data from Strapi in Nuxt.js, use the $strapi
service. For example, to fetch articles:
1export default {
2 async asyncData({ $strapi }) {
3 const articles = await $strapi.find('articles')
4 return { articles }
5 }
6}
For more complex queries, you can use filters, sorting, and pagination:
1const featuredArticles = await $strapi.find('articles', {
2 filters: { featured: true },
3 populate: '*',
4 sort: 'publishedAt:desc',
5 pagination: { page: 1, pageSize: 10 }
6})
4. (Optional) Integrate GraphQL
To enable GraphQL in Strapi, install the plugin:
1npm install @strapi/plugin-graphql
For more details, see the Strapi GraphQL setup guide.
To use GraphQL in Nuxt.js, install Apollo dependencies:
1npm install @nuxtjs/apollo graphql
Configure Apollo in your nuxt.config.js
:
1modules: [
2 '@nuxtjs/apollo',
3],
4apollo: {
5 clientConfigs: {
6 default: {
7 httpEndpoint: 'http://localhost:1337/graphql'
8 }
9 }
10}
You can now create and use GraphQL queries in your components:
1import gql from 'graphql-tag'
2
3export default {
4 apollo: {
5 articles: gql`
6 query {
7 articles {
8 data {
9 id
10 attributes {
11 title
12 content
13 }
14 }
15 }
16 }
17 `
18 }
19}
5. Advanced Configuration
For authentication, use the built-in methods provided by the Nuxt Strapi module:
1await this.$strapi.login({ identifier: 'user@example.com', password: 'password' })
For more on authentication and authorization, see the Strapi authentication guide.
When working with media, construct full URLs by combining the Strapi base URL and the media path:
1getMediaUrl(mediaPath) {
2 return `${this.$strapi.url}${mediaPath}`
3}
To manage environment variables in Nuxt.js, use the following in your nuxt.config.js
:
1// nuxt.config.js
2env: {
3 strapiBaseUri: process.env.API_URL || 'http://localhost:1337'
4}
6. Follow Best Practices for Production
- Implement error handling, security measures, and query optimization.
- Use caching strategies and a CDN for optimized content delivery as your app scales.
Learn how to use a CDN with Strapi.
By following these steps, youβll have a Nuxt.js frontend seamlessly connected to a Strapi backend, ready for scalable and content-rich web applications.
Keep in touch with the latest Strapi and Nuxt.js updates
Project Example (GitHub Project Repo)
This guide walks through a practical example of integrating Nuxt.js with Strapi to create a dynamic blog platform. It highlights how Strapiβs content management capabilities and Nuxt.jsβs rendering features combine to build a fast, SEO-friendly website.
For another practical example, you can learn how to build a News Aggregator App using Strapi and Nuxt.js.
Content Type Creation in Strapi
To get started, you'll first need to set up content types in Strapi:
- Post: The main content type for blog articles.
- Fields: Title (Text), Content (Rich Text), Author (Relation to User), PublishDate (Date), Featured Image (Media)
- Category: To organize blog posts.
- Fields: Name (Text), Description (Text)
- Tag: For additional classification.
- Fields: Name (Text)
API Configuration
Configure API permissions in Strapi:
- Go to Settings > Roles & Permissions > Public
- Enable "find" and "findOne" permissions for Post, Category, and Tag content types
- Save your changes
Data Fetching in Nuxt.js
Set up your Nuxt.js project to fetch data from Strapi:
1// plugins/strapi.js
2import { strapi } from '@strapi/client';
3
4const client = strapi({ baseURL: 'http://localhost:1337/api' });
5
6// nuxt.config.js
7export default {
8 plugins: ['~/plugins/strapi'],
9 // ... other configurations
10}
11
12// pages/index.vue
13export default {
14 async asyncData({ $strapi }) {
15 const posts = await $strapi.$get('/posts?populate=*')
16 return { posts: posts.data }
17 }
18}
Rendering Content Dynamically
Render your data in Nuxt.js components:
1<!-- components/PostList.vue -->
2<template>
3 <div>
4 <article v-for="post in posts" :key="post.id">
5 <h2>{{ post.attributes.title }}</h2>
6 <img :src="post.attributes.featuredImage.data.attributes.url" :alt="post.attributes.title">
7 <p>{{ post.attributes.content.substring(0, 150) }}...</p>
8 <nuxt-link :to="`/post/${post.id}`">Read more</nuxt-link>
9 </article>
10 </div>
11</template>
12
13<script>
14export default {
15 props: ['posts']
16}
17</script>
Key Integration Highlights
- Dynamic Routing: Nuxt.js automatically generates routes based on your project's file and folder structure. You can configure these routes to display Strapi content by manually creating route files and fetching data from Strapi.
- SEO: Use Nuxt's
head
method to set meta tags based on Strapi content:
1// pages/post/_id.vue
2export default {
3 async asyncData({ $strapi, params }) {
4 const post = await $strapi.$get(`/posts/${params.id}?populate=*`)
5 return { post: post.data }
6 },
7 head() {
8 return {
9 title: this.post.attributes.title,
10 meta: [
11 { hid: 'description', name: 'description', content: this.post.attributes.content.substring(0, 160) }
12 ]
13 }
14 }
15}
- Image Handling: Use Nuxt Image for optimized image loading:
1<nuxt-img
2 :src="post.attributes.featuredImage.data.attributes.url"
3 :alt="post.attributes.title"
4 width="800"
5 height="400"
6/>
- GraphQL Integration: For complex data needs, use Strapi's GraphQL API:
1// With Apollo module
2export default {
3 apollo: {
4 posts: gql`
5 query {
6 posts {
7 data {
8 id
9 attributes {
10 title
11 content
12 }
13 }
14 }
15 }
16 `
17 }
18}
This example shows the flexibility, performance, and SEO benefits of integrating Nuxt.js with Strapi. You can efficiently build dynamic, scalable web applications by combining Strapiβs content management with Nuxt.jsβs rendering and routing capabilities.
Check out Strapiβs GitHub repository for the current code and best practices.
Strapi Open Office Hours
If you have any questions about Strapi 5 or just would like to stop by and say hi, you can join us at Strapi's Discord Open Office Hours, Monday through Friday, from 12:30 pm to 1:30 pm CST: Strapi Discord Open Office Hours.
For more details, visit the Strapi documentation and the Nuxt.js documentation.