# Create project directory
mkdir vue-graphql-ssr
cd vue-graphql-ssr
# Initialize Nuxt 3 project structure
npx nuxi@latest init . --no-git
package.json
Replace the generated package.json with this (Nuxt 3.12+ with no extra deps needed):
{
"name": "vue-graphql-ssr",
"private": true,
"type": "module",
"scripts": {
"build": "nuxt build",
"dev": "nuxt dev",
"generate": "nuxt generate",
"preview": "nuxt preview",
"postinstall": "nuxt prepare"
},
"devDependencies": {
"@nuxt/devtools": "latest",
"nuxt": "^3.13.2"
}
}
Run yarn install (or npm install / pnpm install).
nuxt.config.ts
// https://nuxt.com/docs/api/configuration/nuxt-config
export default defineNuxtConfig({
devtools: { enabled: true },
ssr: true // Explicitly enabled (default)
})
pages/index.vue
Replace the default content with this complete page:
<template>
<div class="container">
<h1 class="title">Star Wars Films</h1>
<p class="description">
Vue.js + Nuxt 3 Server-Side Rendering + GraphQL Integration Demo
</p>
<div v-if="pending" class="loading">Loading films...</div>
<ul v-else-if="data?.length" class="films">
<li v-for="film in data" :key="film.title" class="film">
<strong>Episode {{ film.episodeID }}</strong>: {{ film.title }}
</li>
</ul>
<div v-else-if="error" class="error">
Error loading films: {{ error.message }}
</div>
<div v-else class="empty">No films found.</div>
</div>
</template>
<script setup lang="ts">
// Nuxt's useFetch handles SSR, hydration, caching, pending/error states automatically.
// Fetches GraphQL query on server, serializes to HTML & payload for client hydration.
const {
data,
pending,
error
} = await useFetch('https://swapi-graphql.netlify.app/api', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: {
query: `
query AllFilms {
allFilms {
films {
title
episodeID
}
}
}
`
},
transform: (response) => response.data.allFilms.films,
// Optional: server-only fetch (no client refetch)
// server: false
})
</script>
<style scoped>
.container {
min-height: 100vh;
padding: 4rem 2rem;
display: flex;
flex-direction: column;
justify-content: flex-start;
align-items: center;
text-align: center;
}
.title {
font-size: 3rem;
line-height: 1;
font-weight: bold;
letter-spacing: -0.025em;
margin-bottom: 1rem;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
.description {
font-size: 1.25rem;
color: #666;
max-width: 42rem;
margin-bottom: 3rem;
}
.loading,
.error,
.empty {
font-size: 1.5rem;
color: #888;
margin: 2rem 0;
}
.films {
list-style: none;
padding: 0;
margin: 0;
max-width: 600px;
width: 100%;
}
.film {
display: flex;
align-items: center;
padding: 1.5rem;
margin: 0.5rem 0;
background: rgba(255, 255, 255, 0.8);
backdrop-filter: blur(10px);
border-radius: 12px;
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
font-size: 1.2rem;
transition: transform 0.2s ease;
}
.film:hover {
transform: translateY(-2px);
}
.film strong {
color: #667eea;
margin-right: 1rem;
min-width: 120px;
}
@media (max-width: 768px) {
.container {
padding: 2rem 1rem;
}
.title {
font-size: 2rem;
}
.film {
flex-direction: column;
align-items: flex-start;
font-size: 1.1rem;
}
.film strong {
margin-right: 0;
margin-bottom: 0.5rem;
}
}
</style>
Run the App
yarn dev
- Open http://localhost:3000
- SSR Verification:
- View page source: HTML contains rendered film list (no "Loading").
- Disable JS: Page still shows data (true SSR).
- Network tab: No GraphQL request on initial load (data in
__NUXT_JSON_PAYLOAD).
- Refresh: Hydrates instantly on client.
Features Demonstrated
- Vue.js 3 + Nuxt 3 SSR: Full server rendering with client hydration.
- GraphQL Integration: Direct POST to public SWAPI GraphQL API using
useFetch.
- Auto-handling: Pending, error, transform states. SSR-safe, cached, reactive.
- Production:
yarn build && yarn preview for static/SSR preview.
- Extensibility: Add mutations, auth (headers from
useRuntimeConfig), Apollo/Pinia if needed.
This is a production-ready starter. Customize the GraphQL query/body for your schema! 🚀